| 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: | foreach (array_reverse(get_included_files()) as $includedFileName) { |
| 260: | try { |
| 261: | FileChecker::assertReadableFile($includedFileName); |
| 262: | } catch (InvalidFileLocation $exception) { |
| 263: | continue; |
| 264: | } |
| 265: | |
| 266: | |
| 267: | $ast = $this->phpParser->parse(file_get_contents($includedFileName)); |
| 268: | |
| 269: | $this->nodeTraverser->traverse($ast); |
| 270: | |
| 271: | |
| 272: | if ($this->constantVisitor->getNode() !== null) { |
| 273: | $constantFileName = $includedFileName; |
| 274: | break; |
| 275: | } |
| 276: | } |
| 277: | |
| 278: | if ($constantFileName === null) { |
| 279: | return null; |
| 280: | } |
| 281: | |
| 282: | return ['fileName' => $constantFileName, 'name' => $constantName]; |
| 283: | } |
| 284: | |
| 285: | private function createConstantVisitor(): NodeVisitorAbstract |
| 286: | { |
| 287: | return new class () extends NodeVisitorAbstract |
| 288: | { |
| 289: | |
| 290: | |
| 291: | |
| 292: | private $constantName = null; |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | private $node = null; |
| 298: | |
| 299: | public function enterNode(Node $node): ?int |
| 300: | { |
| 301: | if ($node instanceof Node\Stmt\Const_) { |
| 302: | foreach ($node->consts as $constNode) { |
| 303: | if ((($constNodeNamespacedName = $constNode->namespacedName) ? $constNodeNamespacedName->toString() : null) === $this->constantName) { |
| 304: | $this->node = $node; |
| 305: | |
| 306: | return NodeTraverser::STOP_TRAVERSAL; |
| 307: | } |
| 308: | } |
| 309: | |
| 310: | return NodeTraverser::DONT_TRAVERSE_CHILDREN; |
| 311: | } |
| 312: | |
| 313: | if ($node instanceof Node\Expr\FuncCall) { |
| 314: | try { |
| 315: | |
| 316: | ConstantNodeChecker::assertValidDefineFunctionCall($node); |
| 317: | } catch (InvalidConstantNode $exception) { |
| 318: | return null; |
| 319: | } |
| 320: | |
| 321: | $argumentNameNode = $node->args[0]; |
| 322: | assert($argumentNameNode instanceof Node\Arg); |
| 323: | $nameNode = $argumentNameNode->value; |
| 324: | assert($nameNode instanceof Node\Scalar\String_); |
| 325: | |
| 326: | if ($nameNode->value === $this->constantName) { |
| 327: | $this->node = $node; |
| 328: | |
| 329: | return NodeTraverser::STOP_TRAVERSAL; |
| 330: | } |
| 331: | } |
| 332: | |
| 333: | return null; |
| 334: | } |
| 335: | |
| 336: | public function setConstantName(string $constantName): void |
| 337: | { |
| 338: | $this->constantName = $constantName; |
| 339: | } |
| 340: | |
| 341: | |
| 342: | public function getNode() |
| 343: | { |
| 344: | return $this->node; |
| 345: | } |
| 346: | }; |
| 347: | } |
| 348: | } |
| 349: | |