|
7 | 7 | use PHPStan\Reflection\ClassReflection; |
8 | 8 | use PHPStan\Reflection\MethodReflection; |
9 | 9 | use PHPStan\Reflection\Php\PhpMethodReflection; |
10 | | -use PHPStan\Reflection\Php\PhpPropertyReflection; |
11 | | -use PHPStan\Reflection\PropertyReflection; |
12 | 10 | use PHPStan\Reflection\ResolvedMethodReflection; |
13 | | -use PHPStan\Reflection\ResolvedPropertyReflection; |
14 | 11 | use PHPStan\Type\ConditionalTypeForParameter; |
15 | 12 | use PHPStan\Type\Type; |
16 | 13 | use PHPStan\Type\TypeTraverser; |
@@ -114,74 +111,71 @@ public function transformAssertTagParameterWithParameterNameMapping(AssertTagPar |
114 | 111 | return $parameter; |
115 | 112 | } |
116 | 113 |
|
117 | | - /** |
118 | | - * @param array<int, string> $originalPositionalParameterNames |
119 | | - * @param array<int, string> $newPositionalParameterNames |
120 | | - */ |
121 | 114 | public static function resolvePhpDocBlockForProperty( |
122 | 115 | ?string $docComment, |
123 | 116 | ClassReflection $classReflection, |
124 | 117 | ?string $trait, |
125 | 118 | string $propertyName, |
126 | 119 | ?string $file, |
127 | 120 | ?bool $explicit, |
128 | | - array $originalPositionalParameterNames, // unused |
129 | | - array $newPositionalParameterNames, // unused |
130 | 121 | ): self |
131 | 122 | { |
132 | | - $docBlocksFromParents = self::resolveParentPhpDocBlocks( |
133 | | - self::getParentReflections($classReflection), |
134 | | - $propertyName, |
135 | | - 'hasNativeProperty', |
136 | | - 'getNativeProperty', |
137 | | - __FUNCTION__, |
138 | | - $explicit ?? $docComment !== null, |
139 | | - $newPositionalParameterNames, |
140 | | - ); |
| 123 | + $docBlocksFromParents = []; |
| 124 | + foreach (self::getParentReflections($classReflection) as $parentReflection) { |
| 125 | + $oneResult = self::resolvePropertyPhpDocBlockFromClass( |
| 126 | + $parentReflection, |
| 127 | + $propertyName, |
| 128 | + $explicit ?? $docComment !== null, |
| 129 | + ); |
| 130 | + |
| 131 | + if ($oneResult === null) { // Null if it is private or from a wrong trait. |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + $docBlocksFromParents[] = $oneResult; |
| 136 | + } |
141 | 137 |
|
142 | 138 | return new self( |
143 | 139 | $docComment ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING, |
144 | 140 | $file, |
145 | 141 | $classReflection, |
146 | 142 | $trait, |
147 | 143 | $explicit ?? true, |
148 | | - self::remapParameterNames($originalPositionalParameterNames, $newPositionalParameterNames), |
| 144 | + [], |
149 | 145 | $docBlocksFromParents, |
150 | 146 | ); |
151 | 147 | } |
152 | 148 |
|
153 | | - /** |
154 | | - * @param array<int, string> $originalPositionalParameterNames |
155 | | - * @param array<int, string> $newPositionalParameterNames |
156 | | - */ |
157 | 149 | public static function resolvePhpDocBlockForConstant( |
158 | 150 | ?string $docComment, |
159 | 151 | ClassReflection $classReflection, |
160 | | - ?string $trait, // unused |
161 | 152 | string $constantName, |
162 | 153 | ?string $file, |
163 | 154 | ?bool $explicit, |
164 | | - array $originalPositionalParameterNames, // unused |
165 | | - array $newPositionalParameterNames, // unused |
166 | 155 | ): self |
167 | 156 | { |
168 | | - $docBlocksFromParents = self::resolveParentPhpDocBlocks( |
169 | | - self::getParentReflections($classReflection), |
170 | | - $constantName, |
171 | | - 'hasConstant', |
172 | | - 'getConstant', |
173 | | - __FUNCTION__, |
174 | | - $explicit ?? $docComment !== null, |
175 | | - $newPositionalParameterNames, |
176 | | - ); |
| 157 | + $docBlocksFromParents = []; |
| 158 | + foreach (self::getParentReflections($classReflection) as $parentReflection) { |
| 159 | + $oneResult = self::resolveConstantPhpDocBlockFromClass( |
| 160 | + $parentReflection, |
| 161 | + $constantName, |
| 162 | + $explicit ?? $docComment !== null, |
| 163 | + ); |
| 164 | + |
| 165 | + if ($oneResult === null) { // Null if it is private or from a wrong trait. |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + $docBlocksFromParents[] = $oneResult; |
| 170 | + } |
177 | 171 |
|
178 | 172 | return new self( |
179 | 173 | $docComment ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING, |
180 | 174 | $file, |
181 | 175 | $classReflection, |
182 | | - $trait, |
| 176 | + null, |
183 | 177 | $explicit ?? true, |
184 | | - self::remapParameterNames($originalPositionalParameterNames, $newPositionalParameterNames), |
| 178 | + [], |
185 | 179 | $docBlocksFromParents, |
186 | 180 | ); |
187 | 181 | } |
@@ -219,15 +213,21 @@ public static function resolvePhpDocBlockForMethod( |
219 | 213 | $parentReflections[] = $traitReflection; |
220 | 214 | } |
221 | 215 |
|
222 | | - $docBlocksFromParents = self::resolveParentPhpDocBlocks( |
223 | | - $parentReflections, |
224 | | - $methodName, |
225 | | - 'hasNativeMethod', |
226 | | - 'getNativeMethod', |
227 | | - __FUNCTION__, |
228 | | - $explicit ?? $docComment !== null, |
229 | | - $newPositionalParameterNames, |
230 | | - ); |
| 216 | + $docBlocksFromParents = []; |
| 217 | + foreach ($parentReflections as $parentReflection) { |
| 218 | + $oneResult = self::resolveMethodPhpDocBlockFromClass( |
| 219 | + $parentReflection, |
| 220 | + $methodName, |
| 221 | + $explicit ?? $docComment !== null, |
| 222 | + $newPositionalParameterNames, |
| 223 | + ); |
| 224 | + |
| 225 | + if ($oneResult === null) { // Null if it is private or from a wrong trait. |
| 226 | + continue; |
| 227 | + } |
| 228 | + |
| 229 | + $docBlocksFromParents[] = $oneResult; |
| 230 | + } |
231 | 231 |
|
232 | 232 | return new self( |
233 | 233 | $docComment ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING, |
@@ -262,117 +262,124 @@ private static function remapParameterNames( |
262 | 262 | } |
263 | 263 |
|
264 | 264 | /** |
265 | | - * @param array<int, ClassReflection> $parentReflections |
266 | | - * @param array<int, string> $positionalParameterNames |
267 | | - * @return array<int, self> |
| 265 | + * @return array<int, ClassReflection> |
268 | 266 | */ |
269 | | - private static function resolveParentPhpDocBlocks( |
270 | | - array $parentReflections, |
| 267 | + private static function getParentReflections(ClassReflection $classReflection): array |
| 268 | + { |
| 269 | + $result = []; |
| 270 | + |
| 271 | + $parent = $classReflection->getParentClass(); |
| 272 | + if ($parent !== null) { |
| 273 | + $result[] = $parent; |
| 274 | + } |
| 275 | + |
| 276 | + foreach ($classReflection->getInterfaces() as $interface) { |
| 277 | + $result[] = $interface; |
| 278 | + } |
| 279 | + |
| 280 | + return $result; |
| 281 | + } |
| 282 | + |
| 283 | + private static function resolveConstantPhpDocBlockFromClass( |
| 284 | + ClassReflection $classReflection, |
271 | 285 | string $name, |
272 | | - string $hasMethodName, |
273 | | - string $getMethodName, |
274 | | - string $resolveMethodName, |
275 | 286 | bool $explicit, |
276 | | - array $positionalParameterNames, |
277 | | - ): array |
| 287 | + ): ?self |
278 | 288 | { |
279 | | - $result = []; |
| 289 | + if ($classReflection->hasConstant($name)) { |
| 290 | + $parentReflection = $classReflection->getConstant($name); |
| 291 | + if ($parentReflection->isPrivate()) { |
| 292 | + return null; |
| 293 | + } |
280 | 294 |
|
281 | | - foreach ($parentReflections as $parentReflection) { |
282 | | - $oneResult = self::resolvePhpDocBlockFromClass( |
283 | | - $parentReflection, |
| 295 | + $classReflection = $parentReflection->getDeclaringClass(); |
| 296 | + |
| 297 | + return self::resolvePhpDocBlockForConstant( |
| 298 | + $parentReflection->getDocComment() ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING, |
| 299 | + $classReflection, |
284 | 300 | $name, |
285 | | - $hasMethodName, |
286 | | - $getMethodName, |
287 | | - $resolveMethodName, |
| 301 | + $classReflection->getFileName(), |
288 | 302 | $explicit, |
289 | | - $positionalParameterNames, |
290 | 303 | ); |
291 | | - |
292 | | - if ($oneResult === null) { // Null if it is private or from a wrong trait. |
293 | | - continue; |
294 | | - } |
295 | | - |
296 | | - $result[] = $oneResult; |
297 | 304 | } |
298 | 305 |
|
299 | | - return $result; |
| 306 | + return null; |
300 | 307 | } |
301 | 308 |
|
302 | | - /** |
303 | | - * @return array<int, ClassReflection> |
304 | | - */ |
305 | | - private static function getParentReflections(ClassReflection $classReflection): array |
| 309 | + private static function resolvePropertyPhpDocBlockFromClass( |
| 310 | + ClassReflection $classReflection, |
| 311 | + string $name, |
| 312 | + bool $explicit, |
| 313 | + ): ?self |
306 | 314 | { |
307 | | - $result = []; |
| 315 | + if ($classReflection->hasNativeProperty($name)) { |
| 316 | + $parentReflection = $classReflection->getNativeProperty($name); |
| 317 | + if ($parentReflection->isPrivate()) { |
| 318 | + return null; |
| 319 | + } |
308 | 320 |
|
309 | | - $parent = $classReflection->getParentClass(); |
310 | | - if ($parent !== null) { |
311 | | - $result[] = $parent; |
312 | | - } |
| 321 | + $classReflection = $parentReflection->getDeclaringClass(); |
| 322 | + $traitReflection = $parentReflection->getDeclaringTrait(); |
313 | 323 |
|
314 | | - foreach ($classReflection->getInterfaces() as $interface) { |
315 | | - $result[] = $interface; |
| 324 | + $trait = $traitReflection !== null |
| 325 | + ? $traitReflection->getName() |
| 326 | + : null; |
| 327 | + |
| 328 | + return self::resolvePhpDocBlockForProperty( |
| 329 | + $parentReflection->getDocComment() ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING, |
| 330 | + $classReflection, |
| 331 | + $trait, |
| 332 | + $name, |
| 333 | + $classReflection->getFileName(), |
| 334 | + $explicit, |
| 335 | + ); |
316 | 336 | } |
317 | 337 |
|
318 | | - return $result; |
| 338 | + return null; |
319 | 339 | } |
320 | 340 |
|
321 | 341 | /** |
322 | 342 | * @param array<int, string> $positionalParameterNames |
323 | 343 | */ |
324 | | - private static function resolvePhpDocBlockFromClass( |
| 344 | + private static function resolveMethodPhpDocBlockFromClass( |
325 | 345 | ClassReflection $classReflection, |
326 | 346 | string $name, |
327 | | - string $hasMethodName, |
328 | | - string $getMethodName, |
329 | | - string $resolveMethodName, |
330 | 347 | bool $explicit, |
331 | 348 | array $positionalParameterNames, |
332 | 349 | ): ?self |
333 | 350 | { |
334 | | - if ($classReflection->$hasMethodName($name)) { |
335 | | - /** @var PropertyReflection|MethodReflection|ClassConstantReflection $parentReflection */ |
336 | | - $parentReflection = $classReflection->$getMethodName($name); |
| 351 | + if ($classReflection->hasNativeMethod($name)) { |
| 352 | + $parentReflection = $classReflection->getNativeMethod($name); |
337 | 353 | if ($parentReflection->isPrivate()) { |
338 | 354 | return null; |
339 | 355 | } |
340 | 356 |
|
341 | 357 | $classReflection = $parentReflection->getDeclaringClass(); |
342 | | - |
343 | | - if ($parentReflection instanceof PhpPropertyReflection || $parentReflection instanceof ResolvedPropertyReflection) { |
| 358 | + $traitReflection = null; |
| 359 | + if ($parentReflection instanceof PhpMethodReflection || $parentReflection instanceof ResolvedMethodReflection) { |
344 | 360 | $traitReflection = $parentReflection->getDeclaringTrait(); |
345 | | - $positionalMethodParameterNames = []; |
346 | | - } elseif ($parentReflection instanceof MethodReflection) { |
347 | | - $traitReflection = null; |
348 | | - if ($parentReflection instanceof PhpMethodReflection || $parentReflection instanceof ResolvedMethodReflection) { |
349 | | - $traitReflection = $parentReflection->getDeclaringTrait(); |
350 | | - } |
351 | | - $methodVariants = $parentReflection->getVariants(); |
352 | | - $positionalMethodParameterNames = []; |
353 | | - $lowercaseMethodName = strtolower($parentReflection->getName()); |
354 | | - if ( |
355 | | - count($methodVariants) === 1 |
356 | | - && $lowercaseMethodName !== '__construct' |
357 | | - && $lowercaseMethodName !== strtolower($parentReflection->getDeclaringClass()->getName()) |
358 | | - ) { |
359 | | - $methodParameters = $methodVariants[0]->getParameters(); |
360 | | - foreach ($methodParameters as $methodParameter) { |
361 | | - $positionalMethodParameterNames[] = $methodParameter->getName(); |
362 | | - } |
363 | | - } else { |
364 | | - $positionalMethodParameterNames = $positionalParameterNames; |
| 361 | + } |
| 362 | + $methodVariants = $parentReflection->getVariants(); |
| 363 | + $positionalMethodParameterNames = []; |
| 364 | + $lowercaseMethodName = strtolower($parentReflection->getName()); |
| 365 | + if ( |
| 366 | + count($methodVariants) === 1 |
| 367 | + && $lowercaseMethodName !== '__construct' |
| 368 | + && $lowercaseMethodName !== strtolower($parentReflection->getDeclaringClass()->getName()) |
| 369 | + ) { |
| 370 | + $methodParameters = $methodVariants[0]->getParameters(); |
| 371 | + foreach ($methodParameters as $methodParameter) { |
| 372 | + $positionalMethodParameterNames[] = $methodParameter->getName(); |
365 | 373 | } |
366 | 374 | } else { |
367 | | - $traitReflection = null; |
368 | | - $positionalMethodParameterNames = []; |
| 375 | + $positionalMethodParameterNames = $positionalParameterNames; |
369 | 376 | } |
370 | 377 |
|
371 | 378 | $trait = $traitReflection !== null |
372 | 379 | ? $traitReflection->getName() |
373 | 380 | : null; |
374 | 381 |
|
375 | | - return self::$resolveMethodName( |
| 382 | + return self::resolvePhpDocBlockForMethod( |
376 | 383 | $parentReflection->getDocComment() ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING, |
377 | 384 | $classReflection, |
378 | 385 | $trait, |
|
0 commit comments