1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\Reflection; |
6: | |
7: | use PhpParser\Node; |
8: | use PhpParser\Node\Expr\Throw_ as NodeThrow; |
9: | use PhpParser\Node\Expr\Yield_ as YieldNode; |
10: | use PhpParser\Node\Expr\YieldFrom as YieldFromNode; |
11: | use PhpParser\Node\Stmt\ClassMethod as MethodNode; |
12: | use PhpParser\NodeTraverser; |
13: | use PhpParser\NodeVisitor\FindingVisitor; |
14: | use PHPStan\BetterReflection\Reflection\Annotation\AnnotationHelper; |
15: | use PHPStan\BetterReflection\Reflection\Attribute\ReflectionAttributeHelper; |
16: | use PHPStan\BetterReflection\Reflection\Deprecated\DeprecatedHelper; |
17: | use PHPStan\BetterReflection\Reflection\Exception\CodeLocationMissing; |
18: | use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource; |
19: | use PHPStan\BetterReflection\Util\CalculateReflectionColumn; |
20: | use PHPStan\BetterReflection\Util\Exception\NoNodePosition; |
21: | use PHPStan\BetterReflection\Util\GetLastDocComment; |
22: | |
23: | use function array_filter; |
24: | use function array_values; |
25: | use function assert; |
26: | use function count; |
27: | use function is_array; |
28: | |
29: | |
30: | trait ReflectionFunctionAbstract |
31: | { |
32: | |
33: | |
34: | |
35: | |
36: | private string $name; |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | protected array $parameters; |
43: | |
44: | |
45: | private bool $returnsReference; |
46: | |
47: | |
48: | |
49: | protected $returnType; |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | private array $attributes; |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | private $docComment; |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | private $startLine; |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | private $endLine; |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | private $startColumn; |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | private $endColumn; |
86: | |
87: | |
88: | private bool $couldThrow = false; |
89: | |
90: | |
91: | private bool $isClosure = false; |
92: | |
93: | private bool $isGenerator = false; |
94: | |
95: | |
96: | abstract public function __toString(): string; |
97: | |
98: | |
99: | abstract public function getShortName(): string; |
100: | |
101: | |
102: | |
103: | private function fillFromNode($node): void |
104: | { |
105: | $this->parameters = $this->createParameters($node); |
106: | $this->returnsReference = $node->returnsByRef(); |
107: | $this->returnType = $this->createReturnType($node); |
108: | $this->attributes = ReflectionAttributeHelper::createAttributes($this->reflector, $this, $node->attrGroups); |
109: | $this->docComment = GetLastDocComment::forNode($node); |
110: | $this->couldThrow = $this->computeCouldThrow($node); |
111: | |
112: | $startLine = $node->getStartLine(); |
113: | if ($startLine === -1) { |
114: | $startLine = null; |
115: | } |
116: | |
117: | $endLine = $node->getEndLine(); |
118: | if ($endLine === -1) { |
119: | $endLine = null; |
120: | } |
121: | |
122: | |
123: | $this->startLine = $startLine; |
124: | |
125: | $this->endLine = $endLine; |
126: | |
127: | try { |
128: | $this->startColumn = CalculateReflectionColumn::getStartColumn($this->getLocatedSource()->getSource(), $node); |
129: | } catch (NoNodePosition $exception) { |
130: | $this->startColumn = null; |
131: | } |
132: | |
133: | try { |
134: | $this->endColumn = CalculateReflectionColumn::getEndColumn($this->getLocatedSource()->getSource(), $node); |
135: | } catch (NoNodePosition $exception) { |
136: | $this->endColumn = null; |
137: | } |
138: | } |
139: | |
140: | |
141: | |
142: | private function createParameters($node): array |
143: | { |
144: | $parameters = []; |
145: | |
146: | |
147: | $nodeParams = $node->params; |
148: | foreach ($nodeParams as $paramIndex => $paramNode) { |
149: | $parameter = ReflectionParameter::createFromNode( |
150: | $this->reflector, |
151: | $paramNode, |
152: | $this, |
153: | $paramIndex, |
154: | $this->isParameterOptional($nodeParams, $paramIndex), |
155: | ); |
156: | |
157: | $parameters[$parameter->getName()] = $parameter; |
158: | } |
159: | |
160: | return $parameters; |
161: | } |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | public function getName(): string |
170: | { |
171: | $namespace = $this->getNamespaceName(); |
172: | |
173: | if ($namespace === null) { |
174: | return $this->getShortName(); |
175: | } |
176: | |
177: | return $namespace . '\\' . $this->getShortName(); |
178: | } |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | |
186: | public function getNamespaceName(): ?string |
187: | { |
188: | return $this->namespace; |
189: | } |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | public function inNamespace(): bool |
196: | { |
197: | return $this->namespace !== null; |
198: | } |
199: | |
200: | |
201: | |
202: | |
203: | |
204: | |
205: | public function getNumberOfParameters(): int |
206: | { |
207: | return count($this->parameters); |
208: | } |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | public function getNumberOfRequiredParameters(): int |
216: | { |
217: | return count(array_filter( |
218: | $this->parameters, |
219: | static fn (ReflectionParameter $p): bool => ! $p->isOptional(), |
220: | )); |
221: | } |
222: | |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | public function getParameters(): array |
230: | { |
231: | return array_values($this->parameters); |
232: | } |
233: | |
234: | |
235: | private function isParameterOptional(array $parameterNodes, int $parameterIndex): bool |
236: | { |
237: | foreach ($parameterNodes as $otherParameterIndex => $otherParameterNode) { |
238: | if ($otherParameterIndex < $parameterIndex) { |
239: | continue; |
240: | } |
241: | |
242: | |
243: | |
244: | if ($otherParameterNode->default === null && ! $otherParameterNode->variadic) { |
245: | return false; |
246: | } |
247: | } |
248: | |
249: | return true; |
250: | } |
251: | |
252: | |
253: | |
254: | |
255: | |
256: | |
257: | |
258: | public function getParameter(string $parameterName): ?\PHPStan\BetterReflection\Reflection\ReflectionParameter |
259: | { |
260: | return $this->parameters[$parameterName] ?? null; |
261: | } |
262: | |
263: | |
264: | public function getDocComment(): ?string |
265: | { |
266: | return $this->docComment; |
267: | } |
268: | |
269: | |
270: | public function getFileName(): ?string |
271: | { |
272: | return $this->locatedSource->getFileName(); |
273: | } |
274: | |
275: | public function getLocatedSource(): LocatedSource |
276: | { |
277: | return $this->locatedSource; |
278: | } |
279: | |
280: | |
281: | |
282: | |
283: | public function isClosure(): bool |
284: | { |
285: | return $this->isClosure; |
286: | } |
287: | |
288: | public function isDeprecated(): bool |
289: | { |
290: | return DeprecatedHelper::isDeprecated($this); |
291: | } |
292: | |
293: | public function isInternal(): bool |
294: | { |
295: | return $this->locatedSource->isInternal(); |
296: | } |
297: | |
298: | |
299: | |
300: | |
301: | |
302: | public function isUserDefined(): bool |
303: | { |
304: | return ! $this->isInternal(); |
305: | } |
306: | |
307: | |
308: | public function getExtensionName(): ?string |
309: | { |
310: | return $this->locatedSource->getExtensionName(); |
311: | } |
312: | |
313: | |
314: | |
315: | |
316: | public function isVariadic(): bool |
317: | { |
318: | foreach ($this->parameters as $parameter) { |
319: | if ($parameter->isVariadic()) { |
320: | return true; |
321: | } |
322: | } |
323: | |
324: | return false; |
325: | } |
326: | |
327: | |
328: | public function couldThrow(): bool |
329: | { |
330: | return $this->couldThrow; |
331: | } |
332: | |
333: | |
334: | |
335: | |
336: | private function computeCouldThrow($node): bool |
337: | { |
338: | $statements = $node->getStmts(); |
339: | |
340: | if ($statements === null) { |
341: | return false; |
342: | } |
343: | |
344: | $visitor = new FindingVisitor(static fn (Node $node): bool => $node instanceof NodeThrow); |
345: | $traverser = new NodeTraverser($visitor); |
346: | $traverser->traverse($statements); |
347: | |
348: | return $visitor->getFoundNodes() !== []; |
349: | } |
350: | |
351: | |
352: | |
353: | |
354: | |
355: | private function nodeIsOrContainsYield(Node $node): bool |
356: | { |
357: | if ($node instanceof YieldNode) { |
358: | return true; |
359: | } |
360: | |
361: | if ($node instanceof YieldFromNode) { |
362: | return true; |
363: | } |
364: | |
365: | |
366: | foreach ($node->getSubNodeNames() as $nodeName) { |
367: | $nodeProperty = $node->$nodeName; |
368: | |
369: | if ($nodeProperty instanceof Node && $this->nodeIsOrContainsYield($nodeProperty)) { |
370: | return true; |
371: | } |
372: | |
373: | if (! is_array($nodeProperty)) { |
374: | continue; |
375: | } |
376: | |
377: | |
378: | foreach ($nodeProperty as $nodePropertyArrayItem) { |
379: | if ($nodePropertyArrayItem instanceof Node && $this->nodeIsOrContainsYield($nodePropertyArrayItem)) { |
380: | return true; |
381: | } |
382: | } |
383: | } |
384: | |
385: | return false; |
386: | } |
387: | |
388: | |
389: | |
390: | |
391: | |
392: | public function isGenerator(): bool |
393: | { |
394: | return $this->isGenerator; |
395: | } |
396: | |
397: | |
398: | |
399: | |
400: | |
401: | |
402: | |
403: | |
404: | public function getStartLine(): int |
405: | { |
406: | if ($this->startLine === null) { |
407: | throw CodeLocationMissing::create(); |
408: | } |
409: | |
410: | return $this->startLine; |
411: | } |
412: | |
413: | |
414: | |
415: | |
416: | |
417: | |
418: | |
419: | |
420: | public function getEndLine(): int |
421: | { |
422: | if ($this->endLine === null) { |
423: | throw CodeLocationMissing::create(); |
424: | } |
425: | |
426: | return $this->endLine; |
427: | } |
428: | |
429: | |
430: | |
431: | |
432: | |
433: | |
434: | public function getStartColumn(): int |
435: | { |
436: | if ($this->startColumn === null) { |
437: | throw CodeLocationMissing::create(); |
438: | } |
439: | |
440: | return $this->startColumn; |
441: | } |
442: | |
443: | |
444: | |
445: | |
446: | |
447: | |
448: | public function getEndColumn(): int |
449: | { |
450: | if ($this->endColumn === null) { |
451: | throw CodeLocationMissing::create(); |
452: | } |
453: | |
454: | return $this->endColumn; |
455: | } |
456: | |
457: | |
458: | |
459: | |
460: | public function returnsReference(): bool |
461: | { |
462: | return $this->returnsReference; |
463: | } |
464: | |
465: | |
466: | |
467: | |
468: | |
469: | public function getReturnType() |
470: | { |
471: | if ($this->hasTentativeReturnType()) { |
472: | return null; |
473: | } |
474: | |
475: | return $this->returnType; |
476: | } |
477: | |
478: | |
479: | |
480: | |
481: | public function hasReturnType(): bool |
482: | { |
483: | if ($this->hasTentativeReturnType()) { |
484: | return false; |
485: | } |
486: | |
487: | return $this->returnType !== null; |
488: | } |
489: | |
490: | public function hasTentativeReturnType(): bool |
491: | { |
492: | if ($this->isUserDefined()) { |
493: | return false; |
494: | } |
495: | |
496: | return AnnotationHelper::hasTentativeReturnType($this->docComment); |
497: | } |
498: | |
499: | |
500: | |
501: | |
502: | public function getTentativeReturnType() |
503: | { |
504: | if (! $this->hasTentativeReturnType()) { |
505: | return null; |
506: | } |
507: | |
508: | return $this->returnType; |
509: | } |
510: | |
511: | |
512: | |
513: | |
514: | |
515: | private function createReturnType($node) |
516: | { |
517: | $returnType = $node->getReturnType(); |
518: | |
519: | if ($returnType === null) { |
520: | return null; |
521: | } |
522: | |
523: | assert($returnType instanceof Node\Identifier || $returnType instanceof Node\Name || $returnType instanceof Node\NullableType || $returnType instanceof Node\UnionType || $returnType instanceof Node\IntersectionType); |
524: | |
525: | return ReflectionType::createFromNode($this->reflector, $this, $returnType); |
526: | } |
527: | |
528: | |
529: | public function getAttributes(): array |
530: | { |
531: | return $this->attributes; |
532: | } |
533: | |
534: | |
535: | public function getAttributesByName(string $name): array |
536: | { |
537: | return ReflectionAttributeHelper::filterAttributesByName($this->getAttributes(), $name); |
538: | } |
539: | |
540: | |
541: | |
542: | |
543: | |
544: | |
545: | public function getAttributesByInstance(string $className): array |
546: | { |
547: | return ReflectionAttributeHelper::filterAttributesByInstance($this->getAttributes(), $className); |
548: | } |
549: | } |
550: | |