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