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