Skip to content

Commit

Permalink
add dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 13, 2024
1 parent 4ccdb6e commit 0a35265
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Command/FinalizeClassesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;

Expand All @@ -40,6 +41,7 @@ protected function configure(): void
$this->setDescription('Finalize classes without children');

$this->addArgument('paths', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Directories to finalize');
$this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Do no change anything, only list classes about to be finalized');
}

/**
Expand All @@ -48,6 +50,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$paths = (array) $input->getArgument('paths');
$isDryRun = (array) $input->getOption('dry-run');

$this->symfonyStyle->title('1. Detecting parent and entity classes');

Expand Down Expand Up @@ -86,24 +89,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}

$this->symfonyStyle->writeln(sprintf('File "%s" was finalized', $phpFileInfo->getRelativePathname()));
$this->symfonyStyle->writeln(sprintf(
'File "%s" %s finalized',
$phpFileInfo->getRelativePathname(),
$isDryRun ? 'would be' : 'was'
));

$finalizedContents = Strings::replace(
$phpFileInfo->getContents(),
self::NEWLINE_CLASS_START_REGEX,
'final class '
);


$finalizedFilePaths[] = $phpFileInfo->getRelativePath();
FileSystem::write($phpFileInfo->getRealPath(), $finalizedContents);

if ($isDryRun === false) {

Check failure on line 107 in src/Command/FinalizeClassesCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan

Strict comparison using === between array and false will always evaluate to false.
FileSystem::write($phpFileInfo->getRealPath(), $finalizedContents);
}
}

if ($finalizedFilePaths === []) {
$this->symfonyStyle->success('Nothign to finalize');
$this->symfonyStyle->success('Nothing to finalize');
return self::SUCCESS;
}

$this->symfonyStyle->success(sprintf('%d classes were finalized', count($finalizedFilePaths)));
$this->symfonyStyle->success(sprintf(
'%d classes %s finalized',
count($finalizedFilePaths),
$isDryRun ? 'would be' : 'were'
));

return Command::SUCCESS;
}
Expand Down

0 comments on commit 0a35265

Please sign in to comment.