From eabf976d622d4a18fbe0c5aea4e8dc5039ec03a6 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 21 Oct 2023 22:34:08 +0200 Subject: [PATCH] Add --skip-suffix --- README.md | 10 ++++++++++ app/{Console => }/Commands/CheckCommand.php | 15 +++++++++++++-- app/DependencyInjection/ContainerFactory.php | 2 +- app/Filtering/PossiblyUnusedClassesFilter.php | 17 +++++++++++++++-- composer.json | 1 - phpstan.neon | 4 ---- 6 files changed, 39 insertions(+), 10 deletions(-) rename app/{Console => }/Commands/CheckCommand.php (88%) diff --git a/README.md b/README.md index ec20b627..bda987ba 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,13 @@ Many types are excluded by default, as they're collected by framework magic, e.g ```bash vendor/bin/class-leak check bin src --skip-type="App\\Contract\\SomeInterface" ``` + +What if your classes do no implement any type? Use `--skip-suffix` instead: + +```bash +vendor/bin/class-leak check bin src --skip-suffix "Controller" +``` + +
+ +Happy coding! diff --git a/app/Console/Commands/CheckCommand.php b/app/Commands/CheckCommand.php similarity index 88% rename from app/Console/Commands/CheckCommand.php rename to app/Commands/CheckCommand.php index 674ec430..a047c67f 100644 --- a/app/Console/Commands/CheckCommand.php +++ b/app/Commands/CheckCommand.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace TomasVotruba\ClassLeak\Console\Commands; +namespace TomasVotruba\ClassLeak\Commands; use Closure; use Symfony\Component\Console\Command\Command; @@ -46,6 +46,13 @@ protected function configure(): void InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Class types that should be skipped' ); + + $this->addOption( + 'skip-suffix', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'Class suffix that should be skipped' + ); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -56,6 +63,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** @var string[] $typesToSkip */ $typesToSkip = (array) $input->getOption('skip-type'); + /** @var string[] $suffixesToSkip */ + $suffixesToSkip = (array) $input->getOption('skip-suffix'); + $phpFilePaths = $this->phpFilesFinder->findPhpFiles($paths); $this->symfonyStyle->progressStart(count($phpFilePaths)); @@ -70,7 +80,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $possiblyUnusedFilesWithClasses = $this->possiblyUnusedClassesFilter->filter( $existingFilesWithClasses, $usedNames, - $typesToSkip + $typesToSkip, + $suffixesToSkip ); return $this->unusedClassReporter->reportResult( diff --git a/app/DependencyInjection/ContainerFactory.php b/app/DependencyInjection/ContainerFactory.php index f29f2d7f..42f207b9 100644 --- a/app/DependencyInjection/ContainerFactory.php +++ b/app/DependencyInjection/ContainerFactory.php @@ -12,7 +12,7 @@ use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Style\SymfonyStyle; -use TomasVotruba\ClassLeak\Console\Commands\CheckCommand; +use TomasVotruba\ClassLeak\Commands\CheckCommand; final class ContainerFactory { diff --git a/app/Filtering/PossiblyUnusedClassesFilter.php b/app/Filtering/PossiblyUnusedClassesFilter.php index c82b999e..831be536 100644 --- a/app/Filtering/PossiblyUnusedClassesFilter.php +++ b/app/Filtering/PossiblyUnusedClassesFilter.php @@ -45,13 +45,19 @@ final class PossiblyUnusedClassesFilter * @param FileWithClass[] $filesWithClasses * @param string[] $usedClassNames * @param string[] $typesToSkip + * @param string[] $suffixesToSkip * * @return FileWithClass[] */ - public function filter(array $filesWithClasses, array $usedClassNames, array $typesToSkip): array - { + public function filter( + array $filesWithClasses, + array $usedClassNames, + array $typesToSkip, + array $suffixesToSkip + ): array { Assert::allString($usedClassNames); Assert::allString($typesToSkip); + Assert::allString($suffixesToSkip); $possiblyUnusedFilesWithClasses = []; @@ -69,6 +75,13 @@ public function filter(array $filesWithClasses, array $usedClassNames, array $ty } } + // is excluded suffix? + foreach ($suffixesToSkip as $suffixToSkip) { + if (str_ends_with($fileWithClass->getClassName(), $suffixToSkip)) { + continue 2; + } + } + $possiblyUnusedFilesWithClasses[] = $fileWithClass; } diff --git a/composer.json b/composer.json index 33039ceb..4b43fec4 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,6 @@ "rector/rector": "^0.18", "symplify/easy-coding-standard": "^12.0", "symplify/phpstan-extensions": "^11.2", - "tomasvotruba/cognitive-complexity": "^0.1", "tomasvotruba/type-coverage": "^0.2", "tomasvotruba/unused-public": "^0.2", "tracy/tracy": "^2.10" diff --git a/phpstan.neon b/phpstan.neon index 0b725714..431d17d5 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,10 +6,6 @@ parameters: - bin - tests - cognitive_complexity: - class: 30 - function: 10 - excludePaths: - */Fixture/* - */Source/*