File tree 5 files changed +142
-0
lines changed
e2e/ignore-error-extension
5 files changed +142
-0
lines changed Original file line number Diff line number Diff line change 1
1
parameters :
2
2
ignoreErrors :
3
+ -
4
+ message : ' #^This is an error from a rule that uses a collector$#'
5
+ identifier : class.name
6
+ count : 1
7
+ path : src/ClassCollector.php
8
+
9
+ -
10
+ message : ' #^This is an error from a rule that uses a collector$#'
11
+ identifier : class.name
12
+ count : 1
13
+ path : src/ClassRule.php
14
+
15
+ -
16
+ message : ' #^This is an error from a rule that uses a collector$#'
17
+ identifier : class.name
18
+ count : 1
19
+ path : src/ControllerActionReturnTypeIgnoreExtension.php
20
+
21
+ -
22
+ message : ' #^This is an error from a rule that uses a collector$#'
23
+ identifier : class.name
24
+ count : 1
25
+ path : src/ControllerClassNameIgnoreExtension.php
26
+
3
27
-
4
28
message : ' #^Method App\\HomepageController\:\:contactAction\(\) has parameter \$someUnrelatedError with no type specified\.$#'
5
29
identifier : missingType.parameter
Original file line number Diff line number Diff line change @@ -7,7 +7,19 @@ parameters:
7
7
- src
8
8
9
9
services:
10
+ -
11
+ class: App\ClassCollector
12
+ tags:
13
+ - phpstan.collector
14
+ -
15
+ class: App\ClassRule
16
+ tags:
17
+ - phpstan.rules.rule
10
18
-
11
19
class: App\ControllerActionReturnTypeIgnoreExtension
12
20
tags:
13
21
- phpstan.ignoreErrorExtension
22
+ -
23
+ class: App\ControllerClassNameIgnoreExtension
24
+ tags:
25
+ - phpstan.ignoreErrorExtension
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types = 1 );
4
+
5
+ namespace App ;
6
+
7
+ use PhpParser \Node ;
8
+ use PHPStan \Analyser \Scope ;
9
+ use PHPStan \Collectors \Collector ;
10
+
11
+ /**
12
+ * @implements Collector<Node\Stmt\Class_, array{string, int}>
13
+ */
14
+ final class ClassCollector implements Collector
15
+ {
16
+ public function getNodeType (): string
17
+ {
18
+ return Node \Stmt \Class_::class;
19
+ }
20
+
21
+ public function processNode (Node $ node , Scope $ scope ) : ?array
22
+ {
23
+ if ($ node ->name === null ) {
24
+ return null ;
25
+ }
26
+
27
+ return [$ node ->name ->name , $ node ->getStartLine ()];
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace App ;
6
+
7
+ use Override ;
8
+ use PhpParser \Node ;
9
+ use PHPStan \Analyser \Scope ;
10
+ use PHPStan \Node \CollectedDataNode ;
11
+ use PHPStan \Rules \Rule ;
12
+ use PHPStan \Rules \RuleErrorBuilder ;
13
+
14
+ /**
15
+ * @implements Rule<CollectedDataNode>
16
+ */
17
+ final class ClassRule implements Rule
18
+ {
19
+ #[Override]
20
+ public function getNodeType () : string
21
+ {
22
+ return CollectedDataNode::class;
23
+ }
24
+
25
+ #[Override]
26
+ public function processNode (Node $ node , Scope $ scope ) : array
27
+ {
28
+ $ errors = [];
29
+
30
+ foreach ($ node ->get (ClassCollector::class) as $ file => $ data ) {
31
+ foreach ($ data as [$ className , $ line ]) {
32
+ $ errors [] = RuleErrorBuilder::message ('This is an error from a rule that uses a collector ' )
33
+ ->file ($ file )
34
+ ->line ($ line )
35
+ ->identifier ('class.name ' )
36
+ ->build ();
37
+ }
38
+ }
39
+
40
+ return $ errors ;
41
+ }
42
+
43
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace App ;
6
+
7
+ use PhpParser \Node ;
8
+ use PHPStan \Analyser \Error ;
9
+ use PHPStan \Analyser \IgnoreErrorExtension ;
10
+ use PHPStan \Analyser \Scope ;
11
+ use PHPStan \Node \CollectedDataNode ;
12
+
13
+ // This extension will ignore "class.name" errors for classes with names ending with "Controller".
14
+ // These errors are reported by the ClassRule which triggers on CollectedDataNode coming from ClassCollector.
15
+ final class ControllerClassNameIgnoreExtension implements IgnoreErrorExtension
16
+ {
17
+ public function shouldIgnore (Error $ error , Node $ node , Scope $ scope ) : bool
18
+ {
19
+ if ($ error ->getIdentifier () !== 'class.name ' ) {
20
+ return false ;
21
+ }
22
+
23
+ // @phpstan-ignore phpstanApi.instanceofAssumption
24
+ if (!$ node instanceof CollectedDataNode) {
25
+ return false ;
26
+ }
27
+
28
+ if (!str_ends_with ($ error ->getFile (), 'Controller.php ' )) {
29
+ return false ;
30
+ }
31
+
32
+ return true ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments