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: /** @var Standard|null */
87: private static $sharedPrinter = null;
88:
89: /** @var Standard|null */
90: private $printer = null;
91:
92: public static function populate(
93: int $phpVersion,
94: SourceLocator $sourceLocator,
95: Reflector $classReflector,
96: Parser $phpParser,
97: SourceStubber $sourceStubber,
98: Standard $printer
99: ): void {
100: self::$phpVersion = $phpVersion;
101: self::$sharedSourceLocator = $sourceLocator;
102: self::$sharedReflector = $classReflector;
103: self::$sharedPhpParser = $phpParser;
104: self::$sharedSourceStubber = $sourceStubber;
105: self::$sharedPrinter = $printer;
106: }
107:
108: public function __construct()
109: {
110: $this->sourceLocator = self::$sharedSourceLocator;
111: $this->reflector = self::$sharedReflector;
112: $this->phpParser = self::$sharedPhpParser;
113: $this->sourceStubber = self::$sharedSourceStubber;
114: $this->printer = self::$sharedPrinter;
115: }
116:
117: public function sourceLocator(): SourceLocator
118: {
119: $astLocator = $this->astLocator();
120: $sourceStubber = $this->sourceStubber();
121:
122: return $this->sourceLocator
123: ?? $this->sourceLocator = new MemoizingSourceLocator(new AggregateSourceLocator([
124: new PhpInternalSourceLocator($astLocator, $sourceStubber),
125: new EvaledCodeSourceLocator($astLocator, $sourceStubber),
126: new AutoloadSourceLocator($astLocator, $this->phpParser()),
127: ]));
128: }
129:
130: public function reflector(): Reflector
131: {
132: return $this->reflector
133: ?? $this->reflector = new DefaultReflector($this->sourceLocator());
134: }
135:
136: public function phpParser(): Parser
137: {
138: return $this->phpParser
139: ?? $this->phpParser = (new ParserFactory())->createForNewestSupportedVersion();
140: }
141:
142: public function originalPhpParser(): Parser
143: {
144: return $this->originalPhpParser
145: ?? $this->originalPhpParser = (new ParserFactory())->createForNewestSupportedVersion();
146: }
147:
148: public function astLocator(): AstLocator
149: {
150: return $this->astLocator
151: ?? $this->astLocator = new AstLocator($this->phpParser());
152: }
153:
154: public function findReflectionsOnLine(): FindReflectionOnLine
155: {
156: return $this->findReflectionOnLine
157: ?? $this->findReflectionOnLine = new FindReflectionOnLine($this->sourceLocator(), $this->astLocator());
158: }
159:
160: public function sourceStubber(): SourceStubber
161: {
162: return $this->sourceStubber
163: ?? $this->sourceStubber = new AggregateSourceStubber(
164: new PhpStormStubsSourceStubber($this->phpParser(), $this->printer(), self::$phpVersion),
165: new ReflectionSourceStubber($this->printer()),
166: );
167: }
168:
169: public function printer(): Standard
170: {
171: return $this->printer ?? $this->printer = new Standard(['shortArraySyntax' => true]);
172: }
173: }
174: