1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Reflection\Php; |
4: | |
5: | use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod; |
6: | use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter; |
7: | use PHPStan\DependencyInjection\AutowiredParameter; |
8: | use PHPStan\DependencyInjection\GenerateFactory; |
9: | use PHPStan\Internal\DeprecatedAttributeHelper; |
10: | use PHPStan\Parser\Parser; |
11: | use PHPStan\Parser\VariadicMethodsVisitor; |
12: | use PHPStan\Reflection\Assertions; |
13: | use PHPStan\Reflection\AttributeReflection; |
14: | use PHPStan\Reflection\AttributeReflectionFactory; |
15: | use PHPStan\Reflection\ClassMemberReflection; |
16: | use PHPStan\Reflection\ClassReflection; |
17: | use PHPStan\Reflection\ExtendedFunctionVariant; |
18: | use PHPStan\Reflection\ExtendedMethodReflection; |
19: | use PHPStan\Reflection\ExtendedParameterReflection; |
20: | use PHPStan\Reflection\ExtendedParametersAcceptor; |
21: | use PHPStan\Reflection\InitializerExprContext; |
22: | use PHPStan\Reflection\InitializerExprTypeResolver; |
23: | use PHPStan\Reflection\MethodPrototypeReflection; |
24: | use PHPStan\Reflection\ReflectionProvider; |
25: | use PHPStan\TrinaryLogic; |
26: | use PHPStan\Type\ArrayType; |
27: | use PHPStan\Type\BooleanType; |
28: | use PHPStan\Type\Generic\TemplateTypeMap; |
29: | use PHPStan\Type\IntegerType; |
30: | use PHPStan\Type\MixedType; |
31: | use PHPStan\Type\ObjectWithoutClassType; |
32: | use PHPStan\Type\StringType; |
33: | use PHPStan\Type\ThisType; |
34: | use PHPStan\Type\Type; |
35: | use PHPStan\Type\TypehintHelper; |
36: | use PHPStan\Type\VoidType; |
37: | use ReflectionException; |
38: | use function array_key_exists; |
39: | use function array_map; |
40: | use function count; |
41: | use function explode; |
42: | use function in_array; |
43: | use function is_array; |
44: | use function sprintf; |
45: | use function strtolower; |
46: | use const PHP_VERSION_ID; |
47: | |
48: | |
49: | |
50: | |
51: | #[GenerateFactory(interface: PhpMethodReflectionFactory::class)] |
52: | final class PhpMethodReflection implements ExtendedMethodReflection |
53: | { |
54: | |
55: | |
56: | private ?array $parameters = null; |
57: | |
58: | private ?Type $returnType = null; |
59: | |
60: | private ?Type $nativeReturnType = null; |
61: | |
62: | |
63: | private ?array $variants = null; |
64: | |
65: | private ?bool $containsVariadicCalls = null; |
66: | |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | public function __construct( |
75: | private InitializerExprTypeResolver $initializerExprTypeResolver, |
76: | private ClassReflection $declaringClass, |
77: | private ?ClassReflection $declaringTrait, |
78: | private ReflectionMethod $reflection, |
79: | private ReflectionProvider $reflectionProvider, |
80: | private AttributeReflectionFactory $attributeReflectionFactory, |
81: | #[AutowiredParameter(ref: '@defaultAnalysisParser')] |
82: | private Parser $parser, |
83: | private TemplateTypeMap $templateTypeMap, |
84: | private array $phpDocParameterTypes, |
85: | private ?Type $phpDocReturnType, |
86: | private ?Type $phpDocThrowType, |
87: | private ?string $deprecatedDescription, |
88: | private bool $isDeprecated, |
89: | private bool $isInternal, |
90: | private bool $isFinal, |
91: | private ?bool $isPure, |
92: | private Assertions $asserts, |
93: | private bool $acceptsNamedArguments, |
94: | private ?Type $selfOutType, |
95: | private ?string $phpDocComment, |
96: | private array $phpDocParameterOutTypes, |
97: | private array $immediatelyInvokedCallableParameters, |
98: | private array $phpDocClosureThisTypeParameters, |
99: | private array $attributes, |
100: | ) |
101: | { |
102: | } |
103: | |
104: | public function getDeclaringClass(): ClassReflection |
105: | { |
106: | return $this->declaringClass; |
107: | } |
108: | |
109: | public function getDeclaringTrait(): ?ClassReflection |
110: | { |
111: | return $this->declaringTrait; |
112: | } |
113: | |
114: | |
115: | |
116: | |
117: | public function getPrototype(): ClassMemberReflection |
118: | { |
119: | try { |
120: | $prototypeMethod = $this->reflection->getPrototype(); |
121: | $prototypeDeclaringClass = $this->declaringClass->getAncestorWithClassName($prototypeMethod->getDeclaringClass()->getName()); |
122: | if ($prototypeDeclaringClass === null) { |
123: | $prototypeDeclaringClass = $this->reflectionProvider->getClass($prototypeMethod->getDeclaringClass()->getName()); |
124: | } |
125: | |
126: | if (!$prototypeDeclaringClass->hasNativeMethod($prototypeMethod->getName())) { |
127: | return $this; |
128: | } |
129: | |
130: | $tentativeReturnType = null; |
131: | if ($prototypeMethod->getTentativeReturnType() !== null) { |
132: | $tentativeReturnType = TypehintHelper::decideTypeFromReflection($prototypeMethod->getTentativeReturnType(), selfClass: $prototypeDeclaringClass); |
133: | } |
134: | |
135: | return new MethodPrototypeReflection( |
136: | $prototypeMethod->getName(), |
137: | $prototypeDeclaringClass, |
138: | $prototypeMethod->isStatic(), |
139: | $prototypeMethod->isPrivate(), |
140: | $prototypeMethod->isPublic(), |
141: | $prototypeMethod->isAbstract(), |
142: | $prototypeMethod->isInternal(), |
143: | $prototypeDeclaringClass->getNativeMethod($prototypeMethod->getName())->getVariants(), |
144: | $tentativeReturnType, |
145: | ); |
146: | } catch (ReflectionException) { |
147: | return $this; |
148: | } |
149: | } |
150: | |
151: | public function isStatic(): bool |
152: | { |
153: | return $this->reflection->isStatic(); |
154: | } |
155: | |
156: | public function getName(): string |
157: | { |
158: | $name = $this->reflection->getName(); |
159: | $lowercaseName = strtolower($name); |
160: | if ($lowercaseName === $name) { |
161: | if (PHP_VERSION_ID >= 80000) { |
162: | return $name; |
163: | } |
164: | |
165: | |
166: | foreach ($this->getDeclaringClass()->getNativeReflection()->getTraitAliases() as $traitTarget) { |
167: | $correctName = $this->getMethodNameWithCorrectCase($name, $traitTarget); |
168: | if ($correctName !== null) { |
169: | $name = $correctName; |
170: | break; |
171: | } |
172: | } |
173: | } |
174: | |
175: | return $name; |
176: | } |
177: | |
178: | private function getMethodNameWithCorrectCase(string $lowercaseMethodName, string $traitTarget): ?string |
179: | { |
180: | $trait = explode('::', $traitTarget)[0]; |
181: | $traitReflection = $this->reflectionProvider->getClass($trait)->getNativeReflection(); |
182: | foreach ($traitReflection->getTraitAliases() as $methodAlias => $aliasTraitTarget) { |
183: | if ($lowercaseMethodName === strtolower($methodAlias)) { |
184: | return $methodAlias; |
185: | } |
186: | |
187: | $correctName = $this->getMethodNameWithCorrectCase($lowercaseMethodName, $aliasTraitTarget); |
188: | if ($correctName !== null) { |
189: | return $correctName; |
190: | } |
191: | } |
192: | |
193: | return null; |
194: | } |
195: | |
196: | |
197: | |
198: | |
199: | public function getVariants(): array |
200: | { |
201: | return $this->variants ??= [ |
202: | new ExtendedFunctionVariant( |
203: | $this->templateTypeMap, |
204: | null, |
205: | $this->getParameters(), |
206: | $this->isVariadic(), |
207: | $this->getReturnType(), |
208: | $this->getPhpDocReturnType(), |
209: | $this->getNativeReturnType(), |
210: | ), |
211: | ]; |
212: | } |
213: | |
214: | public function getOnlyVariant(): ExtendedParametersAcceptor |
215: | { |
216: | return $this->getVariants()[0]; |
217: | } |
218: | |
219: | public function getNamedArgumentsVariants(): ?array |
220: | { |
221: | return null; |
222: | } |
223: | |
224: | |
225: | |
226: | |
227: | private function getParameters(): array |
228: | { |
229: | return $this->parameters ??= array_map(fn (ReflectionParameter $reflection): PhpParameterReflection => new PhpParameterReflection( |
230: | $this->initializerExprTypeResolver, |
231: | $reflection, |
232: | $this->phpDocParameterTypes[$reflection->getName()] ?? null, |
233: | $this->getDeclaringClass(), |
234: | $this->phpDocParameterOutTypes[$reflection->getName()] ?? null, |
235: | $this->immediatelyInvokedCallableParameters[$reflection->getName()] ?? TrinaryLogic::createMaybe(), |
236: | $this->phpDocClosureThisTypeParameters[$reflection->getName()] ?? null, |
237: | $this->attributeReflectionFactory->fromNativeReflection($reflection->getAttributes(), InitializerExprContext::fromReflectionParameter($reflection)), |
238: | ), $this->reflection->getParameters()); |
239: | } |
240: | |
241: | private function isVariadic(): bool |
242: | { |
243: | $isNativelyVariadic = $this->reflection->isVariadic(); |
244: | $declaringClass = $this->declaringClass; |
245: | $filename = $this->declaringClass->getFileName(); |
246: | if ($this->declaringTrait !== null) { |
247: | $declaringClass = $this->declaringTrait; |
248: | $filename = $this->declaringTrait->getFileName(); |
249: | } |
250: | |
251: | if (!$isNativelyVariadic && $filename !== null && !$this->declaringClass->isBuiltin()) { |
252: | if ($this->containsVariadicCalls !== null) { |
253: | return $this->containsVariadicCalls; |
254: | } |
255: | |
256: | $className = $declaringClass->getName(); |
257: | if ($declaringClass->isAnonymous()) { |
258: | $className = sprintf('%s:%s:%s', VariadicMethodsVisitor::ANONYMOUS_CLASS_PREFIX, $declaringClass->getNativeReflection()->getStartLine(), $declaringClass->getNativeReflection()->getEndLine()); |
259: | } |
260: | if (array_key_exists($className, VariadicMethodsVisitor::$cache)) { |
261: | if (array_key_exists($this->reflection->getName(), VariadicMethodsVisitor::$cache[$className])) { |
262: | return $this->containsVariadicCalls = VariadicMethodsVisitor::$cache[$className][$this->reflection->getName()]; |
263: | } |
264: | |
265: | return $this->containsVariadicCalls = false; |
266: | } |
267: | |
268: | $nodes = $this->parser->parseFile($filename); |
269: | if (count($nodes) > 0) { |
270: | $variadicMethods = $nodes[0]->getAttribute(VariadicMethodsVisitor::ATTRIBUTE_NAME); |
271: | |
272: | if ( |
273: | is_array($variadicMethods) |
274: | && array_key_exists($className, $variadicMethods) |
275: | && array_key_exists($this->reflection->getName(), $variadicMethods[$className]) |
276: | ) { |
277: | return $this->containsVariadicCalls = $variadicMethods[$className][$this->reflection->getName()]; |
278: | } |
279: | } |
280: | |
281: | return $this->containsVariadicCalls = false; |
282: | } |
283: | |
284: | return $isNativelyVariadic; |
285: | } |
286: | |
287: | public function isPrivate(): bool |
288: | { |
289: | return $this->reflection->isPrivate(); |
290: | } |
291: | |
292: | public function isPublic(): bool |
293: | { |
294: | return $this->reflection->isPublic(); |
295: | } |
296: | |
297: | private function getReturnType(): Type |
298: | { |
299: | if ($this->returnType === null) { |
300: | $name = strtolower($this->getName()); |
301: | $returnType = $this->reflection->getReturnType(); |
302: | if ($returnType === null) { |
303: | if (in_array($name, ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) { |
304: | return $this->returnType = TypehintHelper::decideType(new VoidType(), $this->phpDocReturnType); |
305: | } |
306: | if ($name === '__tostring') { |
307: | return $this->returnType = TypehintHelper::decideType(new StringType(), $this->phpDocReturnType); |
308: | } |
309: | if ($name === '__isset') { |
310: | return $this->returnType = TypehintHelper::decideType(new BooleanType(), $this->phpDocReturnType); |
311: | } |
312: | if ($name === '__sleep') { |
313: | return $this->returnType = TypehintHelper::decideType(new ArrayType(new IntegerType(), new StringType()), $this->phpDocReturnType); |
314: | } |
315: | if ($name === '__set_state') { |
316: | return $this->returnType = TypehintHelper::decideType(new ObjectWithoutClassType(), $this->phpDocReturnType); |
317: | } |
318: | } |
319: | |
320: | $this->returnType = TypehintHelper::decideTypeFromReflection( |
321: | $returnType, |
322: | $this->phpDocReturnType, |
323: | $this->declaringClass, |
324: | ); |
325: | } |
326: | |
327: | return $this->returnType; |
328: | } |
329: | |
330: | private function getPhpDocReturnType(): Type |
331: | { |
332: | if ($this->phpDocReturnType !== null) { |
333: | return $this->phpDocReturnType; |
334: | } |
335: | |
336: | return new MixedType(); |
337: | } |
338: | |
339: | private function getNativeReturnType(): Type |
340: | { |
341: | return $this->nativeReturnType ??= TypehintHelper::decideTypeFromReflection( |
342: | $this->reflection->getReturnType(), |
343: | selfClass: $this->declaringClass, |
344: | ); |
345: | } |
346: | |
347: | public function getDeprecatedDescription(): ?string |
348: | { |
349: | if ($this->isDeprecated) { |
350: | return $this->deprecatedDescription; |
351: | } |
352: | |
353: | if ($this->reflection->isDeprecated()) { |
354: | $attributes = $this->reflection->getBetterReflection()->getAttributes(); |
355: | return DeprecatedAttributeHelper::getDeprecatedDescription($attributes); |
356: | } |
357: | |
358: | return null; |
359: | } |
360: | |
361: | public function isDeprecated(): TrinaryLogic |
362: | { |
363: | if ($this->isDeprecated) { |
364: | return TrinaryLogic::createYes(); |
365: | } |
366: | |
367: | return TrinaryLogic::createFromBoolean($this->reflection->isDeprecated()); |
368: | } |
369: | |
370: | public function isInternal(): TrinaryLogic |
371: | { |
372: | return TrinaryLogic::createFromBoolean($this->isInternal); |
373: | } |
374: | |
375: | public function isBuiltin(): TrinaryLogic |
376: | { |
377: | return TrinaryLogic::createFromBoolean($this->reflection->isInternal()); |
378: | } |
379: | |
380: | public function isFinal(): TrinaryLogic |
381: | { |
382: | return TrinaryLogic::createFromBoolean($this->isFinal || $this->reflection->isFinal()); |
383: | } |
384: | |
385: | public function isFinalByKeyword(): TrinaryLogic |
386: | { |
387: | return TrinaryLogic::createFromBoolean($this->reflection->isFinal()); |
388: | } |
389: | |
390: | public function isAbstract(): bool |
391: | { |
392: | return $this->reflection->isAbstract(); |
393: | } |
394: | |
395: | public function getThrowType(): ?Type |
396: | { |
397: | return $this->phpDocThrowType; |
398: | } |
399: | |
400: | public function hasSideEffects(): TrinaryLogic |
401: | { |
402: | if ( |
403: | strtolower($this->getName()) !== '__construct' |
404: | && $this->getReturnType()->isVoid()->yes() |
405: | ) { |
406: | return TrinaryLogic::createYes(); |
407: | } |
408: | if ($this->isPure !== null) { |
409: | return TrinaryLogic::createFromBoolean(!$this->isPure); |
410: | } |
411: | |
412: | if ((new ThisType($this->declaringClass))->isSuperTypeOf($this->getReturnType())->yes()) { |
413: | return TrinaryLogic::createYes(); |
414: | } |
415: | |
416: | return TrinaryLogic::createMaybe(); |
417: | } |
418: | |
419: | public function getAsserts(): Assertions |
420: | { |
421: | return $this->asserts; |
422: | } |
423: | |
424: | public function acceptsNamedArguments(): TrinaryLogic |
425: | { |
426: | return TrinaryLogic::createFromBoolean( |
427: | $this->declaringClass->acceptsNamedArguments() && $this->acceptsNamedArguments, |
428: | ); |
429: | } |
430: | |
431: | public function getSelfOutType(): ?Type |
432: | { |
433: | return $this->selfOutType; |
434: | } |
435: | |
436: | public function getDocComment(): ?string |
437: | { |
438: | return $this->phpDocComment; |
439: | } |
440: | |
441: | public function returnsByReference(): TrinaryLogic |
442: | { |
443: | return TrinaryLogic::createFromBoolean($this->reflection->returnsReference()); |
444: | } |
445: | |
446: | public function isPure(): TrinaryLogic |
447: | { |
448: | if ($this->isPure === null) { |
449: | return TrinaryLogic::createMaybe(); |
450: | } |
451: | |
452: | return TrinaryLogic::createFromBoolean($this->isPure); |
453: | } |
454: | |
455: | public function changePropertyGetHookPhpDocType(Type $phpDocType): self |
456: | { |
457: | return new self( |
458: | $this->initializerExprTypeResolver, |
459: | $this->declaringClass, |
460: | $this->declaringTrait, |
461: | $this->reflection, |
462: | $this->reflectionProvider, |
463: | $this->attributeReflectionFactory, |
464: | $this->parser, |
465: | $this->templateTypeMap, |
466: | $this->phpDocParameterTypes, |
467: | $phpDocType, |
468: | $this->phpDocThrowType, |
469: | $this->deprecatedDescription, |
470: | $this->isDeprecated, |
471: | $this->isInternal, |
472: | $this->isFinal, |
473: | $this->isPure, |
474: | $this->asserts, |
475: | $this->acceptsNamedArguments, |
476: | $this->selfOutType, |
477: | $this->phpDocComment, |
478: | $this->phpDocParameterOutTypes, |
479: | $this->immediatelyInvokedCallableParameters, |
480: | $this->phpDocClosureThisTypeParameters, |
481: | $this->attributes, |
482: | ); |
483: | } |
484: | |
485: | public function changePropertySetHookPhpDocType(string $parameterName, Type $phpDocType): self |
486: | { |
487: | $phpDocParameterTypes = $this->phpDocParameterTypes; |
488: | $phpDocParameterTypes[$parameterName] = $phpDocType; |
489: | |
490: | return new self( |
491: | $this->initializerExprTypeResolver, |
492: | $this->declaringClass, |
493: | $this->declaringTrait, |
494: | $this->reflection, |
495: | $this->reflectionProvider, |
496: | $this->attributeReflectionFactory, |
497: | $this->parser, |
498: | $this->templateTypeMap, |
499: | $phpDocParameterTypes, |
500: | $this->phpDocReturnType, |
501: | $this->phpDocThrowType, |
502: | $this->deprecatedDescription, |
503: | $this->isDeprecated, |
504: | $this->isInternal, |
505: | $this->isFinal, |
506: | $this->isPure, |
507: | $this->asserts, |
508: | $this->acceptsNamedArguments, |
509: | $this->selfOutType, |
510: | $this->phpDocComment, |
511: | $this->phpDocParameterOutTypes, |
512: | $this->immediatelyInvokedCallableParameters, |
513: | $this->phpDocClosureThisTypeParameters, |
514: | $this->attributes, |
515: | ); |
516: | } |
517: | |
518: | public function getAttributes(): array |
519: | { |
520: | return $this->attributes; |
521: | } |
522: | |
523: | } |
524: | |