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