1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\Reflection; |
6: | |
7: | use Closure; |
8: | use Exception; |
9: | use InvalidArgumentException; |
10: | use LogicException; |
11: | use OutOfBoundsException; |
12: | use PhpParser\Node; |
13: | use PhpParser\Node\Param as ParamNode; |
14: | use PHPStan\BetterReflection\NodeCompiler\CompiledValue; |
15: | use PHPStan\BetterReflection\NodeCompiler\CompileNodeToValue; |
16: | use PHPStan\BetterReflection\NodeCompiler\CompilerContext; |
17: | use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode; |
18: | use PHPStan\BetterReflection\Reflection\Attribute\ReflectionAttributeHelper; |
19: | use PHPStan\BetterReflection\Reflection\Exception\CodeLocationMissing; |
20: | use PHPStan\BetterReflection\Reflection\StringCast\ReflectionParameterStringCast; |
21: | use PHPStan\BetterReflection\Reflector\Reflector; |
22: | use PHPStan\BetterReflection\Util\CalculateReflectionColumn; |
23: | use PHPStan\BetterReflection\Util\Exception\NoNodePosition; |
24: | |
25: | use function array_map; |
26: | use function assert; |
27: | use function count; |
28: | use function is_array; |
29: | use function is_object; |
30: | use function is_string; |
31: | use function sprintf; |
32: | |
33: | |
34: | class ReflectionParameter |
35: | { |
36: | |
37: | private $name; |
38: | |
39: | |
40: | |
41: | |
42: | private $default; |
43: | |
44: | |
45: | |
46: | |
47: | private $type; |
48: | |
49: | |
50: | |
51: | |
52: | private $isVariadic; |
53: | |
54: | |
55: | |
56: | |
57: | private $byRef; |
58: | |
59: | |
60: | |
61: | |
62: | private $isPromoted; |
63: | |
64: | |
65: | private $attributes; |
66: | |
67: | |
68: | private $startLine; |
69: | |
70: | |
71: | private $endLine; |
72: | |
73: | |
74: | private $startColumn; |
75: | |
76: | |
77: | private $endColumn; |
78: | |
79: | |
80: | |
81: | private $compiledDefaultValue = null; |
82: | |
83: | |
84: | |
85: | private $reflector; |
86: | |
87: | |
88: | |
89: | private $function; |
90: | |
91: | |
92: | |
93: | private $parameterIndex; |
94: | |
95: | |
96: | |
97: | private $isOptional; |
98: | |
99: | |
100: | |
101: | private function __construct(Reflector $reflector, ParamNode $node, $function, int $parameterIndex, bool $isOptional) |
102: | { |
103: | $this->reflector = $reflector; |
104: | $this->function = $function; |
105: | $this->parameterIndex = $parameterIndex; |
106: | $this->isOptional = $isOptional; |
107: | assert($node->var instanceof Node\Expr\Variable); |
108: | assert(is_string($node->var->name)); |
109: | $name = $node->var->name; |
110: | assert($name !== ''); |
111: | $this->name = $name; |
112: | $this->default = $node->default; |
113: | $this->isPromoted = $node->flags !== 0; |
114: | $this->type = $this->createType($node); |
115: | $this->isVariadic = $node->variadic; |
116: | $this->byRef = $node->byRef; |
117: | $this->attributes = ReflectionAttributeHelper::createAttributes($reflector, $this, $node->attrGroups); |
118: | if ($node->hasAttribute('startLine')) { |
119: | $startLine = $node->getStartLine(); |
120: | assert($startLine > 0); |
121: | } else { |
122: | $startLine = null; |
123: | } |
124: | if ($node->hasAttribute('endLine')) { |
125: | $endLine = $node->getEndLine(); |
126: | assert($endLine > 0); |
127: | } else { |
128: | $endLine = null; |
129: | } |
130: | $this->startLine = $startLine; |
131: | $this->endLine = $endLine; |
132: | try { |
133: | $this->startColumn = CalculateReflectionColumn::getStartColumn($function->getLocatedSource()->getSource(), $node); |
134: | } catch (NoNodePosition $exception) { |
135: | $this->startColumn = null; |
136: | } |
137: | try { |
138: | $this->endColumn = CalculateReflectionColumn::getEndColumn($function->getLocatedSource()->getSource(), $node); |
139: | } catch (NoNodePosition $exception) { |
140: | $this->endColumn = null; |
141: | } |
142: | } |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | public static function createFromClassNameAndMethod(string $className, string $methodName, string $parameterName): self |
152: | { |
153: | $parameter = ($getMethod = ReflectionClass::createFromName($className) |
154: | ->getMethod($methodName)) ? $getMethod->getParameter($parameterName) : null; |
155: | if ($parameter === null) { |
156: | throw new OutOfBoundsException(sprintf('Could not find parameter: %s', $parameterName)); |
157: | } |
158: | return $parameter; |
159: | } |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | public static function createFromClassInstanceAndMethod(object $instance, string $methodName, string $parameterName): self |
169: | { |
170: | $parameter = ($getMethod = ReflectionClass::createFromInstance($instance) |
171: | ->getMethod($methodName)) ? $getMethod->getParameter($parameterName) : null; |
172: | if ($parameter === null) { |
173: | throw new OutOfBoundsException(sprintf('Could not find parameter: %s', $parameterName)); |
174: | } |
175: | return $parameter; |
176: | } |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | public static function createFromClosure(Closure $closure, string $parameterName): ReflectionParameter |
186: | { |
187: | $parameter = ReflectionFunction::createFromClosure($closure) |
188: | ->getParameter($parameterName); |
189: | |
190: | if ($parameter === null) { |
191: | throw new OutOfBoundsException(sprintf('Could not find parameter: %s', $parameterName)); |
192: | } |
193: | |
194: | return $parameter; |
195: | } |
196: | |
197: | |
198: | |
199: | |
200: | |
201: | |
202: | |
203: | |
204: | |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | public static function createFromSpec($spec, string $parameterName): self |
212: | { |
213: | try { |
214: | if (is_array($spec) && count($spec) === 2 && is_string($spec[1])) { |
215: | assert($spec[1] !== ''); |
216: | |
217: | if (is_object($spec[0])) { |
218: | return self::createFromClassInstanceAndMethod($spec[0], $spec[1], $parameterName); |
219: | } |
220: | |
221: | return self::createFromClassNameAndMethod($spec[0], $spec[1], $parameterName); |
222: | } |
223: | |
224: | if (is_string($spec)) { |
225: | $parameter = ReflectionFunction::createFromName($spec)->getParameter($parameterName); |
226: | if ($parameter === null) { |
227: | throw new OutOfBoundsException(sprintf('Could not find parameter: %s', $parameterName)); |
228: | } |
229: | |
230: | return $parameter; |
231: | } |
232: | |
233: | if ($spec instanceof Closure) { |
234: | return self::createFromClosure($spec, $parameterName); |
235: | } |
236: | } catch (OutOfBoundsException $e) { |
237: | throw new InvalidArgumentException('Could not create reflection from the spec given', 0, $e); |
238: | } |
239: | |
240: | throw new InvalidArgumentException('Could not create reflection from the spec given'); |
241: | } |
242: | |
243: | |
244: | public function __toString(): string |
245: | { |
246: | return ReflectionParameterStringCast::toString($this); |
247: | } |
248: | |
249: | |
250: | |
251: | |
252: | |
253: | |
254: | |
255: | public static function createFromNode(Reflector $reflector, ParamNode $node, $function, int $parameterIndex, bool $isOptional): self |
256: | { |
257: | return new self($reflector, $node, $function, $parameterIndex, $isOptional); |
258: | } |
259: | |
260: | |
261: | |
262: | public function withFunction($function): self |
263: | { |
264: | $clone = clone $this; |
265: | $clone->function = $function; |
266: | |
267: | if ($clone->type !== null) { |
268: | $clone->type = $clone->type->withOwner($clone); |
269: | } |
270: | |
271: | $clone->attributes = array_map(static function (ReflectionAttribute $attribute) use ($clone) : ReflectionAttribute { |
272: | return $attribute->withOwner($clone); |
273: | }, $this->attributes); |
274: | |
275: | $this->compiledDefaultValue = null; |
276: | |
277: | return $clone; |
278: | } |
279: | |
280: | |
281: | private function getCompiledDefaultValue(): CompiledValue |
282: | { |
283: | if (! $this->isDefaultValueAvailable()) { |
284: | throw new LogicException('This parameter does not have a default value available'); |
285: | } |
286: | |
287: | if ($this->compiledDefaultValue === null) { |
288: | $this->compiledDefaultValue = (new CompileNodeToValue())->__invoke($this->default, new CompilerContext($this->reflector, $this)); |
289: | } |
290: | |
291: | return $this->compiledDefaultValue; |
292: | } |
293: | |
294: | |
295: | |
296: | |
297: | |
298: | |
299: | public function getName(): string |
300: | { |
301: | return $this->name; |
302: | } |
303: | |
304: | |
305: | |
306: | |
307: | |
308: | public function getDeclaringFunction() |
309: | { |
310: | return $this->function; |
311: | } |
312: | |
313: | |
314: | |
315: | |
316: | |
317: | |
318: | |
319: | public function getDeclaringClass(): ?\PHPStan\BetterReflection\Reflection\ReflectionClass |
320: | { |
321: | if ($this->function instanceof ReflectionMethod) { |
322: | return $this->function->getDeclaringClass(); |
323: | } |
324: | |
325: | return null; |
326: | } |
327: | |
328: | public function getImplementingClass(): ?\PHPStan\BetterReflection\Reflection\ReflectionClass |
329: | { |
330: | if ($this->function instanceof ReflectionMethod) { |
331: | return $this->function->getImplementingClass(); |
332: | } |
333: | |
334: | return null; |
335: | } |
336: | |
337: | |
338: | |
339: | |
340: | |
341: | |
342: | |
343: | |
344: | |
345: | |
346: | public function isOptional(): bool |
347: | { |
348: | return $this->isOptional; |
349: | } |
350: | |
351: | |
352: | |
353: | |
354: | |
355: | |
356: | |
357: | |
358: | |
359: | |
360: | |
361: | public function isDefaultValueAvailable(): bool |
362: | { |
363: | return $this->default !== null; |
364: | } |
365: | |
366: | |
367: | |
368: | |
369: | public function getDefaultValueExpr(): ?\PhpParser\Node\Expr |
370: | { |
371: | return $this->getDefaultValueExpression(); |
372: | } |
373: | |
374: | public function getDefaultValueExpression(): ?\PhpParser\Node\Expr |
375: | { |
376: | return $this->default; |
377: | } |
378: | |
379: | |
380: | |
381: | |
382: | |
383: | |
384: | |
385: | |
386: | |
387: | |
388: | public function getDefaultValue() |
389: | { |
390: | |
391: | $value = $this->getCompiledDefaultValue()->value; |
392: | |
393: | return $value; |
394: | } |
395: | |
396: | |
397: | |
398: | |
399: | public function allowsNull(): bool |
400: | { |
401: | $type = $this->getType(); |
402: | |
403: | if ($type === null) { |
404: | return true; |
405: | } |
406: | |
407: | return $type->allowsNull(); |
408: | } |
409: | |
410: | |
411: | |
412: | |
413: | public function getPosition(): int |
414: | { |
415: | return $this->parameterIndex; |
416: | } |
417: | |
418: | |
419: | |
420: | |
421: | |
422: | |
423: | |
424: | |
425: | public function getType() |
426: | { |
427: | return $this->type; |
428: | } |
429: | |
430: | |
431: | |
432: | |
433: | private function createType(ParamNode $node) |
434: | { |
435: | $type = $node->type; |
436: | |
437: | if ($type === null) { |
438: | return null; |
439: | } |
440: | |
441: | assert($type instanceof Node\Identifier || $type instanceof Node\Name || $type instanceof Node\NullableType || $type instanceof Node\UnionType || $type instanceof Node\IntersectionType); |
442: | |
443: | $allowsNull = $this->default instanceof Node\Expr\ConstFetch && $this->default->name->toLowerString() === 'null' && !$this->isPromoted; |
444: | |
445: | return ReflectionType::createFromNode($this->reflector, $this, $type, $allowsNull); |
446: | } |
447: | |
448: | |
449: | |
450: | |
451: | |
452: | |
453: | public function hasType(): bool |
454: | { |
455: | return $this->type !== null; |
456: | } |
457: | |
458: | |
459: | |
460: | |
461: | public function isVariadic(): bool |
462: | { |
463: | return $this->isVariadic; |
464: | } |
465: | |
466: | |
467: | |
468: | |
469: | public function isPassedByReference(): bool |
470: | { |
471: | return $this->byRef; |
472: | } |
473: | |
474: | public function canBePassedByValue(): bool |
475: | { |
476: | return ! $this->isPassedByReference(); |
477: | } |
478: | |
479: | public function isPromoted(): bool |
480: | { |
481: | return $this->isPromoted; |
482: | } |
483: | |
484: | |
485: | public function isDefaultValueConstant(): bool |
486: | { |
487: | return $this->getCompiledDefaultValue()->constantName !== null; |
488: | } |
489: | |
490: | |
491: | public function getDefaultValueConstantName(): string |
492: | { |
493: | $compiledDefaultValue = $this->getCompiledDefaultValue(); |
494: | |
495: | if ($compiledDefaultValue->constantName === null) { |
496: | throw new LogicException('This parameter is not a constant default value, so cannot have a constant name'); |
497: | } |
498: | |
499: | return $compiledDefaultValue->constantName; |
500: | } |
501: | |
502: | |
503: | |
504: | |
505: | |
506: | |
507: | public function getStartLine(): int |
508: | { |
509: | if ($this->startLine === null) { |
510: | throw CodeLocationMissing::create(); |
511: | } |
512: | |
513: | return $this->startLine; |
514: | } |
515: | |
516: | |
517: | |
518: | |
519: | |
520: | |
521: | public function getEndLine(): int |
522: | { |
523: | if ($this->endLine === null) { |
524: | throw CodeLocationMissing::create(); |
525: | } |
526: | |
527: | return $this->endLine; |
528: | } |
529: | |
530: | |
531: | |
532: | |
533: | |
534: | |
535: | public function getStartColumn(): int |
536: | { |
537: | if ($this->startColumn === null) { |
538: | throw CodeLocationMissing::create(); |
539: | } |
540: | |
541: | return $this->startColumn; |
542: | } |
543: | |
544: | |
545: | |
546: | |
547: | |
548: | |
549: | public function getEndColumn(): int |
550: | { |
551: | if ($this->endColumn === null) { |
552: | throw CodeLocationMissing::create(); |
553: | } |
554: | |
555: | return $this->endColumn; |
556: | } |
557: | |
558: | |
559: | public function getAttributes(): array |
560: | { |
561: | return $this->attributes; |
562: | } |
563: | |
564: | |
565: | public function getAttributesByName(string $name): array |
566: | { |
567: | return ReflectionAttributeHelper::filterAttributesByName($this->getAttributes(), $name); |
568: | } |
569: | |
570: | |
571: | |
572: | |
573: | |
574: | |
575: | public function getAttributesByInstance(string $className): array |
576: | { |
577: | return ReflectionAttributeHelper::filterAttributesByInstance($this->getAttributes(), $className); |
578: | } |
579: | } |
580: | |