Skip to content

Commit

Permalink
add --json option
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 9, 2023
1 parent 62b0174 commit de263cd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
]);

$rectorConfig->paths([
__DIR__ . '/app',
__DIR__ . '/src',
__DIR__ . '/tests',
]);

Expand Down
6 changes: 5 additions & 1 deletion src/Commands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->symfonyStyle->newLine();
}

$usedNames = $this->resolveUsedClassNames($phpFilePaths, function (): void {
$usedNames = $this->resolveUsedClassNames($phpFilePaths, function () use ($isJson): void {
if ($isJson) {
return;
}

$this->symfonyStyle->progressAdvance();
});

Expand Down
19 changes: 15 additions & 4 deletions 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 @@ -21,30 +22,40 @@ public function __construct(
*/
public function reportResult(UnusedClassesResult $unusedClassesResult, int $classCount, bool $isJson): int
{
if ($isJson) {
$jsonResult = [
'unused_class_count' => $unusedClassesResult->getCount(),
'unused_parent_less_classes' => $unusedClassesResult->getParentLessFileWithClasses(),
'unused_classes_with_parents' => $unusedClassesResult->getWithParentsFileWithClasses(),
];

$this->symfonyStyle->writeln(Json::encode($jsonResult, Json::PRETTY));

return Command::SUCCESS;
}

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

if ($unusedClassesResult->getCount() === 0) {
$this->symfonyStyle->success(sprintf('All the %d services are used. Great job!', $classCount));

return Command::SUCCESS;
}

// separate with and without parent, as first one can be removed more easily

if ($unusedClassesResult->getWithParentsFileWithClasses() !== []) {
$this->symfonyStyle->title('Classes with a parent/interface - possibly used by type');

$this->reportFileWithClasses($unusedClassesResult->getWithParentsFileWithClasses());
}

if ($unusedClassesResult->getParentLessFileWithClasses() !== []) {
$this->symfonyStyle->newLine();

$this->symfonyStyle->title('Classes without any parent/interface - easier to remove');

$this->reportFileWithClasses($unusedClassesResult->getParentLessFileWithClasses());
}

$this->symfonyStyle->newLine();

$this->symfonyStyle->error(sprintf(
'Found %d unused classes. Check and remove them or skip them using "--skip-type" option',
$unusedClassesResult->getCount()
Expand Down
14 changes: 13 additions & 1 deletion src/ValueObject/FileWithClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace TomasVotruba\ClassLeak\ValueObject;

use JsonSerializable;
use TomasVotruba\ClassLeak\FileSystem\StaticRelativeFilePathHelper;

final class FileWithClass
final class FileWithClass implements JsonSerializable
{
public function __construct(
private readonly string $filePath,
Expand All @@ -29,4 +30,15 @@ public function hasParentClassOrInterface(): bool
{
return $this->hasParentClassOrInterface;
}

/**
* @return array{file_path: string, class: string}
*/
public function jsonSerialize(): array
{
return [
'file_path' => $this->filePath,
'class' => $this->className,
];
}
}

0 comments on commit de263cd

Please sign in to comment.