-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed4843a
commit d9942db
Showing
641 changed files
with
60,270 additions
and
880 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
declare (strict_types=1); | ||
namespace TomasVotruba\ClassLeak; | ||
|
||
use PhpParser\NodeTraverser; | ||
use PhpParser\Parser; | ||
use ClassLeak202310\PhpParser\NodeTraverser; | ||
use ClassLeak202310\PhpParser\Parser; | ||
use TomasVotruba\ClassLeak\NodeDecorator\FullyQualifiedNameNodeDecorator; | ||
use TomasVotruba\ClassLeak\NodeVisitor\ClassNameNodeVisitor; | ||
|
||
/** | ||
* @see \TomasVotruba\ClassLeak\Tests\ClassNameResolver\ClassNameResolverTest | ||
*/ | ||
final class ClassNameResolver | ||
{ | ||
public function __construct( | ||
private readonly Parser $parser, | ||
private readonly FullyQualifiedNameNodeDecorator $fullyQualifiedNameNodeDecorator | ||
) { | ||
/** | ||
* @readonly | ||
* @var \PhpParser\Parser | ||
*/ | ||
private $parser; | ||
/** | ||
* @readonly | ||
* @var \TomasVotruba\ClassLeak\NodeDecorator\FullyQualifiedNameNodeDecorator | ||
*/ | ||
private $fullyQualifiedNameNodeDecorator; | ||
public function __construct(Parser $parser, FullyQualifiedNameNodeDecorator $fullyQualifiedNameNodeDecorator) | ||
{ | ||
$this->parser = $parser; | ||
$this->fullyQualifiedNameNodeDecorator = $fullyQualifiedNameNodeDecorator; | ||
} | ||
|
||
public function resolveFromFromFilePath(string $filePath): ?string | ||
public function resolveFromFromFilePath(string $filePath) : ?string | ||
{ | ||
/** @var string $fileContents */ | ||
$fileContents = file_get_contents($filePath); | ||
|
||
$fileContents = \file_get_contents($filePath); | ||
$stmts = $this->parser->parse($fileContents); | ||
if ($stmts === null) { | ||
return null; | ||
} | ||
|
||
$this->fullyQualifiedNameNodeDecorator->decorate($stmts); | ||
|
||
$classNameNodeVisitor = new ClassNameNodeVisitor(); | ||
$nodeTraverser = new NodeTraverser(); | ||
$nodeTraverser->addVisitor($classNameNodeVisitor); | ||
$nodeTraverser->traverse($stmts); | ||
|
||
return $classNameNodeVisitor->getClassName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
declare (strict_types=1); | ||
namespace TomasVotruba\ClassLeak\Commands; | ||
|
||
use Closure; | ||
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; | ||
use ClassLeak202310\Symfony\Component\Console\Command\Command; | ||
use ClassLeak202310\Symfony\Component\Console\Input\InputArgument; | ||
use ClassLeak202310\Symfony\Component\Console\Input\InputInterface; | ||
use ClassLeak202310\Symfony\Component\Console\Input\InputOption; | ||
use ClassLeak202310\Symfony\Component\Console\Output\OutputInterface; | ||
use ClassLeak202310\Symfony\Component\Console\Style\SymfonyStyle; | ||
use TomasVotruba\ClassLeak\Filtering\PossiblyUnusedClassesFilter; | ||
use TomasVotruba\ClassLeak\Finder\ClassNamesFinder; | ||
use TomasVotruba\ClassLeak\Finder\PhpFilesFinder; | ||
use TomasVotruba\ClassLeak\Reporting\UnusedClassReporter; | ||
use TomasVotruba\ClassLeak\UseImportsResolver; | ||
|
||
final class CheckCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly ClassNamesFinder $classNamesFinder, | ||
private readonly UseImportsResolver $useImportsResolver, | ||
private readonly PossiblyUnusedClassesFilter $possiblyUnusedClassesFilter, | ||
private readonly UnusedClassReporter $unusedClassReporter, | ||
private readonly SymfonyStyle $symfonyStyle, | ||
private readonly PhpFilesFinder $phpFilesFinder, | ||
) { | ||
/** | ||
* @readonly | ||
* @var \TomasVotruba\ClassLeak\Finder\ClassNamesFinder | ||
*/ | ||
private $classNamesFinder; | ||
/** | ||
* @readonly | ||
* @var \TomasVotruba\ClassLeak\UseImportsResolver | ||
*/ | ||
private $useImportsResolver; | ||
/** | ||
* @readonly | ||
* @var \TomasVotruba\ClassLeak\Filtering\PossiblyUnusedClassesFilter | ||
*/ | ||
private $possiblyUnusedClassesFilter; | ||
/** | ||
* @readonly | ||
* @var \TomasVotruba\ClassLeak\Reporting\UnusedClassReporter | ||
*/ | ||
private $unusedClassReporter; | ||
/** | ||
* @readonly | ||
* @var \Symfony\Component\Console\Style\SymfonyStyle | ||
*/ | ||
private $symfonyStyle; | ||
/** | ||
* @readonly | ||
* @var \TomasVotruba\ClassLeak\Finder\PhpFilesFinder | ||
*/ | ||
private $phpFilesFinder; | ||
public function __construct(ClassNamesFinder $classNamesFinder, UseImportsResolver $useImportsResolver, PossiblyUnusedClassesFilter $possiblyUnusedClassesFilter, UnusedClassReporter $unusedClassReporter, SymfonyStyle $symfonyStyle, PhpFilesFinder $phpFilesFinder) | ||
{ | ||
$this->classNamesFinder = $classNamesFinder; | ||
$this->useImportsResolver = $useImportsResolver; | ||
$this->possiblyUnusedClassesFilter = $possiblyUnusedClassesFilter; | ||
$this->unusedClassReporter = $unusedClassReporter; | ||
$this->symfonyStyle = $symfonyStyle; | ||
$this->phpFilesFinder = $phpFilesFinder; | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
protected function configure() : void | ||
{ | ||
$this->setName('check'); | ||
$this->setDescription('Check classes that are not used in any config and in the code'); | ||
|
||
$this->addArgument( | ||
'paths', | ||
InputArgument::REQUIRED | InputArgument::IS_ARRAY, | ||
'Files and directories to analyze' | ||
); | ||
$this->addOption( | ||
'skip-type', | ||
null, | ||
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' | ||
); | ||
$this->addArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Files and directories to analyze'); | ||
$this->addOption('skip-type', null, 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 | ||
protected function execute(InputInterface $input, OutputInterface $output) : int | ||
{ | ||
/** @var string[] $paths */ | ||
$paths = (array) $input->getArgument('paths'); | ||
|
||
/** @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)); | ||
$this->symfonyStyle->progressStart(\count($phpFilePaths)); | ||
$this->symfonyStyle->newLine(); | ||
|
||
$usedNames = $this->resolveUsedClassNames($phpFilePaths, function (): void { | ||
$usedNames = $this->resolveUsedClassNames($phpFilePaths, function () : void { | ||
$this->symfonyStyle->progressAdvance(); | ||
}); | ||
|
||
$existingFilesWithClasses = $this->classNamesFinder->resolveClassNamesToCheck($phpFilePaths); | ||
|
||
$possiblyUnusedFilesWithClasses = $this->possiblyUnusedClassesFilter->filter( | ||
$existingFilesWithClasses, | ||
$usedNames, | ||
$typesToSkip, | ||
$suffixesToSkip | ||
); | ||
|
||
return $this->unusedClassReporter->reportResult( | ||
$possiblyUnusedFilesWithClasses, | ||
$existingFilesWithClasses | ||
); | ||
$possiblyUnusedFilesWithClasses = $this->possiblyUnusedClassesFilter->filter($existingFilesWithClasses, $usedNames, $typesToSkip, $suffixesToSkip); | ||
return $this->unusedClassReporter->reportResult($possiblyUnusedFilesWithClasses, $existingFilesWithClasses); | ||
} | ||
|
||
/** | ||
* @param string[] $phpFilePaths | ||
* @return string[] | ||
*/ | ||
private function resolveUsedClassNames(array $phpFilePaths, Closure $progressClosure): array | ||
private function resolveUsedClassNames(array $phpFilePaths, Closure $progressClosure) : array | ||
{ | ||
$usedNames = []; | ||
|
||
foreach ($phpFilePaths as $phpFilePath) { | ||
$currentUsedNames = $this->useImportsResolver->resolve($phpFilePath); | ||
$usedNames = array_merge($usedNames, $currentUsedNames); | ||
|
||
$usedNames = \array_merge($usedNames, $currentUsedNames); | ||
$progressClosure(); | ||
} | ||
|
||
$usedNames = array_unique($usedNames); | ||
sort($usedNames); | ||
|
||
$usedNames = \array_unique($usedNames); | ||
\sort($usedNames); | ||
return $usedNames; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
declare (strict_types=1); | ||
namespace TomasVotruba\ClassLeak\FileSystem; | ||
|
||
/** | ||
* @see \TomasVotruba\ClassLeak\Tests\FileSystem\StaticRelativeFilePathHelperTest | ||
*/ | ||
final class StaticRelativeFilePathHelper | ||
{ | ||
public static function resolveFromCwd(string $filePath): string | ||
public static function resolveFromCwd(string $filePath) : string | ||
{ | ||
// make path relative with native PHP | ||
$relativeFilePath = (string) realpath($filePath); | ||
return str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $relativeFilePath); | ||
$relativeFilePath = (string) \realpath($filePath); | ||
return \str_replace(\getcwd() . \DIRECTORY_SEPARATOR, '', $relativeFilePath); | ||
} | ||
} |
Oops, something went wrong.