1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\SourceLocator\Type; |
6: | |
7: | use InvalidArgumentException; |
8: | use PhpParser\Node; |
9: | use PhpParser\NodeTraverser; |
10: | use PhpParser\NodeVisitor\NameResolver; |
11: | use PhpParser\NodeVisitorAbstract; |
12: | use PhpParser\Parser; |
13: | use ReflectionClass; |
14: | use ReflectionException; |
15: | use ReflectionFunction; |
16: | use PHPStan\BetterReflection\BetterReflection; |
17: | use PHPStan\BetterReflection\Identifier\Identifier; |
18: | use PHPStan\BetterReflection\Reflection\Exception\InvalidConstantNode; |
19: | use PHPStan\BetterReflection\SourceLocator\Ast\Locator as AstLocator; |
20: | use PHPStan\BetterReflection\SourceLocator\Exception\InvalidFileLocation; |
21: | use PHPStan\BetterReflection\SourceLocator\FileChecker; |
22: | use PHPStan\BetterReflection\SourceLocator\Located\AliasLocatedSource; |
23: | use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource; |
24: | use PHPStan\BetterReflection\SourceLocator\Type\AutoloadSourceLocator\FileReadTrapStreamWrapper; |
25: | use PHPStan\BetterReflection\Util\ClassExistenceChecker; |
26: | use PHPStan\BetterReflection\Util\ConstantNodeChecker; |
27: | |
28: | use function array_key_exists; |
29: | use function array_reverse; |
30: | use function assert; |
31: | use function defined; |
32: | use function file_get_contents; |
33: | use function function_exists; |
34: | use function get_defined_constants; |
35: | use function get_included_files; |
36: | use function is_file; |
37: | use function is_string; |
38: | use function restore_error_handler; |
39: | use function set_error_handler; |
40: | use function spl_autoload_functions; |
41: | use function strtolower; |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | class AutoloadSourceLocator extends AbstractSourceLocator |
51: | { |
52: | |
53: | |
54: | |
55: | private $phpParser; |
56: | |
57: | |
58: | |
59: | |
60: | private $nodeTraverser; |
61: | |
62: | |
63: | |
64: | |
65: | private $constantVisitor; |
66: | |
67: | public function __construct($astLocator = null, ?\PhpParser\Parser $phpParser = null) |
68: | { |
69: | $betterReflection = new BetterReflection(); |
70: | |
71: | parent::__construct($astLocator ?? $betterReflection->astLocator()); |
72: | |
73: | $this->phpParser = $phpParser ?? $betterReflection->phpParser(); |
74: | $this->constantVisitor = $this->createConstantVisitor(); |
75: | |
76: | $this->nodeTraverser = new NodeTraverser(); |
77: | $this->nodeTraverser->addVisitor(new NameResolver()); |
78: | $this->nodeTraverser->addVisitor($this->constantVisitor); |
79: | } |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
86: | |
87: | protected function createLocatedSource(Identifier $identifier): ?\PHPStan\BetterReflection\SourceLocator\Located\LocatedSource |
88: | { |
89: | $locatedData = $this->attemptAutoloadForIdentifier($identifier); |
90: | |
91: | if ($locatedData === null) { |
92: | return null; |
93: | } |
94: | |
95: | if (! is_file($locatedData['fileName'])) { |
96: | return null; |
97: | } |
98: | |
99: | if (strtolower($identifier->getName()) !== strtolower($locatedData['name'])) { |
100: | return new AliasLocatedSource(file_get_contents($locatedData['fileName']), $locatedData['name'], $locatedData['fileName'], $identifier->getName()); |
101: | } |
102: | |
103: | return new LocatedSource(file_get_contents($locatedData['fileName']), $identifier->getName(), $locatedData['fileName']); |
104: | } |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | private function attemptAutoloadForIdentifier(Identifier $identifier): ?array |
114: | { |
115: | if ($identifier->isClass()) { |
116: | return $this->locateClassByName($identifier->getName()); |
117: | } |
118: | |
119: | if ($identifier->isFunction()) { |
120: | return $this->locateFunctionByName($identifier->getName()); |
121: | } |
122: | |
123: | if ($identifier->isConstant()) { |
124: | return $this->locateConstantByName($identifier->getName()); |
125: | } |
126: | |
127: | return null; |
128: | } |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | private function locateClassByName(string $className): ?array |
154: | { |
155: | if (ClassExistenceChecker::exists($className, false)) { |
156: | $classReflection = new ReflectionClass($className); |
157: | |
158: | $filename = $classReflection->getFileName(); |
159: | |
160: | if (! is_string($filename)) { |
161: | return null; |
162: | } |
163: | |
164: | return ['fileName' => $filename, 'name' => $classReflection->getName()]; |
165: | } |
166: | |
167: | $this->silenceErrors(); |
168: | |
169: | try { |
170: | $locatedFile = FileReadTrapStreamWrapper::withStreamWrapperOverride(static function () use ($className): ?string { |
171: | foreach (spl_autoload_functions() as $preExistingAutoloader) { |
172: | $preExistingAutoloader($className); |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | |
179: | |
180: | if (FileReadTrapStreamWrapper::$autoloadLocatedFile !== null) { |
181: | return FileReadTrapStreamWrapper::$autoloadLocatedFile; |
182: | } |
183: | } |
184: | |
185: | return null; |
186: | }); |
187: | |
188: | if ($locatedFile === null) { |
189: | return null; |
190: | } |
191: | |
192: | return ['fileName' => $locatedFile, 'name' => $className]; |
193: | } finally { |
194: | restore_error_handler(); |
195: | } |
196: | } |
197: | |
198: | private function silenceErrors(): void |
199: | { |
200: | set_error_handler(static function () : bool { |
201: | return true; |
202: | }); |
203: | } |
204: | |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | private function locateFunctionByName(string $functionName): ?array |
216: | { |
217: | if (! function_exists($functionName)) { |
218: | return null; |
219: | } |
220: | |
221: | $reflectionFileName = (new ReflectionFunction($functionName))->getFileName(); |
222: | |
223: | if (! is_string($reflectionFileName)) { |
224: | return null; |
225: | } |
226: | |
227: | return ['fileName' => $reflectionFileName, 'name' => $functionName]; |
228: | } |
229: | |
230: | |
231: | |
232: | |
233: | |
234: | |
235: | |
236: | |
237: | private function locateConstantByName(string $constantName): ?array |
238: | { |
239: | if (! defined($constantName)) { |
240: | return null; |
241: | } |
242: | |
243: | |
244: | $constants = get_defined_constants(true); |
245: | |
246: | if (! array_key_exists($constantName, $constants['user'])) { |
247: | return null; |
248: | } |
249: | |
250: | |
251: | $this->constantVisitor->setConstantName($constantName); |
252: | |
253: | $constantFileName = null; |
254: | |
255: | |
256: | |
257: | |
258: | |
259: | |
260: | foreach (array_reverse(get_included_files()) as $includedFileName) { |
261: | try { |
262: | FileChecker::assertReadableFile($includedFileName); |
263: | } catch (InvalidFileLocation $exception) { |
264: | continue; |
265: | } |
266: | |
267: | |
268: | $ast = $this->phpParser->parse(file_get_contents($includedFileName)); |
269: | |
270: | $this->nodeTraverser->traverse($ast); |
271: | |
272: | |
273: | if ($this->constantVisitor->getNode() !== null) { |
274: | $constantFileName = $includedFileName; |
275: | break; |
276: | } |
277: | } |
278: | |
279: | if ($constantFileName === null) { |
280: | return null; |
281: | } |
282: | |
283: | return ['fileName' => $constantFileName, 'name' => $constantName]; |
284: | } |
285: | |
286: | private function createConstantVisitor(): NodeVisitorAbstract |
287: | { |
288: | return new class () extends NodeVisitorAbstract |
289: | { |
290: | |
291: | |
292: | |
293: | private $constantName = null; |
294: | |
295: | |
296: | |
297: | |
298: | private $node = null; |
299: | |
300: | public function enterNode(Node $node): ?int |
301: | { |
302: | if ($node instanceof Node\Stmt\Const_) { |
303: | foreach ($node->consts as $constNode) { |
304: | if ((($constNodeNamespacedName = $constNode->namespacedName) ? $constNodeNamespacedName->toString() : null) === $this->constantName) { |
305: | $this->node = $node; |
306: | |
307: | return NodeTraverser::STOP_TRAVERSAL; |
308: | } |
309: | } |
310: | |
311: | return NodeTraverser::DONT_TRAVERSE_CHILDREN; |
312: | } |
313: | |
314: | if ($node instanceof Node\Expr\FuncCall) { |
315: | try { |
316: | |
317: | ConstantNodeChecker::assertValidDefineFunctionCall($node); |
318: | } catch (InvalidConstantNode $exception) { |
319: | return null; |
320: | } |
321: | |
322: | $argumentNameNode = $node->args[0]; |
323: | assert($argumentNameNode instanceof Node\Arg); |
324: | $nameNode = $argumentNameNode->value; |
325: | assert($nameNode instanceof Node\Scalar\String_); |
326: | |
327: | if ($nameNode->value === $this->constantName) { |
328: | $this->node = $node; |
329: | |
330: | return NodeTraverser::STOP_TRAVERSAL; |
331: | } |
332: | } |
333: | |
334: | return null; |
335: | } |
336: | |
337: | public function setConstantName(string $constantName): void |
338: | { |
339: | $this->constantName = $constantName; |
340: | } |
341: | |
342: | |
343: | public function getNode() |
344: | { |
345: | return $this->node; |
346: | } |
347: | }; |
348: | } |
349: | } |
350: | |