mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-14 12:29:43 +01:00
Add dynamic Rectors to DescribeCommand [closes #451]
This commit is contained in:
parent
10cdca6f95
commit
3c95af05f3
@ -29,6 +29,11 @@ final class Option
|
|||||||
*/
|
*/
|
||||||
public const OPTION_WITH_STYLE = 'with-style';
|
public const OPTION_WITH_STYLE = 'with-style';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public const OPTION_LEVEL = 'level';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
|
@ -2,11 +2,16 @@
|
|||||||
|
|
||||||
namespace Rector\Console\Command;
|
namespace Rector\Console\Command;
|
||||||
|
|
||||||
|
use Nette\Loaders\RobotLoader;
|
||||||
use Rector\Configuration\Option;
|
use Rector\Configuration\Option;
|
||||||
use Rector\Console\ConsoleStyle;
|
use Rector\Console\ConsoleStyle;
|
||||||
use Rector\Console\Output\DescribeCommandReporter;
|
use Rector\Console\Output\DescribeCommandReporter;
|
||||||
|
use Rector\Contract\Rector\RectorInterface;
|
||||||
use Rector\Guard\RectorGuard;
|
use Rector\Guard\RectorGuard;
|
||||||
use Rector\NodeTraverser\RectorNodeTraverser;
|
use Rector\NodeTraverser\RectorNodeTraverser;
|
||||||
|
use Rector\YamlRector\Contract\YamlRectorInterface;
|
||||||
|
use Rector\YamlRector\YamlFileProcessor;
|
||||||
|
use ReflectionClass;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
@ -50,11 +55,17 @@ final class DescribeCommand extends Command
|
|||||||
*/
|
*/
|
||||||
private $rectorGuard;
|
private $rectorGuard;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var YamlFileProcessor
|
||||||
|
*/
|
||||||
|
private $yamlFileProcessor;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ConsoleStyle $consoleStyle,
|
ConsoleStyle $consoleStyle,
|
||||||
RectorNodeTraverser $rectorNodeTraverser,
|
RectorNodeTraverser $rectorNodeTraverser,
|
||||||
DescribeCommandReporter $describeCommandReporter,
|
DescribeCommandReporter $describeCommandReporter,
|
||||||
RectorGuard $rectorGuard
|
RectorGuard $rectorGuard,
|
||||||
|
YamlFileProcessor $yamlFileProcessor
|
||||||
) {
|
) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
@ -62,6 +73,7 @@ final class DescribeCommand extends Command
|
|||||||
$this->rectorNodeTraverser = $rectorNodeTraverser;
|
$this->rectorNodeTraverser = $rectorNodeTraverser;
|
||||||
$this->describeCommandReporter = $describeCommandReporter;
|
$this->describeCommandReporter = $describeCommandReporter;
|
||||||
$this->rectorGuard = $rectorGuard;
|
$this->rectorGuard = $rectorGuard;
|
||||||
|
$this->yamlFileProcessor = $yamlFileProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure(): void
|
protected function configure(): void
|
||||||
@ -76,19 +88,64 @@ final class DescribeCommand extends Command
|
|||||||
{
|
{
|
||||||
$this->rectorGuard->ensureSomeRectorsAreRegistered();
|
$this->rectorGuard->ensureSomeRectorsAreRegistered();
|
||||||
|
|
||||||
|
$rectors = $this->getRectorsByInput($input);
|
||||||
|
|
||||||
$outputFormat = $input->getOption(self::OPTION_FORMAT);
|
$outputFormat = $input->getOption(self::OPTION_FORMAT);
|
||||||
|
|
||||||
if ($outputFormat === self::FORMAT_MARKDOWN) {
|
if ($outputFormat === self::FORMAT_MARKDOWN) {
|
||||||
$this->consoleStyle->writeln('# All Rectors Overview');
|
if ($input->getOption(Option::OPTION_LEVEL) === 'all') {
|
||||||
|
$headline = '# All Rectors Overview';
|
||||||
|
} else {
|
||||||
|
$headline = '# Rectors Overview';
|
||||||
|
}
|
||||||
|
$this->consoleStyle->writeln($headline);
|
||||||
$this->consoleStyle->newLine();
|
$this->consoleStyle->newLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->describeCommandReporter->reportRectorsInFormat(
|
$this->describeCommandReporter->reportRectorsInFormat(
|
||||||
$this->rectorNodeTraverser->getRectors(),
|
$rectors,
|
||||||
$outputFormat,
|
$outputFormat,
|
||||||
! $input->getOption(Option::OPTION_NO_DIFFS)
|
! $input->getOption(Option::OPTION_NO_DIFFS)
|
||||||
);
|
);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return RectorInterface[]|YamlRectorInterface[]
|
||||||
|
*/
|
||||||
|
private function getRectorsByInput(InputInterface $input): array
|
||||||
|
{
|
||||||
|
if ($input->getOption(Option::OPTION_LEVEL) === 'all') {
|
||||||
|
$robotLoader = $this->createRobotLoaderForAllRectors();
|
||||||
|
$robotLoader->rebuild();
|
||||||
|
|
||||||
|
$rectors = [];
|
||||||
|
foreach ($robotLoader->getIndexedClasses() as $class => $filename) {
|
||||||
|
$reflectionClass = new ReflectionClass($class);
|
||||||
|
if ($reflectionClass->isAbstract()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var RectorInterface|YamlRectorInterface $rector */
|
||||||
|
$rectors[] = $reflectionClass->newInstanceWithoutConstructor();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rectors;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->rectorNodeTraverser->getRectors() + $this->yamlFileProcessor->getYamlRectors();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createRobotLoaderForAllRectors(): RobotLoader
|
||||||
|
{
|
||||||
|
$robotLoader = new RobotLoader();
|
||||||
|
|
||||||
|
$robotLoader->addDirectory(__DIR__ . '/../../Rector');
|
||||||
|
$robotLoader->addDirectory(__DIR__ . '/../../../packages');
|
||||||
|
$robotLoader->setTempDirectory(sys_get_temp_dir() . '/_rector_finder');
|
||||||
|
$robotLoader->acceptFiles = '*Rector.php';
|
||||||
|
|
||||||
|
return $robotLoader;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ use Rector\Console\ConsoleStyle;
|
|||||||
use Rector\ConsoleDiffer\MarkdownDifferAndFormatter;
|
use Rector\ConsoleDiffer\MarkdownDifferAndFormatter;
|
||||||
use Rector\Contract\Rector\RectorInterface;
|
use Rector\Contract\Rector\RectorInterface;
|
||||||
use Rector\RectorDefinition\CodeSample;
|
use Rector\RectorDefinition\CodeSample;
|
||||||
|
use Rector\YamlRector\Contract\YamlRectorInterface;
|
||||||
|
|
||||||
final class DescribeCommandReporter
|
final class DescribeCommandReporter
|
||||||
{
|
{
|
||||||
@ -41,7 +42,10 @@ final class DescribeCommandReporter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function printWithCliFormat(int $i, bool $showDiffs, RectorInterface $rector): void
|
/**
|
||||||
|
* @param RectorInterface|YamlRectorInterface $rector
|
||||||
|
*/
|
||||||
|
private function printWithCliFormat(int $i, bool $showDiffs, $rector): void
|
||||||
{
|
{
|
||||||
$this->consoleStyle->section(sprintf('%d) %s', $i, get_class($rector)));
|
$this->consoleStyle->section(sprintf('%d) %s', $i, get_class($rector)));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user