Skip to content

Commit 07ac1b5

Browse files
authored
Merge pull request #61 from staabm/json
Re-added --json option
2 parents 89461ff + 6bcb2ca commit 07ac1b5

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

src/Commands/CheckCommand.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace TomasVotruba\ClassLeak\Commands;
66

77
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Helper\ProgressBar;
89
use Symfony\Component\Console\Input\InputArgument;
910
use Symfony\Component\Console\Input\InputInterface;
1011
use Symfony\Component\Console\Input\InputOption;
@@ -83,6 +84,8 @@ protected function configure(): void
8384
'File extensions to check',
8485
['php']
8586
);
87+
88+
$this->addOption('json', null, InputOption::VALUE_NONE, 'Output as JSON');
8689
}
8790

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

110+
$isJson = (bool) $input->getOption('json');
111+
107112
/** @var string[] $fileExtensions */
108113
$fileExtensions = (array) $input->getOption('file-extension');
109114

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

112-
$this->symfonyStyle->title('1. Finding used classes');
113-
$usedNames = $this->resolveUsedClassNames($phpFilePaths);
117+
$progressBar = null;
118+
if (! $isJson) {
119+
$this->symfonyStyle->title('1. Finding used classes');
120+
$progressBar = $this->symfonyStyle->createProgressBar(count($phpFilePaths));
121+
}
122+
$usedNames = $this->resolveUsedClassNames($phpFilePaths, $progressBar);
114123

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

117-
$this->symfonyStyle->title('2. Extracting existing files with classes');
118-
119-
$existingFilesWithClasses = $this->classNamesFinder->resolveClassNamesToCheck($phpFilePaths);
126+
$progressBar = null;
127+
if (! $isJson) {
128+
$this->symfonyStyle->title('2. Extracting existing files with classes');
129+
$progressBar = $this->symfonyStyle->createProgressBar(count($phpFilePaths));
130+
}
131+
$existingFilesWithClasses = $this->classNamesFinder->resolveClassNamesToCheck($phpFilePaths, $progressBar);
120132

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

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

135-
return $this->unusedClassReporter->reportResult($unusedClassesResult);
147+
return $this->unusedClassReporter->reportResult($unusedClassesResult, $isJson);
136148
}
137149

138150
/**
139151
* @param string[] $phpFilePaths
140152
* @return string[]
141153
*/
142-
private function resolveUsedClassNames(array $phpFilePaths): array
154+
private function resolveUsedClassNames(array $phpFilePaths, ?ProgressBar $progressBar): array
143155
{
144-
$progressBar = $this->symfonyStyle->createProgressBar(count($phpFilePaths));
145-
146156
$usedNames = [];
147157

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

152-
$progressBar->advance();
162+
$progressBar?->advance();
153163
}
154164

155165
$usedNames = array_unique($usedNames);

src/Finder/ClassNamesFinder.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TomasVotruba\ClassLeak\Finder;
66

7+
use Symfony\Component\Console\Helper\ProgressBar;
78
use Symfony\Component\Console\Style\SymfonyStyle;
89
use TomasVotruba\ClassLeak\ClassNameResolver;
910
use TomasVotruba\ClassLeak\ValueObject\ClassNames;
@@ -12,22 +13,19 @@
1213
final readonly class ClassNamesFinder
1314
{
1415
public function __construct(
15-
private ClassNameResolver $classNameResolver,
16-
private SymfonyStyle $symfonyStyle
16+
private ClassNameResolver $classNameResolver
1717
) {
1818
}
1919

2020
/**
2121
* @param string[] $filePaths
2222
* @return FileWithClass[]
2323
*/
24-
public function resolveClassNamesToCheck(array $filePaths): array
24+
public function resolveClassNamesToCheck(array $filePaths, ?ProgressBar $progressBar): array
2525
{
26-
$progressBar = $this->symfonyStyle->createProgressBar(count($filePaths));
27-
2826
$filesWithClasses = [];
2927
foreach ($filePaths as $filePath) {
30-
$progressBar->advance();
28+
$progressBar?->advance();
3129

3230
$classNames = $this->classNameResolver->resolveFromFilePath($filePath);
3331
if (! $classNames instanceof ClassNames) {

src/Reporting/UnusedClassReporter.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TomasVotruba\ClassLeak\Reporting;
66

7+
use Nette\Utils\Json;
78
use Symfony\Component\Console\Command\Command;
89
use Symfony\Component\Console\Style\SymfonyStyle;
910
use TomasVotruba\ClassLeak\ValueObject\FileWithClass;
@@ -19,8 +20,20 @@ public function __construct(
1920
/**
2021
* @return Command::*
2122
*/
22-
public function reportResult(UnusedClassesResult $unusedClassesResult): int
23+
public function reportResult(UnusedClassesResult $unusedClassesResult, bool $isJson): int
2324
{
25+
if ($isJson) {
26+
$jsonResult = [
27+
'unused_class_count' => $unusedClassesResult->getCount(),
28+
'unused_parent_less_classes' => $unusedClassesResult->getParentLessFileWithClasses(),
29+
'unused_classes_with_parents' => $unusedClassesResult->getWithParentsFileWithClasses(),
30+
'unused_traits' => $unusedClassesResult->getTraits(),
31+
];
32+
$this->symfonyStyle->writeln(Json::encode($jsonResult, Json::PRETTY));
33+
34+
return Command::SUCCESS;
35+
}
36+
2437
$this->symfonyStyle->newLine(2);
2538

2639
if ($unusedClassesResult->getCount() === 0) {

0 commit comments

Comments
 (0)