diff --git a/bin/phpstan b/bin/phpstan index de25ec2c0a..1a7a618c64 100755 --- a/bin/phpstan +++ b/bin/phpstan @@ -4,7 +4,6 @@ use PHPStan\Command\AnalyseCommand; use PHPStan\Command\ClearResultCacheCommand; use PHPStan\Command\FixerWorkerCommand; -use PHPStan\Command\DumpDependenciesCommand; use PHPStan\Command\WorkerCommand; use Symfony\Component\Console\Helper\ProgressBar; @@ -95,7 +94,6 @@ use Symfony\Component\Console\Helper\ProgressBar; $reversedComposerAutoloaderProjectPaths = array_reverse($composerAutoloaderProjectPaths); $application->add(new AnalyseCommand($reversedComposerAutoloaderProjectPaths)); - $application->add(new DumpDependenciesCommand($reversedComposerAutoloaderProjectPaths)); $application->add(new WorkerCommand($reversedComposerAutoloaderProjectPaths)); $application->add(new ClearResultCacheCommand($reversedComposerAutoloaderProjectPaths)); $application->add(new FixerWorkerCommand($reversedComposerAutoloaderProjectPaths)); diff --git a/conf/config.neon b/conf/config.neon index 1bcf0120bd..96066de357 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -533,11 +533,6 @@ services: arguments: parser: @regexParser - - - class: PHPStan\Dependency\DependencyDumper - arguments: - fileFinder: @fileFinderAnalyse - - class: PHPStan\Dependency\DependencyResolver diff --git a/src/Command/DumpDependenciesCommand.php b/src/Command/DumpDependenciesCommand.php deleted file mode 100644 index aa69b5900d..0000000000 --- a/src/Command/DumpDependenciesCommand.php +++ /dev/null @@ -1,130 +0,0 @@ -composerAutoloaderProjectPaths = $composerAutoloaderProjectPaths; - } - - protected function configure(): void - { - $this->setName(self::NAME) - ->setDescription('Dumps files dependency tree') - ->setDefinition([ - new InputArgument('paths', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Paths with source code to run dump on'), - new InputOption('paths-file', null, InputOption::VALUE_REQUIRED, 'Path to a file with a list of paths to run analysis on'), - new InputOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Path to project configuration file'), - new InputOption(ErrorsConsoleStyle::OPTION_NO_PROGRESS, null, InputOption::VALUE_NONE, 'Do not show progress bar, only results'), - new InputOption('autoload-file', 'a', InputOption::VALUE_REQUIRED, 'Project\'s additional autoload file path'), - new InputOption('memory-limit', null, InputOption::VALUE_REQUIRED, 'Memory limit for the run'), - new InputOption('analysed-paths', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Project-scope paths'), - new InputOption('xdebug', null, InputOption::VALUE_NONE, 'Allow running with XDebug for debugging purposes'), - ]); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - try { - /** @var string[] $paths */ - $paths = $input->getArgument('paths'); - - /** @var string|null $memoryLimit */ - $memoryLimit = $input->getOption('memory-limit'); - - /** @var string|null $autoloadFile */ - $autoloadFile = $input->getOption('autoload-file'); - - /** @var string|null $configurationFile */ - $configurationFile = $input->getOption('configuration'); - - /** @var string|null $pathsFile */ - $pathsFile = $input->getOption('paths-file'); - - /** @var bool $allowXdebug */ - $allowXdebug = $input->getOption('xdebug'); - - $inceptionResult = CommandHelper::begin( - $input, - $output, - $paths, - $pathsFile, - $memoryLimit, - $autoloadFile, - $this->composerAutoloaderProjectPaths, - $configurationFile, - null, - '0', // irrelevant but prevents an error when a config file is passed - $allowXdebug, - true - ); - } catch (\PHPStan\Command\InceptionNotSuccessfulException $e) { - return 1; - } - - try { - [$files] = $inceptionResult->getFiles(); - } catch (\PHPStan\File\PathNotFoundException $e) { - $inceptionResult->getErrorOutput()->writeLineFormatted(sprintf('%s', $e->getMessage())); - return 1; - } - - $stdOutput = $inceptionResult->getStdOutput(); - $stdOutputStyole = $stdOutput->getStyle(); - - /** @var DependencyDumper $dependencyDumper */ - $dependencyDumper = $inceptionResult->getContainer()->getByType(DependencyDumper::class); - - /** @var FileHelper $fileHelper */ - $fileHelper = $inceptionResult->getContainer()->getByType(FileHelper::class); - - /** @var string[] $analysedPaths */ - $analysedPaths = $input->getOption('analysed-paths'); - $analysedPaths = array_map(static function (string $path) use ($fileHelper): string { - return $fileHelper->absolutizePath($path); - }, $analysedPaths); - $dependencies = $dependencyDumper->dumpDependencies( - $files, - static function (int $count) use ($stdOutputStyole): void { - $stdOutputStyole->progressStart($count); - }, - static function () use ($stdOutputStyole): void { - $stdOutputStyole->progressAdvance(); - }, - count($analysedPaths) > 0 ? $analysedPaths : null - ); - $stdOutputStyole->progressFinish(); - - try { - $stdOutput->writeLineFormatted(Json::encode($dependencies, Json::PRETTY)); - } catch (\Nette\Utils\JsonException $e) { - $inceptionResult->getErrorOutput()->writeLineFormatted(sprintf('%s', $e->getMessage())); - return 1; - } - - return $inceptionResult->handleReturn(0); - } - -} diff --git a/src/Dependency/DependencyDumper.php b/src/Dependency/DependencyDumper.php deleted file mode 100644 index c7480dc01a..0000000000 --- a/src/Dependency/DependencyDumper.php +++ /dev/null @@ -1,97 +0,0 @@ -dependencyResolver = $dependencyResolver; - $this->nodeScopeResolver = $nodeScopeResolver; - $this->parser = $parser; - $this->scopeFactory = $scopeFactory; - $this->fileFinder = $fileFinder; - } - - /** - * @param string[] $files - * @param callable(int $count): void $countCallback - * @param callable(): void $progressCallback - * @param string[]|null $analysedPaths - * @return string[][] - */ - public function dumpDependencies( - array $files, - callable $countCallback, - callable $progressCallback, - ?array $analysedPaths - ): array - { - $analysedFiles = $files; - if ($analysedPaths !== null) { - $analysedFiles = $this->fileFinder->findFiles($analysedPaths)->getFiles(); - } - $this->nodeScopeResolver->setAnalysedFiles($analysedFiles); - $analysedFiles = array_fill_keys($analysedFiles, true); - - $dependencies = []; - $countCallback(count($files)); - foreach ($files as $file) { - try { - $parserNodes = $this->parser->parseFile($file); - } catch (\PHPStan\Parser\ParserErrorsException $e) { - continue; - } - - $fileDependencies = []; - try { - $this->nodeScopeResolver->processNodes( - $parserNodes, - $this->scopeFactory->create(ScopeContext::create($file)), - function (\PhpParser\Node $node, Scope $scope) use ($analysedFiles, &$fileDependencies): void { - $dependencies = $this->dependencyResolver->resolveDependencies($node, $scope); - $fileDependencies = array_merge( - $fileDependencies, - $dependencies->getFileDependencies($scope->getFile(), $analysedFiles) - ); - } - ); - } catch (\PHPStan\AnalysedCodeException $e) { - // pass - } - - foreach (array_unique($fileDependencies) as $fileDependency) { - $dependencies[$fileDependency][] = $file; - } - - $progressCallback(); - } - - return $dependencies; - } - -} diff --git a/tests/PHPStan/Dependency/DependencyDumperTest.php b/tests/PHPStan/Dependency/DependencyDumperTest.php deleted file mode 100644 index 7ed6cff27b..0000000000 --- a/tests/PHPStan/Dependency/DependencyDumperTest.php +++ /dev/null @@ -1,145 +0,0 @@ -getByType(NodeScopeResolver::class); - - /** @var Parser $realParser */ - $realParser = $container->getByType(Parser::class); - - $mockParser = $this->createMock(Parser::class); - $mockParser->method('parseFile') - ->willReturnCallback(static function (string $file) use ($realParser): array { - if (file_exists($file)) { - return $realParser->parseFile($file); - } - - return []; - }); - - /** @var Broker $realBroker */ - $realBroker = $container->getByType(Broker::class); - - $fileHelper = new FileHelper(__DIR__); - - $mockBroker = $this->createMock(Broker::class); - $mockBroker->method('getClass') - ->willReturnCallback(function (string $class) use ($realBroker, $fileHelper): ClassReflection { - if (in_array($class, [ - GrandChild::class, - Child::class, - ParentClass::class, - ], true)) { - return $realBroker->getClass($class); - } - - $nameParts = explode('\\', $class); - $shortClass = array_pop($nameParts); - - $classReflection = $this->createMock(ClassReflection::class); - $classReflection->method('getInterfaces')->willReturn([]); - $classReflection->method('getTraits')->willReturn([]); - $classReflection->method('getParentClass')->willReturn(false); - $classReflection->method('getFilename')->willReturn( - $fileHelper->normalizePath(__DIR__ . '/data/' . $shortClass . '.php') - ); - - return $classReflection; - }); - - $expectedDependencyTree = $this->getExpectedDependencyTree($fileHelper); - - /** @var ScopeFactory $scopeFactory */ - $scopeFactory = $container->getByType(ScopeFactory::class); - - /** @var FileFinder $fileFinder */ - $fileFinder = $container->getService('fileFinderAnalyse'); - - $dumper = new DependencyDumper( - new DependencyResolver($fileHelper, $mockBroker, new ExportedNodeResolver(self::getContainer()->getByType(FileTypeMapper::class), new Standard())), - $nodeScopeResolver, - $mockParser, - $scopeFactory, - $fileFinder - ); - - $dependencies = $dumper->dumpDependencies( - array_merge( - [$fileHelper->normalizePath(__DIR__ . '/data/GrandChild.php')], - array_keys($expectedDependencyTree) - ), - static function (): void { - }, - static function (): void { - }, - null - ); - - $this->assertCount(count($expectedDependencyTree), $dependencies); - foreach ($expectedDependencyTree as $file => $files) { - $this->assertArrayHasKey($file, $dependencies); - $this->assertSame($files, $dependencies[$file]); - } - } - - /** - * @param FileHelper $fileHelper - * @return string[][] - */ - private function getExpectedDependencyTree(FileHelper $fileHelper): array - { - $tree = [ - 'Child.php' => [ - 'GrandChild.php', - ], - 'Parent.php' => [ - 'GrandChild.php', - 'Child.php', - ], - 'MethodNativeReturnTypehint.php' => [ - 'GrandChild.php', - ], - 'MethodPhpDocReturnTypehint.php' => [ - 'GrandChild.php', - ], - 'ParamNativeReturnTypehint.php' => [ - 'GrandChild.php', - ], - 'ParamPhpDocReturnTypehint.php' => [ - 'GrandChild.php', - ], - ]; - - $expectedTree = []; - foreach ($tree as $file => $files) { - $expectedTree[$fileHelper->normalizePath(__DIR__ . '/data/' . $file)] = array_map(static function (string $file) use ($fileHelper): string { - return $fileHelper->normalizePath(__DIR__ . '/data/' . $file); - }, $files); - } - - return $expectedTree; - } - -} diff --git a/tests/PHPStan/Dependency/data/Child.php b/tests/PHPStan/Dependency/data/Child.php deleted file mode 100644 index 8e9e8a3495..0000000000 --- a/tests/PHPStan/Dependency/data/Child.php +++ /dev/null @@ -1,8 +0,0 @@ -