Skip to content

Commit e94f9bd

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents 7e37026 + 006f503 commit e94f9bd

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/PhpDoc/StubSourceLocatorFactory.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
1111
use PHPStan\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
1212
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
13+
use PHPStan\Reflection\BetterReflection\SourceLocator\LazySourceLocator;
1314
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedPsrAutoloaderLocatorFactory;
1415
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository;
1516
use function dirname;
@@ -30,10 +31,14 @@ public function __construct(
3031
public function create(): SourceLocator
3132
{
3233
$locators = [];
33-
$astPhp8Locator = new Locator($this->php8Parser);
34-
foreach ($this->stubFilesProvider->getStubFiles() as $stubFile) {
35-
$locators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($stubFile);
36-
}
34+
$locators[] = new LazySourceLocator(function () {
35+
$locators = [];
36+
foreach ($this->stubFilesProvider->getStubFiles() as $stubFile) {
37+
$locators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($stubFile);
38+
}
39+
40+
return new AggregateSourceLocator($locators);
41+
});
3742

3843
$locators[] = $this->optimizedPsrAutoloaderLocatorFactory->create(
3944
Psr4Mapping::fromArrayMappings([
@@ -46,6 +51,7 @@ public function create(): SourceLocator
4651
]),
4752
);
4853

54+
$astPhp8Locator = new Locator($this->php8Parser);
4955
$locators[] = new PhpInternalSourceLocator($astPhp8Locator, $this->phpStormStubsSourceStubber);
5056

5157
return new MemoizingSourceLocator(new AggregateSourceLocator($locators));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
/** @var callable(): SourceLocator */
16+
private $sourceLocatorFactory;
17+
18+
private ?SourceLocator $actualSourceLocator = null;
19+
20+
/**
21+
* @param callable(): SourceLocator $sourceLocatorFactory
22+
*/
23+
public function __construct(callable $sourceLocatorFactory)
24+
{
25+
$this->sourceLocatorFactory = $sourceLocatorFactory;
26+
}
27+
28+
#[Override]
29+
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
30+
{
31+
return $this->getSourceLocator()->locateIdentifier($reflector, $identifier);
32+
}
33+
34+
#[Override]
35+
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
36+
{
37+
return $this->getSourceLocator()->locateIdentifiersByType($reflector, $identifierType);
38+
}
39+
40+
private function getSourceLocator(): SourceLocator
41+
{
42+
$factory = $this->sourceLocatorFactory;
43+
return $this->actualSourceLocator ??= $factory();
44+
}
45+
46+
}

0 commit comments

Comments
 (0)