From 1cd97b4ec895fb3ea1a2101afb6a63005f52409c Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 24 Jun 2020 19:51:44 +0200 Subject: [PATCH] More cleanup I realized that "files" from composer.json are already loaded so AutoloadSourceLocator will work even for functions --- compiler/build/scoper.inc.php | 10 -- conf/config.neon | 4 - .../BetterReflectionSourceLocatorFactory.php | 19 --- ...JsonAndInstalledJsonSourceLocatorMaker.php | 110 ------------------ src/Testing/TestCase.php | 26 +---- src/Testing/TestCaseSourceLocatorFactory.php | 25 +--- 6 files changed, 2 insertions(+), 192 deletions(-) delete mode 100644 src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php diff --git a/compiler/build/scoper.inc.php b/compiler/build/scoper.inc.php index 415984b25d..d8d6ea8cde 100644 --- a/compiler/build/scoper.inc.php +++ b/compiler/build/scoper.inc.php @@ -148,16 +148,6 @@ function (string $filePath, string $prefix, string $content): string { return \Nette\Neon\Neon::encode($updatedNeon, \Nette\Neon\Neon::BLOCK); }, - function (string $filePath, string $prefix, string $content): string { - if (!in_array($filePath, [ - 'src/Testing/TestCaseSourceLocatorFactory.php', - 'src/Testing/TestCase.php', - ], true)) { - return $content; - } - - return str_replace(sprintf('%s\\Composer\\Autoload\\ClassLoader', $prefix), 'Composer\\Autoload\\ClassLoader', $content); - }, function (string $filePath, string $prefix, string $content): string { if ($filePath !== 'vendor/jetbrains/phpstorm-stubs/PhpStormStubsMap.php') { return $content; diff --git a/conf/config.neon b/conf/config.neon index 7dd13295de..d563401eef 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -490,9 +490,6 @@ services: - class: PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator - - - class: PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker - - implement: PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory @@ -1152,7 +1149,6 @@ services: scanFiles: %scanFiles% scanDirectories: %scanDirectories% analysedPaths: %analysedPaths% - composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths% analysedPathsFromConfig: %analysedPathsFromConfig% singleReflectionFile: %singleReflectionFile% diff --git a/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php b/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php index 8ac02389b6..64dde7bce7 100644 --- a/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php +++ b/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php @@ -4,7 +4,6 @@ use PHPStan\DependencyInjection\Container; use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator; -use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker; use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository; use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository; use PHPStan\Reflection\BetterReflection\SourceLocator\SkipClassAliasSourceLocator; @@ -37,9 +36,6 @@ class BetterReflectionSourceLocatorFactory /** @var \PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository */ private $optimizedDirectorySourceLocatorRepository; - /** @var ComposerJsonAndInstalledJsonSourceLocatorMaker */ - private $composerJsonAndInstalledJsonSourceLocatorMaker; - /** @var AutoloadSourceLocator */ private $autoloadSourceLocator; @@ -61,9 +57,6 @@ class BetterReflectionSourceLocatorFactory /** @var string[] */ private $analysedPaths; - /** @var string[] */ - private $composerAutoloaderProjectPaths; - /** @var string[] */ private $analysedPathsFromConfig; @@ -76,7 +69,6 @@ class BetterReflectionSourceLocatorFactory * @param string[] $scanFiles * @param string[] $scanDirectories * @param string[] $analysedPaths - * @param string[] $composerAutoloaderProjectPaths * @param string[] $analysedPathsFromConfig * @param string|null $singleReflectionFile */ @@ -86,7 +78,6 @@ public function __construct( ReflectionSourceStubber $reflectionSourceStubber, OptimizedSingleFileSourceLocatorRepository $optimizedSingleFileSourceLocatorRepository, OptimizedDirectorySourceLocatorRepository $optimizedDirectorySourceLocatorRepository, - ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker, AutoloadSourceLocator $autoloadSourceLocator, Container $container, array $autoloadDirectories, @@ -94,7 +85,6 @@ public function __construct( array $scanFiles, array $scanDirectories, array $analysedPaths, - array $composerAutoloaderProjectPaths, array $analysedPathsFromConfig, ?string $singleReflectionFile ) @@ -104,7 +94,6 @@ public function __construct( $this->reflectionSourceStubber = $reflectionSourceStubber; $this->optimizedSingleFileSourceLocatorRepository = $optimizedSingleFileSourceLocatorRepository; $this->optimizedDirectorySourceLocatorRepository = $optimizedDirectorySourceLocatorRepository; - $this->composerJsonAndInstalledJsonSourceLocatorMaker = $composerJsonAndInstalledJsonSourceLocatorMaker; $this->autoloadSourceLocator = $autoloadSourceLocator; $this->container = $container; $this->autoloadDirectories = $autoloadDirectories; @@ -112,7 +101,6 @@ public function __construct( $this->scanFiles = $scanFiles; $this->scanDirectories = $scanDirectories; $this->analysedPaths = $analysedPaths; - $this->composerAutoloaderProjectPaths = $composerAutoloaderProjectPaths; $this->analysedPathsFromConfig = $analysedPathsFromConfig; $this->singleReflectionFile = $singleReflectionFile; } @@ -156,13 +144,6 @@ public function create(): SourceLocator }); $locators[] = new SkipClassAliasSourceLocator(new PhpInternalSourceLocator($astLocator, $this->phpstormStubsSourceStubber)); $locators[] = $this->autoloadSourceLocator; - foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) { - $locator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerAutoloaderProjectPath); - if ($locator === null) { - continue; - } - $locators[] = $locator; - } $locators[] = new PhpInternalSourceLocator($astLocator, $this->reflectionSourceStubber); $locators[] = new EvaledCodeSourceLocator($astLocator, $this->reflectionSourceStubber); diff --git a/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php b/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php deleted file mode 100644 index 7fd42ad80f..0000000000 --- a/src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php +++ /dev/null @@ -1,110 +0,0 @@ -optimizedSingleFileSourceLocatorRepository = $optimizedSingleFileSourceLocatorRepository; - } - - public function create(string $projectInstallationPath): ?SourceLocator - { - $composerJsonPath = $projectInstallationPath . '/composer.json'; - if (!is_file($composerJsonPath)) { - return null; - } - $installedJsonPath = $projectInstallationPath . '/vendor/composer/installed.json'; - if (!is_file($installedJsonPath)) { - return null; - } - - $installedJsonDirectoryPath = dirname($installedJsonPath); - - try { - $composerJsonContents = FileReader::read($composerJsonPath); - $composer = Json::decode($composerJsonContents, Json::FORCE_ARRAY); - } catch (\PHPStan\File\CouldNotReadFileException | \Nette\Utils\JsonException $e) { - return null; - } - - try { - $installedJsonContents = FileReader::read($installedJsonPath); - $installedJson = Json::decode($installedJsonContents, Json::FORCE_ARRAY); - } catch (\PHPStan\File\CouldNotReadFileException | \Nette\Utils\JsonException $e) { - return null; - } - - $installed = $installedJson['packages'] ?? $installedJson; - - $filePaths = array_merge( - $this->prefixPaths($this->packageToFilePaths($composer), $projectInstallationPath . '/'), - ...array_map(function (array $package) use ($projectInstallationPath, $installedJsonDirectoryPath): array { - return $this->prefixPaths( - $this->packageToFilePaths($package), - $this->packagePrefixPath($projectInstallationPath, $installedJsonDirectoryPath, $package) - ); - }, $installed) - ); - - $locators = []; - foreach ($filePaths as $file) { - if (!is_file($file)) { - continue; - } - $locators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($file); - } - - return new AggregateSourceLocator($locators); - } - - /** - * @param mixed[] $package - * - * @return array - */ - private function packageToFilePaths(array $package): array - { - return $package['autoload']['files'] ?? []; - } - - /** - * @param mixed[] $package - */ - private function packagePrefixPath( - string $projectInstallationPath, - string $installedJsonDirectoryPath, - array $package - ): string - { - if (array_key_exists('install-path', $package)) { - return $installedJsonDirectoryPath . '/' . $package['install-path'] . '/'; - } - - return $projectInstallationPath . '/vendor/' . $package['name'] . '/'; - } - - /** - * @param array $paths - * - * @return array - */ - private function prefixPaths(array $paths, string $prefix): array - { - return array_map(static function (string $path) use ($prefix): string { - return $prefix . $path; - }, $paths); - } - -} diff --git a/src/Testing/TestCase.php b/src/Testing/TestCase.php index e6bb36ef89..7c84b1895b 100644 --- a/src/Testing/TestCase.php +++ b/src/Testing/TestCase.php @@ -2,7 +2,6 @@ namespace PHPStan\Testing; -use Composer\Autoload\ClassLoader; use PhpParser\PrettyPrinter\Standard; use PHPStan\Analyser\DirectScopeFactory; use PHPStan\Analyser\MutatingScope; @@ -37,7 +36,6 @@ use PHPStan\Reflection\BetterReflection\Reflector\MemoizingConstantReflector; use PHPStan\Reflection\BetterReflection\Reflector\MemoizingFunctionReflector; use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator; -use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker; use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\FunctionReflectionFactory; @@ -409,31 +407,9 @@ public static function getReflectors(): array return self::$reflectors; } - if (!class_exists(ClassLoader::class)) { - self::fail('Composer ClassLoader is unknown'); - } - - $classLoaderReflection = new \ReflectionClass(ClassLoader::class); - if ($classLoaderReflection->getFileName() === false) { - self::fail('Unknown ClassLoader filename'); - } - - $composerProjectPath = dirname($classLoaderReflection->getFileName(), 3); - if (!is_file($composerProjectPath . '/composer.json')) { - self::fail(sprintf('composer.json not found in directory %s', $composerProjectPath)); - } - - $composerJsonAndInstalledJsonSourceLocatorMaker = self::getContainer()->getByType(ComposerJsonAndInstalledJsonSourceLocatorMaker::class); - $composerSourceLocator = $composerJsonAndInstalledJsonSourceLocatorMaker->create($composerProjectPath); - if ($composerSourceLocator === null) { - self::fail('Could not create composer source locator'); - } - // these need to be synced with TestCase-staticReflection.neon file and TestCaseSourceLocatorFactory - $locators = [ - $composerSourceLocator, - ]; + $locators = []; $phpParser = new PhpParserDecorator(self::getContainer()->getByType(CachedParser::class)); diff --git a/src/Testing/TestCaseSourceLocatorFactory.php b/src/Testing/TestCaseSourceLocatorFactory.php index d25cbc1c77..5267dec460 100644 --- a/src/Testing/TestCaseSourceLocatorFactory.php +++ b/src/Testing/TestCaseSourceLocatorFactory.php @@ -2,10 +2,8 @@ namespace PHPStan\Testing; -use Composer\Autoload\ClassLoader; use PHPStan\DependencyInjection\Container; use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator; -use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker; use Roave\BetterReflection\Reflector\FunctionReflector; use Roave\BetterReflection\SourceLocator\Ast\Locator; use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber; @@ -21,8 +19,6 @@ class TestCaseSourceLocatorFactory private Container $container; - private ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker; - private AutoloadSourceLocator $autoloadSourceLocator; private \PhpParser\Parser $phpParser; @@ -33,7 +29,6 @@ class TestCaseSourceLocatorFactory public function __construct( Container $container, - ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker, AutoloadSourceLocator $autoloadSourceLocator, \PhpParser\Parser $phpParser, PhpStormStubsSourceStubber $phpstormStubsSourceStubber, @@ -41,7 +36,6 @@ public function __construct( ) { $this->container = $container; - $this->composerJsonAndInstalledJsonSourceLocatorMaker = $composerJsonAndInstalledJsonSourceLocatorMaker; $this->autoloadSourceLocator = $autoloadSourceLocator; $this->phpParser = $phpParser; $this->phpstormStubsSourceStubber = $phpstormStubsSourceStubber; @@ -50,24 +44,7 @@ public function __construct( public function create(): SourceLocator { - $classLoaderReflection = new \ReflectionClass(ClassLoader::class); - if ($classLoaderReflection->getFileName() === false) { - throw new \PHPStan\ShouldNotHappenException('Unknown ClassLoader filename'); - } - - $composerProjectPath = dirname($classLoaderReflection->getFileName(), 3); - if (!is_file($composerProjectPath . '/composer.json')) { - throw new \PHPStan\ShouldNotHappenException(sprintf('composer.json not found in directory %s', $composerProjectPath)); - } - - $composerSourceLocator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerProjectPath); - if ($composerSourceLocator === null) { - throw new \PHPStan\ShouldNotHappenException('Could not create composer source locator'); - } - - $locators = [ - $composerSourceLocator, - ]; + $locators = []; $astLocator = new Locator($this->phpParser, function (): FunctionReflector { return $this->container->getService('testCaseFunctionReflector'); });