| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace PHPStan\BetterReflection; |
| 6: | |
| 7: | use PhpParser\Parser; |
| 8: | use PhpParser\ParserFactory; |
| 9: | use PhpParser\PrettyPrinter\Standard; |
| 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: | public static int $phpVersion = PHP_VERSION_ID; |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | private static $sharedSourceLocator = null; |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | private $sourceLocator = null; |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | private static $sharedReflector = null; |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | private $reflector = null; |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | private static $sharedPhpParser = null; |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | private $phpParser = null; |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | private $astLocator = null; |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | private $findReflectionOnLine = null; |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | private $sourceStubber = null; |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | private static $sharedSourceStubber = null; |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | private static $sharedPrinter = null; |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | private $printer = null; |
| 90: | |
| 91: | public static function populate(int $phpVersion, SourceLocator $sourceLocator, Reflector $classReflector, Parser $phpParser, SourceStubber $sourceStubber, Standard $printer): void |
| 92: | { |
| 93: | self::$phpVersion = $phpVersion; |
| 94: | self::$sharedSourceLocator = $sourceLocator; |
| 95: | self::$sharedReflector = $classReflector; |
| 96: | self::$sharedPhpParser = $phpParser; |
| 97: | self::$sharedSourceStubber = $sourceStubber; |
| 98: | self::$sharedPrinter = $printer; |
| 99: | } |
| 100: | |
| 101: | public function __construct() |
| 102: | { |
| 103: | $this->sourceLocator = self::$sharedSourceLocator; |
| 104: | $this->reflector = self::$sharedReflector; |
| 105: | $this->phpParser = self::$sharedPhpParser; |
| 106: | $this->sourceStubber = self::$sharedSourceStubber; |
| 107: | $this->printer = self::$sharedPrinter; |
| 108: | } |
| 109: | |
| 110: | public function sourceLocator(): SourceLocator |
| 111: | { |
| 112: | $astLocator = $this->astLocator(); |
| 113: | $sourceStubber = $this->sourceStubber(); |
| 114: | |
| 115: | return $this->sourceLocator |
| 116: | ?? $this->sourceLocator = new MemoizingSourceLocator(new AggregateSourceLocator([ |
| 117: | new PhpInternalSourceLocator($astLocator, $sourceStubber), |
| 118: | new EvaledCodeSourceLocator($astLocator, $sourceStubber), |
| 119: | new AutoloadSourceLocator($astLocator, $this->phpParser()), |
| 120: | ])); |
| 121: | } |
| 122: | |
| 123: | public function reflector(): Reflector |
| 124: | { |
| 125: | return $this->reflector |
| 126: | ?? $this->reflector = new DefaultReflector($this->sourceLocator()); |
| 127: | } |
| 128: | |
| 129: | public function phpParser(): Parser |
| 130: | { |
| 131: | return $this->phpParser |
| 132: | ?? $this->phpParser = (new ParserFactory())->createForNewestSupportedVersion(); |
| 133: | } |
| 134: | |
| 135: | public function astLocator(): AstLocator |
| 136: | { |
| 137: | return $this->astLocator |
| 138: | ?? $this->astLocator = new AstLocator($this->phpParser()); |
| 139: | } |
| 140: | |
| 141: | public function findReflectionsOnLine(): FindReflectionOnLine |
| 142: | { |
| 143: | return $this->findReflectionOnLine |
| 144: | ?? $this->findReflectionOnLine = new FindReflectionOnLine($this->sourceLocator(), $this->astLocator()); |
| 145: | } |
| 146: | |
| 147: | public function sourceStubber(): SourceStubber |
| 148: | { |
| 149: | return $this->sourceStubber |
| 150: | ?? $this->sourceStubber = new AggregateSourceStubber( |
| 151: | new PhpStormStubsSourceStubber($this->phpParser(), $this->printer(), self::$phpVersion), |
| 152: | new ReflectionSourceStubber($this->printer()), |
| 153: | ); |
| 154: | } |
| 155: | |
| 156: | public function printer(): Standard |
| 157: | { |
| 158: | return $this->printer ?? $this->printer = new Standard(['shortArraySyntax' => true]); |
| 159: | } |
| 160: | } |
| 161: | |