1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Reflection\Php;
4:
5: use PhpParser\Node;
6: use PhpParser\Node\Expr\Variable;
7: use PhpParser\Node\FunctionLike;
8: use PhpParser\Node\Stmt\ClassMethod;
9: use PhpParser\Node\Stmt\Function_;
10: use PHPStan\Node\NodeScanner;
11: use PHPStan\Reflection\Assertions;
12: use PHPStan\Reflection\AttributeReflection;
13: use PHPStan\Reflection\ExtendedFunctionVariant;
14: use PHPStan\Reflection\ExtendedParameterReflection;
15: use PHPStan\Reflection\ExtendedParametersAcceptor;
16: use PHPStan\Reflection\FunctionReflection;
17: use PHPStan\Reflection\PassedByReference;
18: use PHPStan\Reflection\SideEffects;
19: use PHPStan\Reflection\SideEffectsFactory;
20: use PHPStan\ShouldNotHappenException;
21: use PHPStan\TrinaryLogic;
22: use PHPStan\Turbo\ReferencedByTurboExtension;
23: use PHPStan\Type\Generic\TemplateTypeMap;
24: use PHPStan\Type\Generic\TemplateTypeVarianceMap;
25: use PHPStan\Type\MixedType;
26: use PHPStan\Type\Type;
27: use PHPStan\Type\TypehintHelper;
28: use function array_map;
29: use function array_reverse;
30: use function in_array;
31: use function is_string;
32: use function strtolower;
33:
34: /**
35: * @api
36: */
37: #[ReferencedByTurboExtension(key: 'phpFunctionFromParserNodeReflection')]
38: class PhpFunctionFromParserNodeReflection implements FunctionReflection, ExtendedParametersAcceptor
39: {
40:
41: /** @var Function_|ClassMethod|Node\PropertyHook */
42: private Node\FunctionLike $functionLike;
43:
44: /** @var list<ExtendedFunctionVariant>|null */
45: private ?array $variants = null;
46:
47: /**
48: * @param Function_|ClassMethod|Node\PropertyHook $functionLike
49: * @param Type[] $realParameterTypes
50: * @param Type[] $phpDocParameterTypes
51: * @param Type[] $realParameterDefaultValues
52: * @param array<string, list<AttributeReflection>> $parameterAttributes
53: * @param Type[] $parameterOutTypes
54: * @param array<string, bool> $immediatelyInvokedCallableParameters
55: * @param array<string, Type> $phpDocClosureThisTypeParameters
56: * @param list<AttributeReflection> $attributes
57: * @param array<string, bool> $pureUnlessCallableIsImpureParameters
58: * @param array<string, bool> $pureUnlessParameterPassedParameters
59: */
60: public function __construct(
61: FunctionLike $functionLike,
62: private string $fileName,
63: private TemplateTypeMap $templateTypeMap,
64: private array $realParameterTypes,
65: private array $phpDocParameterTypes,
66: private array $realParameterDefaultValues,
67: private array $parameterAttributes,
68: private Type $realReturnType,
69: private ?Type $phpDocReturnType,
70: private ?Type $throwType,
71: private ?string $deprecatedDescription,
72: private bool $isDeprecated,
73: private bool $isInternal,
74: protected ?bool $isPure,
75: private bool $acceptsNamedArguments,
76: private Assertions $assertions,
77: private ?string $phpDocComment,
78: private array $parameterOutTypes,
79: private array $immediatelyInvokedCallableParameters,
80: private array $phpDocClosureThisTypeParameters,
81: private array $attributes,
82: private array $pureUnlessCallableIsImpureParameters,
83: private array $pureUnlessParameterPassedParameters,
84: )
85: {
86: $this->functionLike = $functionLike;
87: }
88:
89: /**
90: * @phpstan-assert-if-true PhpMethodFromParserNodeReflection $this
91: */
92: public function isMethodOrPropertyHook(): bool
93: {
94: return $this instanceof PhpMethodFromParserNodeReflection;
95: }
96:
97: protected function getFunctionLike(): FunctionLike
98: {
99: return $this->functionLike;
100: }
101:
102: public function getFileName(): string
103: {
104: return $this->fileName;
105: }
106:
107: public function getName(): string
108: {
109: if ($this->functionLike instanceof ClassMethod) {
110: return $this->functionLike->name->name;
111: }
112:
113: if (!$this->functionLike instanceof Function_) {
114: // PropertyHook is handled in PhpMethodFromParserNodeReflection subclass
115: throw new ShouldNotHappenException();
116: }
117:
118: if ($this->functionLike->namespacedName === null) {
119: throw new ShouldNotHappenException();
120: }
121:
122: return (string) $this->functionLike->namespacedName;
123: }
124:
125: public function getVariants(): array
126: {
127: return $this->variants ??= [
128: new ExtendedFunctionVariant(
129: $this->getTemplateTypeMap(),
130: $this->getResolvedTemplateTypeMap(),
131: $this->getParameters(),
132: $this->isVariadic(),
133: $this->getReturnType(),
134: $this->getPhpDocReturnType(),
135: $this->getNativeReturnType(),
136: ),
137: ];
138: }
139:
140: public function getOnlyVariant(): ExtendedParametersAcceptor
141: {
142: return $this;
143: }
144:
145: public function getNamedArgumentsVariants(): ?array
146: {
147: return null;
148: }
149:
150: public function getTemplateTypeMap(): TemplateTypeMap
151: {
152: return $this->templateTypeMap;
153: }
154:
155: public function getResolvedTemplateTypeMap(): TemplateTypeMap
156: {
157: return TemplateTypeMap::createEmpty();
158: }
159:
160: /**
161: * @return list<ExtendedParameterReflection>
162: */
163: public function getParameters(): array
164: {
165: $parameters = [];
166: $isOptional = true;
167:
168: /** @var Node\Param $parameter */
169: foreach (array_reverse($this->functionLike->getParams()) as $parameter) {
170: if ($parameter->default === null && !$parameter->variadic) {
171: $isOptional = false;
172: }
173:
174: if (!$parameter->var instanceof Variable || !is_string($parameter->var->name)) {
175: throw new ShouldNotHappenException();
176: }
177:
178: if (isset($this->immediatelyInvokedCallableParameters[$parameter->var->name])) {
179: $immediatelyInvokedCallable = TrinaryLogic::createFromBoolean($this->immediatelyInvokedCallableParameters[$parameter->var->name]);
180: } else {
181: $immediatelyInvokedCallable = TrinaryLogic::createMaybe();
182: }
183:
184: if (isset($this->phpDocClosureThisTypeParameters[$parameter->var->name])) {
185: $closureThisType = $this->phpDocClosureThisTypeParameters[$parameter->var->name];
186: } else {
187: $closureThisType = null;
188: }
189:
190: $pureUnlessCallableIsImpureParameter = TrinaryLogic::createFromBoolean($this->pureUnlessCallableIsImpureParameters[$parameter->var->name] ?? false);
191: $pureUnlessParameterPassedParameter = TrinaryLogic::createFromBoolean($this->pureUnlessParameterPassedParameters[$parameter->var->name] ?? false);
192:
193: $parameters[] = new PhpParameterFromParserNodeReflection(
194: $parameter->var->name,
195: $isOptional,
196: $this->realParameterTypes[$parameter->var->name],
197: $this->phpDocParameterTypes[$parameter->var->name] ?? null,
198: $parameter->byRef
199: ? PassedByReference::createCreatesNewVariable()
200: : PassedByReference::createNo(),
201: $this->realParameterDefaultValues[$parameter->var->name] ?? null,
202: $parameter->variadic,
203: $this->parameterOutTypes[$parameter->var->name] ?? null,
204: $immediatelyInvokedCallable,
205: $closureThisType,
206: $this->parameterAttributes[$parameter->var->name] ?? [],
207: $pureUnlessCallableIsImpureParameter,
208: $pureUnlessParameterPassedParameter,
209: );
210: }
211:
212: return array_reverse($parameters);
213: }
214:
215: public function isVariadic(): bool
216: {
217: foreach ($this->functionLike->getParams() as $parameter) {
218: if ($parameter->variadic) {
219: return true;
220: }
221: }
222:
223: return false;
224: }
225:
226: public function getReturnType(): Type
227: {
228: return TypehintHelper::decideType($this->realReturnType, $this->phpDocReturnType);
229: }
230:
231: public function getPhpDocReturnType(): Type
232: {
233: return $this->phpDocReturnType ?? new MixedType();
234: }
235:
236: public function getNativeReturnType(): Type
237: {
238: return $this->realReturnType;
239: }
240:
241: public function getCallSiteVarianceMap(): TemplateTypeVarianceMap
242: {
243: return TemplateTypeVarianceMap::createEmpty();
244: }
245:
246: public function getDeprecatedDescription(): ?string
247: {
248: if ($this->isDeprecated) {
249: return $this->deprecatedDescription;
250: }
251:
252: return null;
253: }
254:
255: public function isDeprecated(): TrinaryLogic
256: {
257: return TrinaryLogic::createFromBoolean($this->isDeprecated);
258: }
259:
260: public function isInternal(): TrinaryLogic
261: {
262: return TrinaryLogic::createFromBoolean($this->isInternal);
263: }
264:
265: public function getThrowType(): ?Type
266: {
267: return $this->throwType;
268: }
269:
270: public function hasSideEffects(): TrinaryLogic
271: {
272: // a void function that is pure unless its callable is impure has only the callable's effects
273: if ($this->getReturnType()->isVoid()->yes() && !$this->hasPureUnlessCallableIsImpureParameter()) {
274: return TrinaryLogic::createYes();
275: }
276: if ($this->isPure !== null) {
277: return TrinaryLogic::createFromBoolean(!$this->isPure);
278: }
279:
280: return TrinaryLogic::createMaybe();
281: }
282:
283: public function isBuiltin(): bool
284: {
285: return false;
286: }
287:
288: protected function hasPureUnlessCallableIsImpureParameter(): bool
289: {
290: return in_array(true, $this->pureUnlessCallableIsImpureParameters, true);
291: }
292:
293: public function isGenerator(): bool
294: {
295: // the yield scan walks the whole body; reflections for the same node are
296: // recreated per ask, so the answer is memoized on the AST node itself
297: // and lives and dies with the parser-cached AST
298: $cached = $this->functionLike->getAttribute('phpstanIsGenerator');
299: if ($cached !== null) {
300: return $cached;
301: }
302:
303: $isGenerator = NodeScanner::nodeIsOrContainsYield($this->functionLike);
304: $this->functionLike->setAttribute('phpstanIsGenerator', $isGenerator);
305:
306: return $isGenerator;
307: }
308:
309: public function acceptsNamedArguments(): TrinaryLogic
310: {
311: return TrinaryLogic::createFromBoolean($this->acceptsNamedArguments);
312: }
313:
314: public function getAsserts(): Assertions
315: {
316: return $this->assertions;
317: }
318:
319: public function getDocComment(): ?string
320: {
321: return $this->phpDocComment;
322: }
323:
324: public function returnsByReference(): TrinaryLogic
325: {
326: return TrinaryLogic::createFromBoolean($this->functionLike->returnsByRef());
327: }
328:
329: public function isPure(): TrinaryLogic
330: {
331: if ($this->isPure === null) {
332: return TrinaryLogic::createMaybe();
333: }
334:
335: return TrinaryLogic::createFromBoolean($this->isPure);
336: }
337:
338: /**
339: * @return array<string, TrinaryLogic>
340: */
341: public function getPureUnlessCallableIsImpureParameters(): array
342: {
343: return array_map(static fn (bool $value): TrinaryLogic => TrinaryLogic::createFromBoolean($value), $this->pureUnlessCallableIsImpureParameters);
344: }
345:
346: /**
347: * @return array<string, TrinaryLogic>
348: */
349: public function getPureUnlessParameterPassedParameters(): array
350: {
351: return array_map(static fn (bool $value): TrinaryLogic => TrinaryLogic::createFromBoolean($value), $this->pureUnlessParameterPassedParameters);
352: }
353:
354: public function getSideEffects(): SideEffects
355: {
356: return SideEffectsFactory::createFromReflection($this);
357: }
358:
359: public function getAttributes(): array
360: {
361: return $this->attributes;
362: }
363:
364: public function mustUseReturnValue(): TrinaryLogic
365: {
366: foreach ($this->attributes as $attrib) {
367: if (strtolower($attrib->getName()) === 'nodiscard') {
368: return TrinaryLogic::createYes();
369: }
370: }
371: return TrinaryLogic::createNo();
372: }
373:
374: }
375: