5
5
use PhpParser \Node ;
6
6
use PHPStan \Analyser \Scope ;
7
7
use PHPStan \Rules \Rule ;
8
+ use PHPStan \ShouldNotHappenException ;
8
9
9
10
class PluginManagerInspectionRule implements Rule
10
11
{
@@ -38,14 +39,14 @@ public function processNode(Node $node, Scope $scope): array
38
39
39
40
foreach ($ node ->stmts as $ stmt ) {
40
41
if ($ stmt instanceof Node \Stmt \ClassMethod && $ stmt ->name ->toString () === '__construct ' ) {
41
- foreach ($ stmt ->stmts as $ statement ) {
42
+ foreach ($ stmt ->stmts ?? [] as $ statement ) {
42
43
if ($ statement instanceof Node \Stmt \Expression) {
43
44
$ statement = $ statement ->expr ;
44
45
}
45
- if ($ statement instanceof Node \Expr \MethodCall) {
46
- if (( string ) $ statement ->name === ' alterInfo ' ) {
47
- $ hasAlterInfoSet = true ;
48
- }
46
+ if ($ statement instanceof Node \Expr \MethodCall
47
+ && $ statement ->name instanceof Node \Identifier
48
+ && $ statement -> name -> name === ' alterInfo ' ) {
49
+ $ hasAlterInfoSet = true ;
49
50
}
50
51
}
51
52
}
@@ -58,25 +59,27 @@ public function processNode(Node $node, Scope $scope): array
58
59
return $ errors ;
59
60
}
60
61
61
- private function isYamlDiscovery (Node \Stmt \Class_ $ class ): bool {
62
+ private function isYamlDiscovery (Node \Stmt \Class_ $ class ): bool
63
+ {
62
64
foreach ($ class ->stmts as $ stmt ) {
63
65
// YAML discovery plugin managers must override getDiscovery.
64
66
if ($ stmt instanceof Node \Stmt \ClassMethod && $ stmt ->name ->toString () === 'getDiscovery ' ) {
65
- foreach ($ stmt ->stmts as $ methodStmt ) {
66
-
67
+ foreach ($ stmt ->stmts ?? [] as $ methodStmt ) {
67
68
if ($ methodStmt instanceof Node \Stmt \If_) {
68
69
foreach ($ methodStmt ->stmts as $ ifStmt ) {
69
70
if ($ ifStmt instanceof Node \Stmt \Expression) {
70
71
$ ifStmtExpr = $ ifStmt ->expr ;
71
72
if ($ ifStmtExpr instanceof Node \Expr \Assign) {
72
73
$ ifStmtExprVar = $ ifStmtExpr ->var ;
73
- if (
74
- $ ifStmtExprVar instanceof Node \Expr \PropertyFetch
74
+ if ($ ifStmtExprVar instanceof Node \Expr \PropertyFetch
75
75
&& $ ifStmtExprVar ->var instanceof Node \Expr \Variable
76
+ && $ ifStmtExprVar ->name instanceof Node \Identifier
76
77
&& $ ifStmtExprVar ->name ->name === 'discovery '
77
78
) {
78
79
$ ifStmtExprExpr = $ ifStmtExpr ->expr ;
79
- if ($ ifStmtExprExpr instanceof Node \Expr \New_ && $ ifStmtExprExpr ->class ->toString () === 'Drupal\Core\Plugin\Discovery\YamlDiscovery ' ) {
80
+ if ($ ifStmtExprExpr instanceof Node \Expr \New_
81
+ && ($ ifStmtExprExpr ->class instanceof Node \Name)
82
+ && $ ifStmtExprExpr ->class ->toString () === 'Drupal\Core\Plugin\Discovery\YamlDiscovery ' ) {
80
83
return true ;
81
84
}
82
85
}
@@ -91,19 +94,24 @@ private function isYamlDiscovery(Node\Stmt\Class_ $class): bool {
91
94
return false ;
92
95
}
93
96
94
- private function inspectYamlPluginManager (Node \Stmt \Class_ $ class ): array {
97
+ private function inspectYamlPluginManager (Node \Stmt \Class_ $ class ): array
98
+ {
95
99
$ errors = [];
96
100
97
- $ fqn = (string ) $ class ->namespacedName ;
101
+ $ fqn = (string )$ class ->namespacedName ;
98
102
$ reflection = new \ReflectionClass ($ fqn );
99
103
$ constructor = $ reflection ->getConstructor ();
100
104
105
+ if ($ constructor === null ) {
106
+ throw new ShouldNotHappenException ();
107
+ }
108
+
101
109
if ($ constructor ->class !== $ fqn ) {
102
110
$ errors [] = sprintf ('%s must override __construct if using YAML plugins. ' , $ fqn );
103
111
} else {
104
112
foreach ($ class ->stmts as $ stmt ) {
105
113
if ($ stmt instanceof Node \Stmt \ClassMethod && $ stmt ->name ->toString () === '__construct ' ) {
106
- foreach ($ stmt ->stmts as $ constructorStmt ) {
114
+ foreach ($ stmt ->stmts ?? [] as $ constructorStmt ) {
107
115
if ($ constructorStmt instanceof Node \Stmt \Expression) {
108
116
$ constructorStmt = $ constructorStmt ->expr ;
109
117
}
@@ -113,7 +121,7 @@ private function inspectYamlPluginManager(Node\Stmt\Class_ $class): array {
113
121
&& $ constructorStmt ->name instanceof Node \Identifier
114
122
&& $ constructorStmt ->name ->name === '__construct ' ) {
115
123
$ errors [] = sprintf ('YAML plugin managers should not invoke its parent constructor. ' );
116
- }
124
+ }
117
125
}
118
126
}
119
127
}
0 commit comments