Skip to content

Commit

Permalink
[FEATURE] Use OutputFormatter for show command (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Jun 4, 2021
1 parent 4f33f3f commit ac7666a
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);


namespace Rector\ListReporting\Contract\Output;


use Rector\Core\Contract\Rector\RectorInterface;

interface ShowOutputFormatterInterface
{
/**
* @param RectorInterface[] $rectors
*/
public function list(array $rectors): void;

public function getName(): string;
}
39 changes: 39 additions & 0 deletions packages/ListReporting/Output/ConsoleOutputFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);


namespace Rector\ListReporting\Output;


use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\ListReporting\Contract\Output\ShowOutputFormatterInterface;

final class ConsoleOutputFormatter implements ShowOutputFormatterInterface
{
/**
* @var string
*/
public const NAME = 'console';

public function __construct(private OutputStyleInterface $outputStyle)
{
}

public function list(array $rectors): void
{
$rectorCount = count($rectors);
$this->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;
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhpParser\\Node\\(.*?)\> but returns array<PhpParser\\Node\>#'
- '#Parameter \#1 (.*?) expects Symfony\\Component\\DependencyInjection\\ContainerBuilder, Symfony\\Component\\DependencyInjection\\ContainerInterface given#'
Expand Down
48 changes: 31 additions & 17 deletions src/Console/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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 <path>" 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);
}
}
55 changes: 55 additions & 0 deletions src/Console/Output/ShowOutputFormatterCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);


namespace Rector\Core\Console\Output;


use Rector\ListReporting\Contract\Output\ShowOutputFormatterInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

final class ShowOutputFormatterCollector
{
/**
* @var ShowOutputFormatterInterface[]
*/
private array $outputFormatters = [];

/**
* @param ShowOutputFormatterInterface[] $showOutputFormatters
*/
public function __construct(array $showOutputFormatters)
{
foreach ($showOutputFormatters as $showOutputFormatter) {
$this->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())
));
}
}

0 comments on commit ac7666a

Please sign in to comment.