5
5
use PhpParser \Node ;
6
6
use PHPStan \Analyser \Scope ;
7
7
use PHPStan \Node \ClassPropertiesNode ;
8
- use PHPStan \Reflection \ClassReflection ;
8
+ use PHPStan \Reflection \ConstructorsHelper ;
9
9
use PHPStan \Rules \Rule ;
10
10
use PHPStan \Rules \RuleErrorBuilder ;
11
11
use PHPStan \ShouldNotHappenException ;
12
- use ReflectionException ;
13
- use function array_key_exists ;
14
- use function explode ;
15
12
use function sprintf ;
16
13
17
14
/**
20
17
class MissingReadOnlyByPhpDocPropertyAssignRule implements Rule
21
18
{
22
19
23
- /** @var array<string, string[]> */
24
- private array $ additionalConstructorsCache = [];
25
-
26
- /**
27
- * @param string[] $additionalConstructors
28
- */
29
20
public function __construct (
30
- private array $ additionalConstructors ,
21
+ private ConstructorsHelper $ constructorsHelper ,
31
22
)
32
23
{
33
24
}
@@ -43,7 +34,7 @@ public function processNode(Node $node, Scope $scope): array
43
34
throw new ShouldNotHappenException ();
44
35
}
45
36
$ classReflection = $ scope ->getClassReflection ();
46
- [$ properties , $ prematureAccess , $ additionalAssigns ] = $ node ->getUninitializedProperties ($ scope , $ this ->getConstructors ($ classReflection ), []);
37
+ [$ properties , $ prematureAccess , $ additionalAssigns ] = $ node ->getUninitializedProperties ($ scope , $ this ->constructorsHelper -> getConstructors ($ classReflection ), []);
47
38
48
39
$ errors = [];
49
40
foreach ($ properties as $ propertyName => $ propertyNode ) {
@@ -82,46 +73,4 @@ public function processNode(Node $node, Scope $scope): array
82
73
return $ errors ;
83
74
}
84
75
85
- /**
86
- * @return string[]
87
- */
88
- private function getConstructors (ClassReflection $ classReflection ): array
89
- {
90
- if (array_key_exists ($ classReflection ->getName (), $ this ->additionalConstructorsCache )) {
91
- return $ this ->additionalConstructorsCache [$ classReflection ->getName ()];
92
- }
93
- $ constructors = [];
94
- if ($ classReflection ->hasConstructor ()) {
95
- $ constructors [] = $ classReflection ->getConstructor ()->getName ();
96
- }
97
-
98
- $ nativeReflection = $ classReflection ->getNativeReflection ();
99
- foreach ($ this ->additionalConstructors as $ additionalConstructor ) {
100
- [$ className , $ methodName ] = explode (':: ' , $ additionalConstructor );
101
- if (!$ nativeReflection ->hasMethod ($ methodName )) {
102
- continue ;
103
- }
104
- $ nativeMethod = $ nativeReflection ->getMethod ($ methodName );
105
- if ($ nativeMethod ->getDeclaringClass ()->getName () !== $ nativeReflection ->getName ()) {
106
- continue ;
107
- }
108
-
109
- try {
110
- $ prototype = $ nativeMethod ->getPrototype ();
111
- } catch (ReflectionException ) {
112
- $ prototype = $ nativeMethod ;
113
- }
114
-
115
- if ($ prototype ->getDeclaringClass ()->getName () !== $ className ) {
116
- continue ;
117
- }
118
-
119
- $ constructors [] = $ methodName ;
120
- }
121
-
122
- $ this ->additionalConstructorsCache [$ classReflection ->getName ()] = $ constructors ;
123
-
124
- return $ constructors ;
125
- }
126
-
127
76
}
0 commit comments