Skip to content

Commit

Permalink
Re-added --json option
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jan 7, 2025
1 parent 89461ff commit 022697d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
30 changes: 20 additions & 10 deletions src/Commands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace TomasVotruba\ClassLeak\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -83,6 +84,8 @@ protected function configure(): void
'File extensions to check',
['php']
);

$this->addOption('json', null, InputOption::VALUE_NONE, 'Output as JSON');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -104,19 +107,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var string[] $pathsToSkip */
$pathsToSkip = (array) $input->getOption('skip-path');

$isJson = (bool) $input->getOption('json');

/** @var string[] $fileExtensions */
$fileExtensions = (array) $input->getOption('file-extension');

$phpFilePaths = $this->phpFilesFinder->findPhpFiles($paths, $fileExtensions, $pathsToSkip);

$this->symfonyStyle->title('1. Finding used classes');
$usedNames = $this->resolveUsedClassNames($phpFilePaths);
$progressBar = null;
if (! $isJson) {
$this->symfonyStyle->title('1. Finding used classes');
$progressBar = $this->symfonyStyle->createProgressBar(count($phpFilePaths));
}
$usedNames = $this->resolveUsedClassNames($phpFilePaths, $progressBar);

$this->symfonyStyle->newLine(2);

$this->symfonyStyle->title('2. Extracting existing files with classes');

$existingFilesWithClasses = $this->classNamesFinder->resolveClassNamesToCheck($phpFilePaths);
$progressBar = null;
if (! $isJson) {
$this->symfonyStyle->title('2. Extracting existing files with classes');
$progressBar = $this->symfonyStyle->createProgressBar(count($phpFilePaths));
}
$existingFilesWithClasses = $this->classNamesFinder->resolveClassNamesToCheck($phpFilePaths, $progressBar);

$this->symfonyStyle->newLine(2);

Expand All @@ -132,24 +144,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$unusedClassesResult = $this->unusedClassesResultFactory->create($possiblyUnusedFilesWithClasses);
$this->symfonyStyle->newLine();

return $this->unusedClassReporter->reportResult($unusedClassesResult);
return $this->unusedClassReporter->reportResult($unusedClassesResult, $isJson);
}

/**
* @param string[] $phpFilePaths
* @return string[]
*/
private function resolveUsedClassNames(array $phpFilePaths): array
private function resolveUsedClassNames(array $phpFilePaths, ?ProgressBar $progressBar): array
{
$progressBar = $this->symfonyStyle->createProgressBar(count($phpFilePaths));

$usedNames = [];

foreach ($phpFilePaths as $phpFilePath) {
$currentUsedNames = $this->useImportsResolver->resolve($phpFilePath);
$usedNames = [...$usedNames, ...$currentUsedNames];

$progressBar->advance();
$progressBar?->advance();
}

$usedNames = array_unique($usedNames);
Expand Down
7 changes: 3 additions & 4 deletions src/Finder/ClassNamesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TomasVotruba\ClassLeak\Finder;

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Style\SymfonyStyle;
use TomasVotruba\ClassLeak\ClassNameResolver;
use TomasVotruba\ClassLeak\ValueObject\ClassNames;
Expand All @@ -21,13 +22,11 @@ public function __construct(
* @param string[] $filePaths
* @return FileWithClass[]
*/
public function resolveClassNamesToCheck(array $filePaths): array
public function resolveClassNamesToCheck(array $filePaths, ?ProgressBar $progressBar): array
{
$progressBar = $this->symfonyStyle->createProgressBar(count($filePaths));

$filesWithClasses = [];
foreach ($filePaths as $filePath) {
$progressBar->advance();
$progressBar?->advance();

$classNames = $this->classNameResolver->resolveFromFilePath($filePath);
if (! $classNames instanceof ClassNames) {
Expand Down
15 changes: 14 additions & 1 deletion src/Reporting/UnusedClassReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TomasVotruba\ClassLeak\Reporting;

use Nette\Utils\Json;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use TomasVotruba\ClassLeak\ValueObject\FileWithClass;
Expand All @@ -19,8 +20,20 @@ public function __construct(
/**
* @return Command::*
*/
public function reportResult(UnusedClassesResult $unusedClassesResult): int
public function reportResult(UnusedClassesResult $unusedClassesResult, bool $isJson): int
{
if ($isJson) {
$jsonResult = [
'unused_class_count' => $unusedClassesResult->getCount(),
'unused_parent_less_classes' => $unusedClassesResult->getParentLessFileWithClasses(),
'unused_classes_with_parents' => $unusedClassesResult->getWithParentsFileWithClasses(),
'unused_traits' => $unusedClassesResult->getTraits(),
];
$this->symfonyStyle->writeln(Json::encode($jsonResult, Json::PRETTY));

return Command::SUCCESS;
}

$this->symfonyStyle->newLine(2);

if ($unusedClassesResult->getCount() === 0) {
Expand Down

0 comments on commit 022697d

Please sign in to comment.