|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Testing; |
| 4 | + |
| 5 | +use Composer\Autoload\ClassLoader; |
| 6 | +use PHPStan\DependencyInjection\Container; |
| 7 | +use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator; |
| 8 | +use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker; |
| 9 | +use Roave\BetterReflection\Reflector\FunctionReflector; |
| 10 | +use Roave\BetterReflection\SourceLocator\Ast\Locator; |
| 11 | +use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber; |
| 12 | +use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; |
| 13 | +use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator; |
| 14 | +use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator; |
| 15 | +use Roave\BetterReflection\SourceLocator\Type\SourceLocator; |
| 16 | + |
| 17 | +class TestCaseSourceLocatorFactory |
| 18 | +{ |
| 19 | + |
| 20 | + private Container $container; |
| 21 | + |
| 22 | + private ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker; |
| 23 | + |
| 24 | + private \PhpParser\Parser $phpParser; |
| 25 | + |
| 26 | + private PhpStormStubsSourceStubber $phpStormStubsSourceStubber; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + Container $container, |
| 30 | + ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker, |
| 31 | + \PhpParser\Parser $phpParser, |
| 32 | + PhpStormStubsSourceStubber $phpStormStubsSourceStubber |
| 33 | + ) |
| 34 | + { |
| 35 | + $this->container = $container; |
| 36 | + $this->composerJsonAndInstalledJsonSourceLocatorMaker = $composerJsonAndInstalledJsonSourceLocatorMaker; |
| 37 | + $this->phpParser = $phpParser; |
| 38 | + $this->phpStormStubsSourceStubber = $phpStormStubsSourceStubber; |
| 39 | + } |
| 40 | + |
| 41 | + public function create(): SourceLocator |
| 42 | + { |
| 43 | + $classLoaderReflection = new \ReflectionClass(ClassLoader::class); |
| 44 | + if ($classLoaderReflection->getFileName() === false) { |
| 45 | + throw new \PHPStan\ShouldNotHappenException('Unknown ClassLoader filename'); |
| 46 | + } |
| 47 | + |
| 48 | + $composerProjectPath = dirname($classLoaderReflection->getFileName(), 3); |
| 49 | + if (!is_file($composerProjectPath . '/composer.json')) { |
| 50 | + throw new \PHPStan\ShouldNotHappenException(sprintf('composer.json not found in directory %s', $composerProjectPath)); |
| 51 | + } |
| 52 | + |
| 53 | + $composerSourceLocator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerProjectPath); |
| 54 | + if ($composerSourceLocator === null) { |
| 55 | + throw new \PHPStan\ShouldNotHappenException('Could not create composer source locator'); |
| 56 | + } |
| 57 | + |
| 58 | + $locators = [ |
| 59 | + $composerSourceLocator, |
| 60 | + ]; |
| 61 | + $astLocator = new Locator($this->phpParser, function (): FunctionReflector { |
| 62 | + return $this->container->getService('testCaseFunctionReflector'); |
| 63 | + }); |
| 64 | + |
| 65 | + $locators[] = new AutoloadSourceLocator($astLocator); |
| 66 | + $locators[] = new PhpInternalSourceLocator($astLocator, $this->phpStormStubsSourceStubber); |
| 67 | + |
| 68 | + return new MemoizingSourceLocator(new AggregateSourceLocator($locators)); |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments