|
20 | 20 | use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator; |
21 | 21 | use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker; |
22 | 22 | use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher; |
| 23 | +use PHPStan\Reflection\BetterReflection\SourceLocator\LazySourceLocator; |
23 | 24 | use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository; |
24 | 25 | use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedPsrAutoloaderLocatorFactory; |
25 | 26 | use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository; |
@@ -80,95 +81,99 @@ public function __construct( |
80 | 81 |
|
81 | 82 | public function create(): SourceLocator |
82 | 83 | { |
83 | | - $locators = [ |
84 | | - $this->optimizedSingleFileSourceLocatorRepository->getOrCreate( |
85 | | - PHP_VERSION_ID < 80500 |
86 | | - ? __DIR__ . '/../../../stubs/runtime/Attribute84.php' |
87 | | - : __DIR__ . '/../../../stubs/runtime/Attribute85.php', |
88 | | - ), |
89 | | - ]; |
90 | | - |
91 | | - if ($this->singleReflectionFile !== null) { |
92 | | - $locators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($this->singleReflectionFile); |
93 | | - } |
94 | | - |
95 | | - $astLocator = new Locator($this->parser); |
96 | | - $locators[] = new AutoloadFunctionsSourceLocator( |
97 | | - new AutoloadSourceLocator($this->fileNodesFetcher, false), |
98 | | - new ReflectionClassSourceLocator( |
99 | | - $astLocator, |
100 | | - $this->reflectionSourceStubber, |
101 | | - ), |
102 | | - ); |
103 | | - |
104 | | - $analysedDirectories = []; |
105 | | - $analysedFiles = []; |
106 | | - |
107 | | - foreach (array_merge($this->analysedPaths, $this->analysedPathsFromConfig) as $analysedPath) { |
108 | | - if (is_file($analysedPath)) { |
109 | | - $analysedFiles[] = $analysedPath; |
110 | | - continue; |
| 84 | + $initializer = function () { |
| 85 | + $locators = [ |
| 86 | + $this->optimizedSingleFileSourceLocatorRepository->getOrCreate( |
| 87 | + PHP_VERSION_ID < 80500 |
| 88 | + ? __DIR__ . '/../../../stubs/runtime/Attribute84.php' |
| 89 | + : __DIR__ . '/../../../stubs/runtime/Attribute85.php', |
| 90 | + ), |
| 91 | + ]; |
| 92 | + |
| 93 | + if ($this->singleReflectionFile !== null) { |
| 94 | + $locators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($this->singleReflectionFile); |
111 | 95 | } |
112 | 96 |
|
113 | | - if (!is_dir($analysedPath)) { |
114 | | - continue; |
| 97 | + $astLocator = new Locator($this->parser); |
| 98 | + $locators[] = new AutoloadFunctionsSourceLocator( |
| 99 | + new AutoloadSourceLocator($this->fileNodesFetcher, false), |
| 100 | + new ReflectionClassSourceLocator( |
| 101 | + $astLocator, |
| 102 | + $this->reflectionSourceStubber, |
| 103 | + ), |
| 104 | + ); |
| 105 | + |
| 106 | + $analysedDirectories = []; |
| 107 | + $analysedFiles = []; |
| 108 | + |
| 109 | + foreach (array_merge($this->analysedPaths, $this->analysedPathsFromConfig) as $analysedPath) { |
| 110 | + if (is_file($analysedPath)) { |
| 111 | + $analysedFiles[] = $analysedPath; |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + if (!is_dir($analysedPath)) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + $analysedDirectories[] = $analysedPath; |
115 | 120 | } |
116 | 121 |
|
117 | | - $analysedDirectories[] = $analysedPath; |
118 | | - } |
| 122 | + $fileLocators = []; |
| 123 | + $analysedFiles = array_unique(array_merge($analysedFiles, $this->scanFiles)); |
| 124 | + foreach ($analysedFiles as $analysedFile) { |
| 125 | + $fileLocators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($analysedFile); |
| 126 | + } |
119 | 127 |
|
120 | | - $fileLocators = []; |
121 | | - $analysedFiles = array_unique(array_merge($analysedFiles, $this->scanFiles)); |
122 | | - foreach ($analysedFiles as $analysedFile) { |
123 | | - $fileLocators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($analysedFile); |
124 | | - } |
| 128 | + $directories = array_unique(array_merge($analysedDirectories, $this->scanDirectories)); |
| 129 | + foreach ($directories as $directory) { |
| 130 | + $fileLocators[] = $this->optimizedDirectorySourceLocatorRepository->getOrCreate($directory); |
| 131 | + } |
125 | 132 |
|
126 | | - $directories = array_unique(array_merge($analysedDirectories, $this->scanDirectories)); |
127 | | - foreach ($directories as $directory) { |
128 | | - $fileLocators[] = $this->optimizedDirectorySourceLocatorRepository->getOrCreate($directory); |
129 | | - } |
| 133 | + $astPhp8Locator = new Locator($this->php8Parser); |
130 | 134 |
|
131 | | - $astPhp8Locator = new Locator($this->php8Parser); |
| 135 | + $composerLocators = []; |
132 | 136 |
|
133 | | - $composerLocators = []; |
| 137 | + foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) { |
| 138 | + $locator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerAutoloaderProjectPath); |
| 139 | + if ($locator === null) { |
| 140 | + continue; |
| 141 | + } |
| 142 | + $composerLocators[] = $locator; |
| 143 | + } |
134 | 144 |
|
135 | | - foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) { |
136 | | - $locator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerAutoloaderProjectPath); |
137 | | - if ($locator === null) { |
138 | | - continue; |
| 145 | + if (count($composerLocators) > 0) { |
| 146 | + $fileLocators[] = new SkipPolyfillSourceLocator(new AggregateSourceLocator($composerLocators), $this->phpVersion); |
139 | 147 | } |
140 | | - $composerLocators[] = $locator; |
141 | | - } |
142 | | - |
143 | | - if (count($composerLocators) > 0) { |
144 | | - $fileLocators[] = new SkipPolyfillSourceLocator(new AggregateSourceLocator($composerLocators), $this->phpVersion); |
145 | | - } |
146 | | - |
147 | | - if (extension_loaded('phar')) { |
148 | | - $pharProtocolPath = Phar::running(); |
149 | | - if ($pharProtocolPath !== '') { |
150 | | - $mappings = [ |
151 | | - 'PHPStan\\BetterReflection\\' => [$pharProtocolPath . '/vendor/ondrejmirtes/better-reflection/src/'], |
152 | | - ]; |
153 | | - if ($this->playgroundMode) { |
154 | | - $mappings['PHPStan\\'] = [$pharProtocolPath . '/src/']; |
155 | | - } else { |
156 | | - $mappings['PHPStan\\Testing\\'] = [$pharProtocolPath . '/src/Testing/']; |
| 148 | + |
| 149 | + if (extension_loaded('phar')) { |
| 150 | + $pharProtocolPath = Phar::running(); |
| 151 | + if ($pharProtocolPath !== '') { |
| 152 | + $mappings = [ |
| 153 | + 'PHPStan\\BetterReflection\\' => [$pharProtocolPath . '/vendor/ondrejmirtes/better-reflection/src/'], |
| 154 | + ]; |
| 155 | + if ($this->playgroundMode) { |
| 156 | + $mappings['PHPStan\\'] = [$pharProtocolPath . '/src/']; |
| 157 | + } else { |
| 158 | + $mappings['PHPStan\\Testing\\'] = [$pharProtocolPath . '/src/Testing/']; |
| 159 | + } |
| 160 | + $fileLocators[] = $this->optimizedPsrAutoloaderLocatorFactory->create( |
| 161 | + Psr4Mapping::fromArrayMappings($mappings), |
| 162 | + ); |
157 | 163 | } |
158 | | - $fileLocators[] = $this->optimizedPsrAutoloaderLocatorFactory->create( |
159 | | - Psr4Mapping::fromArrayMappings($mappings), |
160 | | - ); |
161 | 164 | } |
162 | | - } |
163 | 165 |
|
164 | | - $locators[] = new RewriteClassAliasSourceLocator(new AggregateSourceLocator($fileLocators)); |
165 | | - $locators[] = new SkipClassAliasSourceLocator(new PhpInternalSourceLocator($astPhp8Locator, $this->phpstormStubsSourceStubber)); |
| 166 | + $locators[] = new RewriteClassAliasSourceLocator(new AggregateSourceLocator($fileLocators)); |
| 167 | + $locators[] = new SkipClassAliasSourceLocator(new PhpInternalSourceLocator($astPhp8Locator, $this->phpstormStubsSourceStubber)); |
| 168 | + |
| 169 | + $locators[] = new AutoloadSourceLocator($this->fileNodesFetcher, true); |
| 170 | + $locators[] = new PhpVersionBlacklistSourceLocator(new PhpInternalSourceLocator($astLocator, $this->reflectionSourceStubber), $this->phpstormStubsSourceStubber); |
| 171 | + $locators[] = new PhpVersionBlacklistSourceLocator(new EvaledCodeSourceLocator($astLocator, $this->reflectionSourceStubber), $this->phpstormStubsSourceStubber); |
166 | 172 |
|
167 | | - $locators[] = new AutoloadSourceLocator($this->fileNodesFetcher, true); |
168 | | - $locators[] = new PhpVersionBlacklistSourceLocator(new PhpInternalSourceLocator($astLocator, $this->reflectionSourceStubber), $this->phpstormStubsSourceStubber); |
169 | | - $locators[] = new PhpVersionBlacklistSourceLocator(new EvaledCodeSourceLocator($astLocator, $this->reflectionSourceStubber), $this->phpstormStubsSourceStubber); |
| 173 | + return new AggregateSourceLocator($locators); |
| 174 | + }; |
170 | 175 |
|
171 | | - return new MemoizingSourceLocator(new AggregateSourceLocator($locators)); |
| 176 | + return new MemoizingSourceLocator(new LazySourceLocator($initializer)); |
172 | 177 | } |
173 | 178 |
|
174 | 179 | } |
0 commit comments