From ac7666a4c943cf0fd8ad3b8a2ccc7541883d987c Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Fri, 4 Jun 2021 22:09:20 +0200 Subject: [PATCH] [FEATURE] Use OutputFormatter for show command (#147) --- .../Output/ConsoleOutputFormatter.php | 1 + .../Output/ShowOutputFormatterInterface.php | 18 ++++++ .../Output/ConsoleOutputFormatter.php | 39 +++++++++++++ phpstan.neon | 1 + src/Console/Command/ShowCommand.php | 48 ++++++++++------ .../Output/ShowOutputFormatterCollector.php | 55 +++++++++++++++++++ 6 files changed, 145 insertions(+), 17 deletions(-) create mode 100644 packages/ListReporting/Contract/Output/ShowOutputFormatterInterface.php create mode 100644 packages/ListReporting/Output/ConsoleOutputFormatter.php create mode 100644 src/Console/Output/ShowOutputFormatterCollector.php diff --git a/packages/ChangesReporting/Output/ConsoleOutputFormatter.php b/packages/ChangesReporting/Output/ConsoleOutputFormatter.php index 9c7464ff25b..0d3da6e32ad 100644 --- a/packages/ChangesReporting/Output/ConsoleOutputFormatter.php +++ b/packages/ChangesReporting/Output/ConsoleOutputFormatter.php @@ -10,6 +10,7 @@ use Rector\Core\Configuration\Configuration; use Rector\Core\Configuration\Option; use Rector\Core\Contract\Console\OutputStyleInterface; +use Rector\Core\Contract\Rector\RectorInterface; use Rector\Core\ValueObject\Application\RectorError; use Rector\Core\ValueObject\ProcessResult; use Rector\Core\ValueObject\Reporting\FileDiff; diff --git a/packages/ListReporting/Contract/Output/ShowOutputFormatterInterface.php b/packages/ListReporting/Contract/Output/ShowOutputFormatterInterface.php new file mode 100644 index 00000000000..06cd4902cc8 --- /dev/null +++ b/packages/ListReporting/Contract/Output/ShowOutputFormatterInterface.php @@ -0,0 +1,18 @@ +outputStyle->title('Loaded Rector rules'); + + foreach ($rectors as $rector) { + $this->outputStyle->writeln(' * ' . get_class($rector)); + } + + $message = sprintf('%d loaded Rectors', $rectorCount); + $this->outputStyle->success($message); + } + + public function getName(): string + { + return self::NAME; + } +} diff --git a/phpstan.neon b/phpstan.neon index 72bfa0d3c9c..cce458dc66f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -177,6 +177,7 @@ parameters: - src/Configuration/Configuration.php - src/Console/Command/ProcessCommand.php - src/Console/Command/InitCommand.php + - src/Console/Command/ShowCommand.php - '#Method (.*?) should return array but returns array#' - '#Parameter \#1 (.*?) expects Symfony\\Component\\DependencyInjection\\ContainerBuilder, Symfony\\Component\\DependencyInjection\\ContainerInterface given#' diff --git a/src/Console/Command/ShowCommand.php b/src/Console/Command/ShowCommand.php index 63916212840..e6f667b26ba 100644 --- a/src/Console/Command/ShowCommand.php +++ b/src/Console/Command/ShowCommand.php @@ -4,12 +4,16 @@ namespace Rector\Core\Console\Command; +use Rector\ChangesReporting\Output\ConsoleOutputFormatter; +use Rector\Core\Configuration\Option; +use Rector\Core\Console\Output\ShowOutputFormatterCollector; +use Rector\Core\Contract\Console\OutputStyleInterface; use Rector\Core\Contract\Rector\RectorInterface; use Rector\PostRector\Contract\Rector\PostRectorInterface; use Symfony\Component\Console\Command\Command; 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 Symplify\PackageBuilder\Console\ShellCode; final class ShowCommand extends Command @@ -18,7 +22,8 @@ final class ShowCommand extends Command * @param RectorInterface[] $rectors */ public function __construct( - private SymfonyStyle $symfonyStyle, + private OutputStyleInterface $outputStyle, + private ShowOutputFormatterCollector $showOutputFormatterCollector, private array $rectors ) { parent::__construct(); @@ -27,40 +32,49 @@ public function __construct( protected function configure(): void { $this->setDescription('Show loaded Rectors with their configuration'); + + $names = $this->showOutputFormatterCollector->getNames(); + + $description = sprintf('Select output format: "%s".', implode('", "', $names)); + $this->addOption( + Option::OPTION_OUTPUT_FORMAT, + 'o', + InputOption::VALUE_OPTIONAL, + $description, + ConsoleOutputFormatter::NAME + ); } protected function execute(InputInterface $input, OutputInterface $output): int { - $this->reportLoadedRectors(); + $outputFormat = (string)$input->getOption(Option::OPTION_OUTPUT_FORMAT); + + $this->reportLoadedRectors($outputFormat); return ShellCode::SUCCESS; } - private function reportLoadedRectors(): void + private function reportLoadedRectors(string $outputFormat): void { $rectors = array_filter( $this->rectors, - fn (RectorInterface $rector): bool => ! $rector instanceof PostRectorInterface + fn(RectorInterface $rector): bool => ! $rector instanceof PostRectorInterface ); $rectorCount = count($rectors); - if ($rectorCount > 0) { - $this->symfonyStyle->title('Loaded Rector rules'); - - foreach ($rectors as $rector) { - $this->symfonyStyle->writeln(' * ' . get_class($rector)); - } - - $message = sprintf('%d loaded Rectors', $rectorCount); - $this->symfonyStyle->success($message); - } else { + if ($rectorCount === 0) { $warningMessage = sprintf( 'No Rectors were loaded.%sAre sure your "rector.php" config is in the root?%sTry "--config " option to include it.', - PHP_EOL . PHP_EOL, + PHP_EOL.PHP_EOL, PHP_EOL ); - $this->symfonyStyle->warning($warningMessage); + $this->outputStyle->warning($warningMessage); + + return; } + + $outputFormatter = $this->showOutputFormatterCollector->getByName($outputFormat); + $outputFormatter->list($rectors); } } diff --git a/src/Console/Output/ShowOutputFormatterCollector.php b/src/Console/Output/ShowOutputFormatterCollector.php new file mode 100644 index 00000000000..c2344e35176 --- /dev/null +++ b/src/Console/Output/ShowOutputFormatterCollector.php @@ -0,0 +1,55 @@ +outputFormatters[$showOutputFormatter->getName()] = $showOutputFormatter; + } + } + + public function getByName(string $name): ShowOutputFormatterInterface + { + $this->ensureOutputFormatExists($name); + + return $this->outputFormatters[$name]; + } + + /** + * @return string[] + */ + public function getNames(): array + { + return array_keys($this->outputFormatters); + } + + private function ensureOutputFormatExists(string $name): void + { + if (isset($this->outputFormatters[$name])) { + return; + } + + throw new InvalidConfigurationException(sprintf( + 'Output formatter "%s" was not found. Pick one of "%s".', + $name, + implode('", "', $this->getNames()) + )); + } +}