Skip to content

Commit

Permalink
Merge pull request #24 from TomasVotruba/tv-add-skip-suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Oct 21, 2023
2 parents e440900 + eabf976 commit ed4843a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

<br>

Happy coding!
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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));
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion app/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
17 changes: 15 additions & 2 deletions app/Filtering/PossiblyUnusedClassesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ parameters:
- bin
- tests

cognitive_complexity:
class: 30
function: 10

excludePaths:
- */Fixture/*
- */Source/*

0 comments on commit ed4843a

Please sign in to comment.