rector/src/Reporting/MissingRectorRulesReporter.php
Tomas Votruba 764b0a2692 Updated Rector to commit cb5b01223d46272004e947f122ae1e36d516f83a
cb5b01223d [automated] Re-Generate Nodes/Rectors Documentation (#3259)
2023-01-01 00:36:31 +00:00

62 lines
2.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Core\Reporting;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\PostRector\Contract\Rector\ComplementaryRectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use RectorPrefix202301\Symfony\Component\Console\Command\Command;
final class MissingRectorRulesReporter
{
/**
* @var RectorInterface[]
* @readonly
*/
private $rectors;
/**
* @readonly
* @var \Rector\Core\Contract\Console\OutputStyleInterface
*/
private $rectorOutputStyle;
/**
* @param RectorInterface[] $rectors
*/
public function __construct(array $rectors, OutputStyleInterface $rectorOutputStyle)
{
$this->rectors = $rectors;
$this->rectorOutputStyle = $rectorOutputStyle;
}
public function reportIfMissing() : ?int
{
if ($this->filterActiveRectors($this->rectors) !== []) {
return null;
}
$this->rectorOutputStyle->warning('We could not find any Rector rules to run. You have 2 options to add them:');
$this->rectorOutputStyle->title('1. Add single rule to "rector.php"');
$this->rectorOutputStyle->writeln(' $rectorConfig->rule(...);');
$this->rectorOutputStyle->newLine(1);
$this->rectorOutputStyle->title('2. Add set of rules to "rector.php"');
$this->rectorOutputStyle->writeln(' $rectorConfig->sets([SetList::...]);');
$this->rectorOutputStyle->newLine(1);
$this->rectorOutputStyle->title('Missing "rector.php" in your project? Let Rector create it for you');
$this->rectorOutputStyle->writeln(' vendor/bin/rector init');
$this->rectorOutputStyle->newLine();
return Command::FAILURE;
}
/**
* @param RectorInterface[] $rectors
* @return RectorInterface[]
*/
private function filterActiveRectors(array $rectors) : array
{
return \array_filter($rectors, static function (RectorInterface $rector) : bool {
if ($rector instanceof PostRectorInterface) {
return \false;
}
return !$rector instanceof ComplementaryRectorInterface;
});
}
}