9
9
use PHPStan \BetterReflection \Reflection \Adapter \ReflectionFunction ;
10
10
use PHPStan \BetterReflection \Reflection \Adapter \ReflectionParameter ;
11
11
use PHPStan \BetterReflection \Reflection \ReflectionConstant ;
12
+ use PHPStan \Parser \PropertyHookNameVisitor ;
13
+ use PHPStan \Reflection \Php \PhpMethodFromParserNodeReflection ;
12
14
use PHPStan \ShouldNotHappenException ;
13
15
use function array_slice ;
14
16
use function count ;
@@ -32,21 +34,25 @@ private function __construct(
32
34
private ?string $ traitName ,
33
35
private ?string $ function ,
34
36
private ?string $ method ,
37
+ private ?string $ property ,
35
38
)
36
39
{
37
40
}
38
41
39
42
public static function fromScope (Scope $ scope ): self
40
43
{
44
+ $ function = $ scope ->getFunction ();
45
+
41
46
return new self (
42
47
$ scope ->getFile (),
43
48
$ scope ->getNamespace (),
44
49
$ scope ->isInClass () ? $ scope ->getClassReflection ()->getName () : null ,
45
50
$ scope ->isInTrait () ? $ scope ->getTraitReflection ()->getName () : null ,
46
- $ scope ->isInAnonymousFunction () ? '{closure} ' : ($ scope ->getFunction () !== null ? $ scope ->getFunction ()->getName () : null ),
47
- $ scope ->isInAnonymousFunction () ? '{closure} ' : ($ scope ->getFunction () instanceof MethodReflection
48
- ? sprintf ('%s::%s ' , $ scope ->getFunction ()->getDeclaringClass ()->getName (), $ scope ->getFunction ()->getName ())
49
- : ($ scope ->getFunction () instanceof FunctionReflection ? $ scope ->getFunction ()->getName () : null )),
51
+ $ scope ->isInAnonymousFunction () ? '{closure} ' : ($ function !== null ? $ function ->getName () : null ),
52
+ $ scope ->isInAnonymousFunction () ? '{closure} ' : ($ function instanceof MethodReflection
53
+ ? sprintf ('%s::%s ' , $ function ->getDeclaringClass ()->getName (), $ function ->getName ())
54
+ : ($ function instanceof FunctionReflection ? $ function ->getName () : null )),
55
+ $ function instanceof PhpMethodFromParserNodeReflection && $ function ->isPropertyHook () ? $ function ->getHookedPropertyName () : null ,
50
56
);
51
57
}
52
58
@@ -81,6 +87,7 @@ public static function fromClass(string $className, ?string $fileName): self
81
87
null ,
82
88
null ,
83
89
null ,
90
+ null ,
84
91
);
85
92
}
86
93
@@ -96,6 +103,7 @@ public static function fromReflectionParameter(ReflectionParameter $parameter):
96
103
null ,
97
104
$ declaringFunction ->getName (),
98
105
$ declaringFunction ->getName (),
106
+ null , // Property hook parameter cannot have a default value. fromReflectionParameter is only used for that
99
107
);
100
108
}
101
109
@@ -110,6 +118,7 @@ public static function fromReflectionParameter(ReflectionParameter $parameter):
110
118
$ betterReflection ->getDeclaringClass ()->isTrait () ? $ betterReflection ->getDeclaringClass ()->getName () : null ,
111
119
$ declaringFunction ->getName (),
112
120
sprintf ('%s::%s ' , $ declaringFunction ->getDeclaringClass ()->getName (), $ declaringFunction ->getName ()),
121
+ null , // Property hook parameter cannot have a default value. fromReflectionParameter is only used for that
113
122
);
114
123
}
115
124
@@ -127,15 +136,36 @@ public static function fromStubParameter(
127
136
$ namespace = self ::parseNamespace ($ function ->namespacedName ->toString ());
128
137
}
129
138
}
139
+
140
+ $ functionName = null ;
141
+ $ propertyName = null ;
142
+ if ($ function instanceof Function_ && $ function ->namespacedName !== null ) {
143
+ $ functionName = $ function ->namespacedName ->toString ();
144
+ } elseif ($ function instanceof ClassMethod) {
145
+ $ functionName = $ function ->name ->toString ();
146
+ } elseif ($ function instanceof PropertyHook) {
147
+ $ propertyName = $ function ->getAttribute (PropertyHookNameVisitor::ATTRIBUTE_NAME );
148
+ $ functionName = sprintf ('$%s::%s ' , $ propertyName , $ function ->name ->toString ());
149
+ }
150
+
151
+ $ methodName = null ;
152
+ if ($ function instanceof ClassMethod && $ className !== null ) {
153
+ $ methodName = sprintf ('%s::%s ' , $ className , $ function ->name ->toString ());
154
+ } elseif ($ function instanceof PropertyHook) {
155
+ $ propertyName = $ function ->getAttribute (PropertyHookNameVisitor::ATTRIBUTE_NAME );
156
+ $ methodName = sprintf ('%s::$%s::%s ' , $ className , $ propertyName , $ function ->name ->toString ());
157
+ } elseif ($ function instanceof Function_ && $ function ->namespacedName !== null ) {
158
+ $ methodName = $ function ->namespacedName ->toString ();
159
+ }
160
+
130
161
return new self (
131
162
$ stubFile ,
132
163
$ namespace ,
133
164
$ className ,
134
165
null ,
135
- $ function instanceof Function_ && $ function ->namespacedName !== null ? $ function ->namespacedName ->toString () : ($ function instanceof ClassMethod ? $ function ->name ->toString () : null ),
136
- $ function instanceof ClassMethod && $ className !== null
137
- ? sprintf ('%s::%s ' , $ className , $ function ->name ->toString ())
138
- : ($ function instanceof Function_ && $ function ->namespacedName !== null ? $ function ->namespacedName ->toString () : null ),
166
+ $ functionName ,
167
+ $ methodName ,
168
+ $ propertyName ,
139
169
);
140
170
}
141
171
@@ -148,12 +178,13 @@ public static function fromGlobalConstant(ReflectionConstant $constant): self
148
178
null ,
149
179
null ,
150
180
null ,
181
+ null ,
151
182
);
152
183
}
153
184
154
185
public static function createEmpty (): self
155
186
{
156
- return new self (null , null , null , null , null , null );
187
+ return new self (null , null , null , null , null , null , null );
157
188
}
158
189
159
190
public function getFile (): ?string
@@ -186,4 +217,9 @@ public function getMethod(): ?string
186
217
return $ this ->method ;
187
218
}
188
219
220
+ public function getProperty (): ?string
221
+ {
222
+ return $ this ->property ;
223
+ }
224
+
189
225
}
0 commit comments