Skip to content

Commit a009ea9

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents cff51bb + 98d015f commit a009ea9

2 files changed

Lines changed: 124 additions & 74 deletions

File tree

src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator;
2121
use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker;
2222
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
23+
use PHPStan\Reflection\BetterReflection\SourceLocator\LazySourceLocator;
2324
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository;
2425
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedPsrAutoloaderLocatorFactory;
2526
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository;
@@ -80,95 +81,99 @@ public function __construct(
8081

8182
public function create(): SourceLocator
8283
{
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);
11195
}
11296

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;
115120
}
116121

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+
}
119127

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+
}
125132

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);
130134

131-
$astPhp8Locator = new Locator($this->php8Parser);
135+
$composerLocators = [];
132136

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+
}
134144

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);
139147
}
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+
);
157163
}
158-
$fileLocators[] = $this->optimizedPsrAutoloaderLocatorFactory->create(
159-
Psr4Mapping::fromArrayMappings($mappings),
160-
);
161164
}
162-
}
163165

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);
166172

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+
};
170175

171-
return new MemoizingSourceLocator(new AggregateSourceLocator($locators));
176+
return new MemoizingSourceLocator(new LazySourceLocator($initializer));
172177
}
173178

174179
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\BetterReflection\SourceLocator;
4+
5+
use Override;
6+
use PHPStan\BetterReflection\Identifier\Identifier;
7+
use PHPStan\BetterReflection\Identifier\IdentifierType;
8+
use PHPStan\BetterReflection\Reflection\Reflection;
9+
use PHPStan\BetterReflection\Reflector\Reflector;
10+
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
11+
12+
final class LazySourceLocator implements SourceLocator
13+
{
14+
15+
private ?SourceLocator $wrappedSourceLocator = null;
16+
17+
/** @var callable():SourceLocator */
18+
private $initializer;
19+
20+
/**
21+
* @param callable():SourceLocator $initializer
22+
*/
23+
public function __construct(callable $initializer)
24+
{
25+
$this->initializer = $initializer;
26+
}
27+
28+
private function lazyInitialize(): SourceLocator
29+
{
30+
return $this->wrappedSourceLocator ??= ($this->initializer)();
31+
}
32+
33+
#[Override]
34+
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
35+
{
36+
return $this->lazyInitialize()->locateIdentifier($reflector, $identifier);
37+
}
38+
39+
#[Override]
40+
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
41+
{
42+
return $this->lazyInitialize()->locateIdentifiersByType($reflector, $identifierType);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)