1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\Reflection; |
6: | |
7: | use Closure; |
8: | use PhpParser\Node; |
9: | use PHPStan\BetterReflection\BetterReflection; |
10: | use PHPStan\BetterReflection\Reflection\Adapter\Exception\NotImplemented; |
11: | use PHPStan\BetterReflection\Reflection\Exception\FunctionDoesNotExist; |
12: | use PHPStan\BetterReflection\Reflection\StringCast\ReflectionFunctionStringCast; |
13: | use PHPStan\BetterReflection\Reflector\DefaultReflector; |
14: | use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound; |
15: | use PHPStan\BetterReflection\Reflector\Reflector; |
16: | use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource; |
17: | use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator; |
18: | use PHPStan\BetterReflection\SourceLocator\Type\ClosureSourceLocator; |
19: | |
20: | use function assert; |
21: | use function function_exists; |
22: | |
23: | |
24: | class ReflectionFunction implements Reflection |
25: | { |
26: | private Reflector $reflector; |
27: | private LocatedSource $locatedSource; |
28: | |
29: | |
30: | |
31: | private $namespace = null; |
32: | use ReflectionFunctionAbstract; |
33: | |
34: | public const CLOSURE_NAME = '{closure}'; |
35: | |
36: | private bool $isStatic; |
37: | |
38: | |
39: | |
40: | private function __construct(Reflector $reflector, $node, LocatedSource $locatedSource, ?string $namespace = null) |
41: | { |
42: | $this->reflector = $reflector; |
43: | $this->locatedSource = $locatedSource; |
44: | $this->namespace = $namespace; |
45: | assert($node instanceof Node\Stmt\Function_ || $node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction); |
46: | $this->name = $node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction |
47: | ? self::CLOSURE_NAME |
48: | : $node->name->name; |
49: | $this->fillFromNode($node); |
50: | $isClosure = $node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction; |
51: | $this->isStatic = $isClosure && $node->static; |
52: | $this->isClosure = $isClosure; |
53: | $this->isGenerator = $this->nodeIsOrContainsYield($node); |
54: | } |
55: | |
56: | |
57: | public static function createFromClosure(Closure $closure): self |
58: | { |
59: | $configuration = new BetterReflection(); |
60: | |
61: | return (new DefaultReflector(new AggregateSourceLocator([ |
62: | $configuration->sourceLocator(), |
63: | new ClosureSourceLocator($closure, $configuration->phpParser()), |
64: | ])))->reflectFunction(self::CLOSURE_NAME); |
65: | } |
66: | |
67: | |
68: | public function __toString(): string |
69: | { |
70: | return ReflectionFunctionStringCast::toString($this); |
71: | } |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | public static function createFromNode(Reflector $reflector, $node, LocatedSource $locatedSource, ?string $namespace = null): self |
80: | { |
81: | return new self($reflector, $node, $locatedSource, $namespace); |
82: | } |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | public function getShortName(): string |
91: | { |
92: | return $this->name; |
93: | } |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | public function isDisabled(): bool |
108: | { |
109: | return false; |
110: | } |
111: | |
112: | public function isStatic(): bool |
113: | { |
114: | return $this->isStatic; |
115: | } |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | public function getClosure(): Closure |
122: | { |
123: | $this->assertIsNoClosure(); |
124: | |
125: | $functionName = $this->getName(); |
126: | |
127: | $this->assertFunctionExist($functionName); |
128: | |
129: | return static fn (...$args) => $functionName(...$args); |
130: | } |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | public function invoke(...$args) |
139: | { |
140: | return $this->invokeArgs($args); |
141: | } |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | public function invokeArgs(array $args = []) |
151: | { |
152: | $this->assertIsNoClosure(); |
153: | |
154: | $functionName = $this->getName(); |
155: | |
156: | $this->assertFunctionExist($functionName); |
157: | |
158: | return $functionName(...$args); |
159: | } |
160: | |
161: | |
162: | private function assertIsNoClosure(): void |
163: | { |
164: | if ($this->isClosure()) { |
165: | throw new NotImplemented('Not implemented for closures'); |
166: | } |
167: | } |
168: | |
169: | |
170: | private function assertFunctionExist(string $functionName): void |
171: | { |
172: | if (! function_exists($functionName)) { |
173: | throw FunctionDoesNotExist::fromName($functionName); |
174: | } |
175: | } |
176: | } |
177: | |