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: * @api
50: */
51: #[GenerateFactory(interface: PhpMethodReflectionFactory::class)]
52: final class PhpMethodReflection implements ExtendedMethodReflection
53: {
54:
55: /** @var list<PhpParameterReflection>|null */
56: private ?array $parameters = null;
57:
58: private ?Type $returnType = null;
59:
60: private ?Type $nativeReturnType = null;
61:
62: /** @var list<ExtendedFunctionVariant>|null */
63: private ?array $variants = null;
64:
65: private ?bool $containsVariadicCalls = null;
66:
67: /**
68: * @param Type[] $phpDocParameterTypes
69: * @param Type[] $phpDocParameterOutTypes
70: * @param array<string, TrinaryLogic> $immediatelyInvokedCallableParameters
71: * @param array<string, Type> $phpDocClosureThisTypeParameters
72: * @param list<AttributeReflection> $attributes
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: * @return self|MethodPrototypeReflection
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: // fix for https://bugs.php.net/bug.php?id=74939
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: * @return list<ExtendedParametersAcceptor>
198: */
199: public function getVariants(): array
200: {
201: if ($this->variants === null) {
202: $this->variants = [
203: new ExtendedFunctionVariant(
204: $this->templateTypeMap,
205: null,
206: $this->getParameters(),
207: $this->isVariadic(),
208: $this->getReturnType(),
209: $this->getPhpDocReturnType(),
210: $this->getNativeReturnType(),
211: ),
212: ];
213: }
214:
215: return $this->variants;
216: }
217:
218: public function getOnlyVariant(): ExtendedParametersAcceptor
219: {
220: return $this->getVariants()[0];
221: }
222:
223: public function getNamedArgumentsVariants(): ?array
224: {
225: return null;
226: }
227:
228: /**
229: * @return list<ExtendedParameterReflection>
230: */
231: private function getParameters(): array
232: {
233: if ($this->parameters === null) {
234: $this->parameters = array_map(fn (ReflectionParameter $reflection): PhpParameterReflection => new PhpParameterReflection(
235: $this->initializerExprTypeResolver,
236: $reflection,
237: $this->phpDocParameterTypes[$reflection->getName()] ?? null,
238: $this->getDeclaringClass(),
239: $this->phpDocParameterOutTypes[$reflection->getName()] ?? null,
240: $this->immediatelyInvokedCallableParameters[$reflection->getName()] ?? TrinaryLogic::createMaybe(),
241: $this->phpDocClosureThisTypeParameters[$reflection->getName()] ?? null,
242: $this->attributeReflectionFactory->fromNativeReflection($reflection->getAttributes(), InitializerExprContext::fromReflectionParameter($reflection)),
243: ), $this->reflection->getParameters());
244: }
245:
246: return $this->parameters;
247: }
248:
249: private function isVariadic(): bool
250: {
251: $isNativelyVariadic = $this->reflection->isVariadic();
252: $declaringClass = $this->declaringClass;
253: $filename = $this->declaringClass->getFileName();
254: if ($this->declaringTrait !== null) {
255: $declaringClass = $this->declaringTrait;
256: $filename = $this->declaringTrait->getFileName();
257: }
258:
259: if (!$isNativelyVariadic && $filename !== null && !$this->declaringClass->isBuiltin()) {
260: if ($this->containsVariadicCalls !== null) {
261: return $this->containsVariadicCalls;
262: }
263:
264: $className = $declaringClass->getName();
265: if ($declaringClass->isAnonymous()) {
266: $className = sprintf('%s:%s:%s', VariadicMethodsVisitor::ANONYMOUS_CLASS_PREFIX, $declaringClass->getNativeReflection()->getStartLine(), $declaringClass->getNativeReflection()->getEndLine());
267: }
268: if (array_key_exists($className, VariadicMethodsVisitor::$cache)) {
269: if (array_key_exists($this->reflection->getName(), VariadicMethodsVisitor::$cache[$className])) {
270: return $this->containsVariadicCalls = VariadicMethodsVisitor::$cache[$className][$this->reflection->getName()];
271: }
272:
273: return $this->containsVariadicCalls = false;
274: }
275:
276: $nodes = $this->parser->parseFile($filename);
277: if (count($nodes) > 0) {
278: $variadicMethods = $nodes[0]->getAttribute(VariadicMethodsVisitor::ATTRIBUTE_NAME);
279:
280: if (
281: is_array($variadicMethods)
282: && array_key_exists($className, $variadicMethods)
283: && array_key_exists($this->reflection->getName(), $variadicMethods[$className])
284: ) {
285: return $this->containsVariadicCalls = $variadicMethods[$className][$this->reflection->getName()];
286: }
287: }
288:
289: return $this->containsVariadicCalls = false;
290: }
291:
292: return $isNativelyVariadic;
293: }
294:
295: public function isPrivate(): bool
296: {
297: return $this->reflection->isPrivate();
298: }
299:
300: public function isPublic(): bool
301: {
302: return $this->reflection->isPublic();
303: }
304:
305: private function getReturnType(): Type
306: {
307: if ($this->returnType === null) {
308: $name = strtolower($this->getName());
309: $returnType = $this->reflection->getReturnType();
310: if ($returnType === null) {
311: if (in_array($name, ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
312: return $this->returnType = TypehintHelper::decideType(new VoidType(), $this->phpDocReturnType);
313: }
314: if ($name === '__tostring') {
315: return $this->returnType = TypehintHelper::decideType(new StringType(), $this->phpDocReturnType);
316: }
317: if ($name === '__isset') {
318: return $this->returnType = TypehintHelper::decideType(new BooleanType(), $this->phpDocReturnType);
319: }
320: if ($name === '__sleep') {
321: return $this->returnType = TypehintHelper::decideType(new ArrayType(new IntegerType(), new StringType()), $this->phpDocReturnType);
322: }
323: if ($name === '__set_state') {
324: return $this->returnType = TypehintHelper::decideType(new ObjectWithoutClassType(), $this->phpDocReturnType);
325: }
326: }
327:
328: $this->returnType = TypehintHelper::decideTypeFromReflection(
329: $returnType,
330: $this->phpDocReturnType,
331: $this->declaringClass,
332: );
333: }
334:
335: return $this->returnType;
336: }
337:
338: private function getPhpDocReturnType(): Type
339: {
340: if ($this->phpDocReturnType !== null) {
341: return $this->phpDocReturnType;
342: }
343:
344: return new MixedType();
345: }
346:
347: private function getNativeReturnType(): Type
348: {
349: if ($this->nativeReturnType === null) {
350: $this->nativeReturnType = TypehintHelper::decideTypeFromReflection(
351: $this->reflection->getReturnType(),
352: selfClass: $this->declaringClass,
353: );
354: }
355:
356: return $this->nativeReturnType;
357: }
358:
359: public function getDeprecatedDescription(): ?string
360: {
361: if ($this->isDeprecated) {
362: return $this->deprecatedDescription;
363: }
364:
365: if ($this->reflection->isDeprecated()) {
366: $attributes = $this->reflection->getBetterReflection()->getAttributes();
367: return DeprecatedAttributeHelper::getDeprecatedDescription($attributes);
368: }
369:
370: return null;
371: }
372:
373: public function isDeprecated(): TrinaryLogic
374: {
375: if ($this->isDeprecated) {
376: return TrinaryLogic::createYes();
377: }
378:
379: return TrinaryLogic::createFromBoolean($this->reflection->isDeprecated());
380: }
381:
382: public function isInternal(): TrinaryLogic
383: {
384: return TrinaryLogic::createFromBoolean($this->isInternal);
385: }
386:
387: public function isBuiltin(): TrinaryLogic
388: {
389: return TrinaryLogic::createFromBoolean($this->reflection->isInternal());
390: }
391:
392: public function isFinal(): TrinaryLogic
393: {
394: return TrinaryLogic::createFromBoolean($this->isFinal || $this->reflection->isFinal());
395: }
396:
397: public function isFinalByKeyword(): TrinaryLogic
398: {
399: return TrinaryLogic::createFromBoolean($this->reflection->isFinal());
400: }
401:
402: public function isAbstract(): bool
403: {
404: return $this->reflection->isAbstract();
405: }
406:
407: public function getThrowType(): ?Type
408: {
409: return $this->phpDocThrowType;
410: }
411:
412: public function hasSideEffects(): TrinaryLogic
413: {
414: if (
415: strtolower($this->getName()) !== '__construct'
416: && $this->getReturnType()->isVoid()->yes()
417: ) {
418: return TrinaryLogic::createYes();
419: }
420: if ($this->isPure !== null) {
421: return TrinaryLogic::createFromBoolean(!$this->isPure);
422: }
423:
424: if ((new ThisType($this->declaringClass))->isSuperTypeOf($this->getReturnType())->yes()) {
425: return TrinaryLogic::createYes();
426: }
427:
428: return TrinaryLogic::createMaybe();
429: }
430:
431: public function getAsserts(): Assertions
432: {
433: return $this->asserts;
434: }
435:
436: public function acceptsNamedArguments(): TrinaryLogic
437: {
438: return TrinaryLogic::createFromBoolean(
439: $this->declaringClass->acceptsNamedArguments() && $this->acceptsNamedArguments,
440: );
441: }
442:
443: public function getSelfOutType(): ?Type
444: {
445: return $this->selfOutType;
446: }
447:
448: public function getDocComment(): ?string
449: {
450: return $this->phpDocComment;
451: }
452:
453: public function returnsByReference(): TrinaryLogic
454: {
455: return TrinaryLogic::createFromBoolean($this->reflection->returnsReference());
456: }
457:
458: public function isPure(): TrinaryLogic
459: {
460: if ($this->isPure === null) {
461: return TrinaryLogic::createMaybe();
462: }
463:
464: return TrinaryLogic::createFromBoolean($this->isPure);
465: }
466:
467: public function changePropertyGetHookPhpDocType(Type $phpDocType): self
468: {
469: return new self(
470: $this->initializerExprTypeResolver,
471: $this->declaringClass,
472: $this->declaringTrait,
473: $this->reflection,
474: $this->reflectionProvider,
475: $this->attributeReflectionFactory,
476: $this->parser,
477: $this->templateTypeMap,
478: $this->phpDocParameterTypes,
479: $phpDocType,
480: $this->phpDocThrowType,
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: );
495: }
496:
497: public function changePropertySetHookPhpDocType(string $parameterName, Type $phpDocType): self
498: {
499: $phpDocParameterTypes = $this->phpDocParameterTypes;
500: $phpDocParameterTypes[$parameterName] = $phpDocType;
501:
502: return new self(
503: $this->initializerExprTypeResolver,
504: $this->declaringClass,
505: $this->declaringTrait,
506: $this->reflection,
507: $this->reflectionProvider,
508: $this->attributeReflectionFactory,
509: $this->parser,
510: $this->templateTypeMap,
511: $phpDocParameterTypes,
512: $this->phpDocReturnType,
513: $this->phpDocThrowType,
514: $this->deprecatedDescription,
515: $this->isDeprecated,
516: $this->isInternal,
517: $this->isFinal,
518: $this->isPure,
519: $this->asserts,
520: $this->acceptsNamedArguments,
521: $this->selfOutType,
522: $this->phpDocComment,
523: $this->phpDocParameterOutTypes,
524: $this->immediatelyInvokedCallableParameters,
525: $this->phpDocClosureThisTypeParameters,
526: $this->attributes,
527: );
528: }
529:
530: public function getAttributes(): array
531: {
532: return $this->attributes;
533: }
534:
535: }
536: