From 0d6fa752e447bc3a64abcd9604a7ff0fc1679a8f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 16 Feb 2024 00:27:52 +0100 Subject: [PATCH] add exclude-paths to find-multi-classes (#8) --- src/Command/FindMultiClassesCommand.php | 14 ++++++++++++-- src/Finder/MultipleClassInOneFileFinder.php | 5 +++-- src/Finder/PhpFilesFinder.php | 20 +++++++++++++++++--- src/RobotLoader/PhpClassLoader.php | 5 ++++- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/Command/FindMultiClassesCommand.php b/src/Command/FindMultiClassesCommand.php index 065816a7bf..bab77c1e86 100644 --- a/src/Command/FindMultiClassesCommand.php +++ b/src/Command/FindMultiClassesCommand.php @@ -10,6 +10,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -33,6 +34,13 @@ protected function configure(): void InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Path to source to analyse' ); + + $this->addOption( + 'exclude-path', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'Path to exclude' + ); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -40,9 +48,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** @var string[] $source */ $source = $input->getArgument('sources'); - $phpFileInfos = PhpFilesFinder::find($source); + $excludedPaths = (array) $input->getOption('exclude-path'); + + $phpFileInfos = PhpFilesFinder::find($source, $excludedPaths); - $multipleClassesByFile = $this->multipleClassInOneFileFinder->findInDirectories($source); + $multipleClassesByFile = $this->multipleClassInOneFileFinder->findInDirectories($source, $excludedPaths); if ($multipleClassesByFile === []) { $this->symfonyStyle->success(sprintf('No file with 2+ classes found in %d files', count($phpFileInfos))); diff --git a/src/Finder/MultipleClassInOneFileFinder.php b/src/Finder/MultipleClassInOneFileFinder.php index d719a05517..9c18d123e6 100644 --- a/src/Finder/MultipleClassInOneFileFinder.php +++ b/src/Finder/MultipleClassInOneFileFinder.php @@ -15,11 +15,12 @@ public function __construct( /** * @param string[] $directories + * @param string[] $excludedPaths * @return array */ - public function findInDirectories(array $directories): array + public function findInDirectories(array $directories, array $excludedPaths): array { - $fileByClasses = $this->phpClassLoader->load($directories); + $fileByClasses = $this->phpClassLoader->load($directories, $excludedPaths); $classesByFile = []; foreach ($fileByClasses as $class => $filePath) { diff --git a/src/Finder/PhpFilesFinder.php b/src/Finder/PhpFilesFinder.php index 2a4b7ba1f4..df032580d6 100644 --- a/src/Finder/PhpFilesFinder.php +++ b/src/Finder/PhpFilesFinder.php @@ -12,18 +12,32 @@ final class PhpFilesFinder { /** * @param string[] $paths + * @param string[] $excludedPaths * @return SplFileInfo[] */ - public static function find(array $paths): array + public static function find(array $paths, array $excludedPaths = []): array { Assert::allString($paths); Assert::allFileExists($paths); + Assert::allString($excludedPaths); + Assert::allFileExists($excludedPaths); + $finder = Finder::create() ->files() ->in($paths) - ->name('*.php'); + ->name('*.php') + // exclude paths, as notPaths() does no work + ->filter(function (SplFileInfo $splFileInfo) use ($excludedPaths): bool { + foreach ($excludedPaths as $excludedPath) { + if (str_contains($splFileInfo->getRealPath(), $excludedPath)) { + return false; + } + } + + return true; + }); - return iterator_to_array($finder); + return iterator_to_array($finder->getIterator()); } } diff --git a/src/RobotLoader/PhpClassLoader.php b/src/RobotLoader/PhpClassLoader.php index 078ae4b5bd..9b91c0f73f 100644 --- a/src/RobotLoader/PhpClassLoader.php +++ b/src/RobotLoader/PhpClassLoader.php @@ -8,12 +8,15 @@ final class PhpClassLoader { /** * @param string[] $directories + * @param string[] $excludedPaths * @return array */ - public function load(array $directories): array + public function load(array $directories, array $excludedPaths): array { $robotLoader = new RobotLoader(); $robotLoader->addDirectory(...$directories); + $robotLoader->excludeDirectory(...$excludedPaths); + $robotLoader->setTempDirectory(sys_get_temp_dir() . '/multiple-classes'); $robotLoader->rebuild();