File tree Expand file tree Collapse file tree
Reflection/BetterReflection/SourceLocator Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010use PHPStan \BetterReflection \SourceLocator \Type \MemoizingSourceLocator ;
1111use PHPStan \BetterReflection \SourceLocator \Type \PhpInternalSourceLocator ;
1212use PHPStan \BetterReflection \SourceLocator \Type \SourceLocator ;
13+ use PHPStan \Reflection \BetterReflection \SourceLocator \LazySourceLocator ;
1314use PHPStan \Reflection \BetterReflection \SourceLocator \OptimizedPsrAutoloaderLocatorFactory ;
1415use PHPStan \Reflection \BetterReflection \SourceLocator \OptimizedSingleFileSourceLocatorRepository ;
1516use 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 ));
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments