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