| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace PHPStan\BetterReflection; |
| 6: | |
| 7: | use PhpParser\Lexer\Emulative; |
| 8: | use PhpParser\Parser; |
| 9: | use PhpParser\ParserFactory; |
| 10: | use PHPStan\BetterReflection\Reflector\DefaultReflector; |
| 11: | use PHPStan\BetterReflection\Reflector\Reflector; |
| 12: | use PHPStan\BetterReflection\SourceLocator\Ast\Locator as AstLocator; |
| 13: | use PHPStan\BetterReflection\SourceLocator\SourceStubber\AggregateSourceStubber; |
| 14: | use PHPStan\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber; |
| 15: | use PHPStan\BetterReflection\SourceLocator\SourceStubber\ReflectionSourceStubber; |
| 16: | use PHPStan\BetterReflection\SourceLocator\SourceStubber\SourceStubber; |
| 17: | use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator; |
| 18: | use PHPStan\BetterReflection\SourceLocator\Type\AutoloadSourceLocator; |
| 19: | use PHPStan\BetterReflection\SourceLocator\Type\EvaledCodeSourceLocator; |
| 20: | use PHPStan\BetterReflection\SourceLocator\Type\MemoizingSourceLocator; |
| 21: | use PHPStan\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator; |
| 22: | use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator; |
| 23: | use PHPStan\BetterReflection\Util\FindReflectionOnLine; |
| 24: | |
| 25: | use const PHP_VERSION_ID; |
| 26: | |
| 27: | final class BetterReflection |
| 28: | { |
| 29: | |
| 30: | |
| 31: | |
| 32: | public static $phpVersion = PHP_VERSION_ID; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | private static $sharedSourceLocator = null; |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | private $sourceLocator = null; |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | private static $sharedReflector = null; |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | private $reflector = null; |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | private static $sharedPhpParser = null; |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | private $phpParser = null; |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | private $astLocator = null; |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | private $findReflectionOnLine = null; |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | private $sourceStubber = null; |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | private static $sharedSourceStubber = null; |
| 83: | |
| 84: | public static function populate(int $phpVersion, SourceLocator $sourceLocator, Reflector $classReflector, Parser $phpParser, SourceStubber $sourceStubber): void |
| 85: | { |
| 86: | self::$phpVersion = $phpVersion; |
| 87: | self::$sharedSourceLocator = $sourceLocator; |
| 88: | self::$sharedReflector = $classReflector; |
| 89: | self::$sharedPhpParser = $phpParser; |
| 90: | self::$sharedSourceStubber = $sourceStubber; |
| 91: | } |
| 92: | |
| 93: | public function __construct() |
| 94: | { |
| 95: | $this->sourceLocator = self::$sharedSourceLocator; |
| 96: | $this->reflector = self::$sharedReflector; |
| 97: | $this->phpParser = self::$sharedPhpParser; |
| 98: | $this->sourceStubber = self::$sharedSourceStubber; |
| 99: | } |
| 100: | |
| 101: | public function sourceLocator(): SourceLocator |
| 102: | { |
| 103: | $astLocator = $this->astLocator(); |
| 104: | $sourceStubber = $this->sourceStubber(); |
| 105: | |
| 106: | return $this->sourceLocator |
| 107: | ?? $this->sourceLocator = new MemoizingSourceLocator(new AggregateSourceLocator([ |
| 108: | new PhpInternalSourceLocator($astLocator, $sourceStubber), |
| 109: | new EvaledCodeSourceLocator($astLocator, $sourceStubber), |
| 110: | new AutoloadSourceLocator($astLocator, $this->phpParser()), |
| 111: | ])); |
| 112: | } |
| 113: | |
| 114: | public function reflector(): Reflector |
| 115: | { |
| 116: | return $this->reflector |
| 117: | ?? $this->reflector = new DefaultReflector($this->sourceLocator()); |
| 118: | } |
| 119: | |
| 120: | public function phpParser(): Parser |
| 121: | { |
| 122: | return $this->phpParser |
| 123: | ?? $this->phpParser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7, new Emulative([ |
| 124: | 'usedAttributes' => ['comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'], |
| 125: | ])); |
| 126: | } |
| 127: | |
| 128: | public function astLocator(): AstLocator |
| 129: | { |
| 130: | return $this->astLocator |
| 131: | ?? $this->astLocator = new AstLocator($this->phpParser()); |
| 132: | } |
| 133: | |
| 134: | public function findReflectionsOnLine(): FindReflectionOnLine |
| 135: | { |
| 136: | return $this->findReflectionOnLine |
| 137: | ?? $this->findReflectionOnLine = new FindReflectionOnLine($this->sourceLocator(), $this->astLocator()); |
| 138: | } |
| 139: | |
| 140: | public function sourceStubber(): SourceStubber |
| 141: | { |
| 142: | return $this->sourceStubber |
| 143: | ?? $this->sourceStubber = new AggregateSourceStubber(new PhpStormStubsSourceStubber($this->phpParser(), self::$phpVersion), new ReflectionSourceStubber()); |
| 144: | } |
| 145: | } |
| 146: | |