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