1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Reflection\Php;
4:
5: use PhpParser\Node;
6: use PhpParser\Node\Stmt\ClassMethod;
7: use PHPStan\Parser\PreservedPropertiesVisitor;
8: use PHPStan\PhpDoc\ResolvedPhpDocBlock;
9: use PHPStan\Reflection\Assertions;
10: use PHPStan\Reflection\AttributeReflection;
11: use PHPStan\Reflection\ClassMemberReflection;
12: use PHPStan\Reflection\ClassReflection;
13: use PHPStan\Reflection\ExtendedMethodReflection;
14: use PHPStan\Reflection\MissingMethodFromReflectionException;
15: use PHPStan\ShouldNotHappenException;
16: use PHPStan\TrinaryLogic;
17: use PHPStan\Turbo\ReferencedByTurboExtension;
18: use PHPStan\Type\ArrayType;
19: use PHPStan\Type\BooleanType;
20: use PHPStan\Type\Generic\TemplateTypeMap;
21: use PHPStan\Type\IntegerType;
22: use PHPStan\Type\MixedType;
23: use PHPStan\Type\NullType;
24: use PHPStan\Type\ObjectWithoutClassType;
25: use PHPStan\Type\StringType;
26: use PHPStan\Type\Type;
27: use PHPStan\Type\TypeCombinator;
28: use PHPStan\Type\UnionType;
29: use PHPStan\Type\VoidType;
30: use function in_array;
31: use function sprintf;
32: use function strtolower;
33:
34: /**
35: * @api
36: */
37: #[ReferencedByTurboExtension(key: 'phpMethodFromParserNodeReflection')]
38: final class PhpMethodFromParserNodeReflection extends PhpFunctionFromParserNodeReflection implements ExtendedMethodReflection
39: {
40:
41: /**
42: * @param Type[] $realParameterTypes
43: * @param Type[] $phpDocParameterTypes
44: * @param Type[] $realParameterDefaultValues
45: * @param array<string, list<AttributeReflection>> $parameterAttributes
46: * @param array<string, bool> $immediatelyInvokedCallableParameters
47: * @param array<string, Type> $phpDocClosureThisTypeParameters
48: * @param list<AttributeReflection> $attributes
49: */
50: public function __construct(
51: private ClassReflection $declaringClass,
52: private ClassMethod|Node\PropertyHook $classMethod,
53: private ?string $hookForProperty,
54: string $fileName,
55: TemplateTypeMap $templateTypeMap,
56: array $realParameterTypes,
57: array $phpDocParameterTypes,
58: array $realParameterDefaultValues,
59: array $parameterAttributes,
60: Type $realReturnType,
61: ?Type $phpDocReturnType,
62: ?Type $throwType,
63: ?string $deprecatedDescription,
64: bool $isDeprecated,
65: bool $isInternal,
66: private bool $isFinal,
67: ?bool $isPure,
68: bool $acceptsNamedArguments,
69: Assertions $assertions,
70: private ?Type $selfOutType,
71: ?string $phpDocComment,
72: private ?ResolvedPhpDocBlock $resolvedPhpDoc,
73: array $parameterOutTypes,
74: array $immediatelyInvokedCallableParameters,
75: array $phpDocClosureThisTypeParameters,
76: private bool $isConstructor,
77: array $attributes,
78: array $pureUnlessCallableIsImpureParameters,
79: array $pureUnlessParameterPassedParameters,
80: )
81: {
82: if ($this->classMethod instanceof Node\PropertyHook) {
83: if ($this->hookForProperty === null) {
84: throw new ShouldNotHappenException('Hook was provided but property was not');
85: }
86: } elseif ($this->hookForProperty !== null) {
87: throw new ShouldNotHappenException('Hooked property was provided but hook was not');
88: }
89:
90: $name = strtolower($classMethod->name->name);
91: if ($this->isConstructor) {
92: $realReturnType = new VoidType();
93: }
94: if (in_array($name, ['__destruct', '__unset', '__wakeup', '__clone'], true)) {
95: $realReturnType = new VoidType();
96: }
97: if ($name === '__tostring') {
98: $realReturnType = new StringType();
99: }
100: if ($name === '__isset') {
101: $realReturnType = new BooleanType();
102: }
103: if ($name === '__sleep') {
104: $realReturnType = new ArrayType(new IntegerType(), new StringType());
105: }
106: if ($name === '__set_state') {
107: $realReturnType = TypeCombinator::intersect(new ObjectWithoutClassType(), $realReturnType);
108: }
109: if ($name === '__set') {
110: $realReturnType = new VoidType();
111: }
112:
113: if ($name === '__debuginfo') {
114: $realReturnType = TypeCombinator::intersect(new UnionType([new ArrayType(new MixedType(true), new MixedType(true)), new NullType()]), $realReturnType);
115: }
116:
117: if ($name === '__unserialize') {
118: $realReturnType = new VoidType();
119: }
120: if ($name === '__serialize') {
121: $realReturnType = new ArrayType(new MixedType(true), new MixedType(true));
122: }
123:
124: parent::__construct(
125: $classMethod,
126: $fileName,
127: $templateTypeMap,
128: $realParameterTypes,
129: $phpDocParameterTypes,
130: $realParameterDefaultValues,
131: $parameterAttributes,
132: $realReturnType,
133: $phpDocReturnType,
134: $throwType,
135: $deprecatedDescription,
136: $isDeprecated,
137: $isInternal,
138: $isPure,
139: $acceptsNamedArguments,
140: $assertions,
141: $phpDocComment,
142: $parameterOutTypes,
143: $immediatelyInvokedCallableParameters,
144: $phpDocClosureThisTypeParameters,
145: $attributes,
146: $pureUnlessCallableIsImpureParameters,
147: $pureUnlessParameterPassedParameters,
148: );
149: }
150:
151: public function getDeclaringClass(): ClassReflection
152: {
153: return $this->declaringClass;
154: }
155:
156: public function getPrototype(): ClassMemberReflection
157: {
158: try {
159: return $this->declaringClass->getNativeMethod($this->getClassMethod()->name->name)->getPrototype();
160: } catch (MissingMethodFromReflectionException) {
161: return $this;
162: }
163: }
164:
165: private function getClassMethod(): ClassMethod|Node\PropertyHook
166: {
167: /** @var Node\Stmt\ClassMethod|Node\PropertyHook $functionLike */
168: $functionLike = $this->getFunctionLike();
169: return $functionLike;
170: }
171:
172: public function getName(): string
173: {
174: $function = $this->getFunctionLike();
175: if (!$function instanceof Node\PropertyHook) {
176: return parent::getName();
177: }
178:
179: if ($this->hookForProperty === null) {
180: throw new ShouldNotHappenException('Hook was provided but property was not');
181: }
182:
183: return sprintf('$%s::%s', $this->hookForProperty, $function->name->toString());
184: }
185:
186: /**
187: * @phpstan-assert-if-true !null $this->getHookedPropertyName()
188: * @phpstan-assert-if-true !null $this->getPropertyHookName()
189: */
190: public function isPropertyHook(): bool
191: {
192: return $this->hookForProperty !== null;
193: }
194:
195: public function getHookedPropertyName(): ?string
196: {
197: return $this->hookForProperty;
198: }
199:
200: /**
201: * @return 'get'|'set'|null
202: */
203: public function getPropertyHookName(): ?string
204: {
205: $function = $this->getFunctionLike();
206: if (!$function instanceof Node\PropertyHook) {
207: return null;
208: }
209:
210: $name = $function->name->toLowerString();
211: if (!in_array($name, ['get', 'set'], true)) {
212: throw new ShouldNotHappenException(sprintf('Unknown property hook: %s', $name));
213: }
214:
215: return $name;
216: }
217:
218: public function isStatic(): bool
219: {
220: $method = $this->getClassMethod();
221: if ($method instanceof Node\PropertyHook) {
222: return false;
223: }
224:
225: return $method->isStatic();
226: }
227:
228: public function isPrivate(): bool
229: {
230: $method = $this->getClassMethod();
231: if ($method instanceof Node\PropertyHook) {
232: return false;
233: }
234:
235: return $method->isPrivate();
236: }
237:
238: public function isPublic(): bool
239: {
240: $method = $this->getClassMethod();
241: if ($method instanceof Node\PropertyHook) {
242: return true;
243: }
244:
245: return $method->isPublic();
246: }
247:
248: public function isFinal(): TrinaryLogic
249: {
250: $method = $this->getClassMethod();
251: if ($method instanceof Node\PropertyHook) {
252: return TrinaryLogic::createFromBoolean($method->isFinal());
253: }
254:
255: return TrinaryLogic::createFromBoolean($method->isFinal() || $this->isFinal);
256: }
257:
258: public function isFinalByKeyword(): TrinaryLogic
259: {
260: return TrinaryLogic::createFromBoolean($this->getClassMethod()->isFinal());
261: }
262:
263: public function isBuiltin(): bool
264: {
265: return false;
266: }
267:
268: public function getSelfOutType(): ?Type
269: {
270: return $this->selfOutType;
271: }
272:
273: public function returnsByReference(): TrinaryLogic
274: {
275: return TrinaryLogic::createFromBoolean($this->getClassMethod()->returnsByRef());
276: }
277:
278: public function isAbstract(): TrinaryLogic
279: {
280: $method = $this->getClassMethod();
281: if ($method instanceof Node\PropertyHook) {
282: return TrinaryLogic::createFromBoolean($method->body === null);
283: }
284:
285: return TrinaryLogic::createFromBoolean($method->isAbstract());
286: }
287:
288: public function isConstructor(): bool
289: {
290: return $this->isConstructor;
291: }
292:
293: /**
294: * Lowercase names of the methods of the class this method is only ever run from,
295: * null when other code can call it too.
296: *
297: * @internal
298: * @return non-empty-list<string>|null
299: */
300: public function getOnlyCalledFrom(): ?array
301: {
302: return $this->getClassMethod()->getAttribute(PreservedPropertiesVisitor::ONLY_CALLED_FROM_ATTRIBUTE_NAME);
303: }
304:
305: public function hasSideEffects(): TrinaryLogic
306: {
307: // a void function that is pure unless its callable is impure has only the callable's effects
308: if (
309: strtolower($this->getName()) !== '__construct'
310: && $this->getReturnType()->isVoid()->yes()
311: && !$this->hasPureUnlessCallableIsImpureParameter()
312: ) {
313: return TrinaryLogic::createYes();
314: }
315: if ($this->isPure !== null) {
316: return TrinaryLogic::createFromBoolean(!$this->isPure);
317: }
318:
319: return TrinaryLogic::createMaybe();
320: }
321:
322: public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock
323: {
324: return $this->resolvedPhpDoc;
325: }
326:
327: }
328: