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: /** @psalm-immutable */
24: class ReflectionFunction implements Reflection
25: {
26: use ReflectionFunctionAbstract;
27:
28: public const CLOSURE_NAME = '{closure}';
29:
30: /**
31: * @var bool
32: */
33: private $isStatic;
34: /**
35: * @var \PHPStan\BetterReflection\Reflector\Reflector
36: */
37: private $reflector;
38: /**
39: * @var \PHPStan\BetterReflection\SourceLocator\Located\LocatedSource
40: */
41: private $locatedSource;
42: /**
43: * @var non-empty-string|null
44: */
45: private $namespace = null;
46: /** @param non-empty-string|null $namespace
47: * @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Expr\ArrowFunction $node */
48: private function __construct(Reflector $reflector, $node, LocatedSource $locatedSource, ?string $namespace = null)
49: {
50: $this->reflector = $reflector;
51: $this->locatedSource = $locatedSource;
52: $this->namespace = $namespace;
53: assert($node instanceof Node\Stmt\Function_ || $node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction);
54: $name = $node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction
55: ? self::CLOSURE_NAME
56: : $node->name->name;
57: assert($name !== '');
58: $this->name = $name;
59: $this->fillFromNode($node);
60: $isClosure = $node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction;
61: $this->isStatic = $isClosure && $node->static;
62: $this->isClosure = $isClosure;
63: $this->isGenerator = $this->nodeIsOrContainsYield($node);
64: }
65:
66: /**
67: * @deprecated Use Reflector instead.
68: *
69: * @throws IdentifierNotFound
70: */
71: public static function createFromName(string $functionName): self
72: {
73: return (new BetterReflection())->reflector()->reflectFunction($functionName);
74: }
75:
76: /** @throws IdentifierNotFound */
77: public static function createFromClosure(Closure $closure): self
78: {
79: $configuration = new BetterReflection();
80:
81: return (new DefaultReflector(new AggregateSourceLocator([
82: $configuration->sourceLocator(),
83: new ClosureSourceLocator($closure, $configuration->phpParser()),
84: ])))->reflectFunction(self::CLOSURE_NAME);
85: }
86:
87: /** @return non-empty-string */
88: public function __toString(): string
89: {
90: return ReflectionFunctionStringCast::toString($this);
91: }
92:
93: /**
94: * @internal
95: *
96: * @param non-empty-string|null $namespace
97: * @param \PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Expr\ArrowFunction $node
98: */
99: public static function createFromNode(Reflector $reflector, $node, LocatedSource $locatedSource, ?string $namespace = null): self
100: {
101: return new self($reflector, $node, $locatedSource, $namespace);
102: }
103:
104: /**
105: * Get the "short" name of the function (e.g. for A\B\foo, this will return
106: * "foo").
107: *
108: * @return non-empty-string
109: */
110: public function getShortName(): string
111: {
112: return $this->name;
113: }
114:
115: /**
116: * Check to see if this function has been disabled (by the PHP INI file
117: * directive `disable_functions`).
118: *
119: * Note - we cannot reflect on internal functions (as there is no PHP source
120: * code we can access. This means, at present, we can only EVER return false
121: * from this function, because you cannot disable user-defined functions.
122: *
123: * @see https://php.net/manual/en/ini.core.php#ini.disable-functions
124: *
125: * @todo https://github.com/Roave/BetterReflection/issues/14
126: */
127: public function isDisabled(): bool
128: {
129: return false;
130: }
131:
132: public function isStatic(): bool
133: {
134: return $this->isStatic;
135: }
136:
137: /**
138: * @throws NotImplemented
139: * @throws FunctionDoesNotExist
140: */
141: public function getClosure(): Closure
142: {
143: $this->assertIsNoClosure();
144:
145: $functionName = $this->getName();
146:
147: $this->assertFunctionExist($functionName);
148:
149: return static function (...$args) use ($functionName) {
150: return $functionName(...$args);
151: };
152: }
153:
154: /**
155: * @throws NotImplemented
156: * @throws FunctionDoesNotExist
157: * @param mixed ...$args
158: * @return mixed
159: */
160: public function invoke(...$args)
161: {
162: return $this->invokeArgs($args);
163: }
164:
165: /**
166: * @param array<mixed> $args
167: *
168: * @throws NotImplemented
169: * @throws FunctionDoesNotExist
170: * @return mixed
171: */
172: public function invokeArgs(array $args = [])
173: {
174: $this->assertIsNoClosure();
175:
176: $functionName = $this->getName();
177:
178: $this->assertFunctionExist($functionName);
179:
180: return $functionName(...$args);
181: }
182:
183: /** @throws NotImplemented */
184: private function assertIsNoClosure(): void
185: {
186: if ($this->isClosure()) {
187: throw new NotImplemented('Not implemented for closures');
188: }
189: }
190:
191: /** @throws FunctionDoesNotExist */
192: private function assertFunctionExist(string $functionName): void
193: {
194: if (! function_exists($functionName)) {
195: throw FunctionDoesNotExist::fromName($functionName);
196: }
197: }
198: }
199: