File tree Expand file tree Collapse file tree 5 files changed +126
-0
lines changed
tests/src/DeprecatedScope Expand file tree Collapse file tree 5 files changed +126
-0
lines changed Original file line number Diff line number Diff line change 43
43
php-version :
44
44
- " 8.1"
45
45
- " 8.2"
46
+ - " 8.3"
46
47
drupal :
47
48
- " ^10"
48
49
include :
Original file line number Diff line number Diff line change @@ -325,6 +325,10 @@ services:
325
325
class : mglaman\PHPStanDrupal\DeprecatedScope\GroupLegacyScope
326
326
tags :
327
327
- phpstan.deprecations.deprecatedScopeResolver
328
+ -
329
+ class : mglaman\PHPStanDrupal\DeprecatedScope\IgnoreDeprecationsScope
330
+ tags :
331
+ - phpstan.deprecations.deprecatedScopeResolver
328
332
-
329
333
class : mglaman\PHPStanDrupal\DeprecatedScope\DeprecationHelperScope
330
334
tags :
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace mglaman \PHPStanDrupal \DeprecatedScope ;
6
+
7
+ use PHPStan \Analyser \Scope ;
8
+ use PHPStan \Rules \Deprecations \DeprecatedScopeResolver ;
9
+ use PHPUnit \Framework \Attributes \IgnoreDeprecations ;
10
+
11
+ final class IgnoreDeprecationsScope implements DeprecatedScopeResolver
12
+ {
13
+
14
+ public function isScopeDeprecated (Scope $ scope ): bool
15
+ {
16
+ if (!class_exists (IgnoreDeprecations::class)) {
17
+ return false ;
18
+ }
19
+
20
+ if ($ scope ->isInClass ()) {
21
+ $ class = $ scope ->getClassReflection ()->getNativeReflection ();
22
+ if ($ class ->getAttributes (IgnoreDeprecations::class) !== []) {
23
+ return true ;
24
+ }
25
+
26
+ $ function = $ scope ->getFunction ();
27
+ if ($ function === null ) {
28
+ return false ;
29
+ }
30
+
31
+ $ method = $ class ->getMethod ($ function ->getName ());
32
+ if ($ method ->getAttributes (IgnoreDeprecations::class) !== []) {
33
+ return true ;
34
+ }
35
+ }
36
+ return false ;
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace mglaman \PHPStanDrupal \Tests \DeprecatedScope ;
6
+
7
+ use mglaman \PHPStanDrupal \Tests \DrupalRuleTestCase ;
8
+ use PHPStan \Rules \Deprecations \CallToDeprecatedFunctionRule ;
9
+ use PHPStan \Rules \Deprecations \DeprecatedScopeHelper ;
10
+ use PHPStan \Rules \Rule ;
11
+ use PHPUnit \Framework \Attributes \IgnoreDeprecations ;
12
+
13
+ final class IgnoreDeprecationsScopeTest extends DrupalRuleTestCase {
14
+
15
+ protected function getRule (): Rule
16
+ {
17
+ // @phpstan-ignore-next-line
18
+ return new CallToDeprecatedFunctionRule (
19
+ self ::createReflectionProvider (),
20
+ self ::getContainer ()->getByType (DeprecatedScopeHelper::class)
21
+ );
22
+ }
23
+
24
+ public function testCustomScope (): void
25
+ {
26
+ if (!class_exists (IgnoreDeprecations::class)) {
27
+ $ errors = [
28
+ [
29
+ 'Call to deprecated function Deprecated\deprecated_function(). ' ,
30
+ 12 ,
31
+ ],
32
+ [
33
+ 'Call to deprecated function Deprecated\deprecated_function(). ' ,
34
+ 20 ,
35
+ ],
36
+ [
37
+ 'Call to deprecated function Deprecated\deprecated_function(). ' ,
38
+ 25 ,
39
+ ],
40
+ ];
41
+ } else {
42
+ $ errors = [
43
+ [
44
+ 'Call to deprecated function Deprecated\deprecated_function(). ' ,
45
+ 20 ,
46
+ ],
47
+ ];
48
+ }
49
+ require_once __DIR__ . '/data/deprecated-data-definition.php ' ;
50
+ $ this ->analyse (
51
+ [__DIR__ . '/data/ignore-deprecations.php ' ],
52
+ $ errors
53
+ );
54
+ }
55
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace IgnoreDeprecations ;
4
+
5
+ use PHPUnit \Framework \Attributes \IgnoreDeprecations ;
6
+ use function Deprecated \deprecated_function ;
7
+
8
+ #[IgnoreDeprecations]
9
+ final class FooTest {
10
+
11
+ public function foo (): void {
12
+ deprecated_function ();
13
+ }
14
+
15
+ }
16
+
17
+ final class BarTest {
18
+
19
+ public function bar (): void {
20
+ deprecated_function ();
21
+ }
22
+
23
+ #[IgnoreDeprecations]
24
+ public function barNot (): void {
25
+ deprecated_function ();
26
+ }
27
+
28
+ }
You can’t perform that action at this time.
0 commit comments