Skip to content

Commit

Permalink
add exclude-paths to find-multi-classes (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 15, 2024
1 parent 4534676 commit 0d6fa75
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/Command/FindMultiClassesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,16 +34,25 @@ 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
{
/** @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)));

Expand Down
5 changes: 3 additions & 2 deletions src/Finder/MultipleClassInOneFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ public function __construct(

/**
* @param string[] $directories
* @param string[] $excludedPaths
* @return array<string, string[]>
*/
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) {
Expand Down
20 changes: 17 additions & 3 deletions src/Finder/PhpFilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
5 changes: 4 additions & 1 deletion src/RobotLoader/PhpClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ final class PhpClassLoader
{
/**
* @param string[] $directories
* @param string[] $excludedPaths
* @return array<string, string>
*/
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();

Expand Down

0 comments on commit 0d6fa75

Please sign in to comment.