Add AfterRectorCodingStyle

This commit is contained in:
Tomas Votruba 2018-10-21 11:50:31 +02:00
parent 161c3cd6fa
commit 985d38e267
3 changed files with 64 additions and 49 deletions

View File

@ -8,6 +8,7 @@ use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Name\FullyQualified;
use Rector\CodingStyle\AfterRectorCodingStyle;
use Rector\Console\ConsoleStyle; use Rector\Console\ConsoleStyle;
use Rector\ContributorTools\Configuration\Configuration; use Rector\ContributorTools\Configuration\Configuration;
use Rector\ContributorTools\Configuration\ConfigurationFactory; use Rector\ContributorTools\Configuration\ConfigurationFactory;
@ -16,7 +17,6 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
use Symplify\PackageBuilder\Console\Command\CommandNaming; use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode; use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\PackageBuilder\FileSystem\FinderSanitizer; use Symplify\PackageBuilder\FileSystem\FinderSanitizer;
@ -57,17 +57,24 @@ final class CreateRectorCommand extends Command
*/ */
private $generatedFiles = []; private $generatedFiles = [];
/**
* @var AfterRectorCodingStyle
*/
private $afterRectorCodingStyle;
public function __construct( public function __construct(
ConsoleStyle $consoleStyle, ConsoleStyle $consoleStyle,
ConfigurationFactory $configurationFactory, ConfigurationFactory $configurationFactory,
BetterStandardPrinter $betterStandardPrinter, BetterStandardPrinter $betterStandardPrinter,
FinderSanitizer $finderSanitizer FinderSanitizer $finderSanitizer,
AfterRectorCodingStyle $afterRectorCodingStyle
) { ) {
parent::__construct(); parent::__construct();
$this->consoleStyle = $consoleStyle; $this->consoleStyle = $consoleStyle;
$this->configurationFactory = $configurationFactory; $this->configurationFactory = $configurationFactory;
$this->betterStandardPrinter = $betterStandardPrinter; $this->betterStandardPrinter = $betterStandardPrinter;
$this->finderSanitizer = $finderSanitizer; $this->finderSanitizer = $finderSanitizer;
$this->afterRectorCodingStyle = $afterRectorCodingStyle;
} }
protected function configure(): void protected function configure(): void
@ -97,7 +104,7 @@ final class CreateRectorCommand extends Command
} }
} }
$this->runStyle($this->generatedFiles); $this->applyCodingStyle();
$this->printSuccess($configuration, $testCasePath); $this->printSuccess($configuration, $testCasePath);
@ -190,29 +197,13 @@ CODE_SAMPLE;
)); ));
} }
/** private function applyCodingStyle(): void
* @param string[] $source
*/
private function runStyle(array $source): void
{ {
// filter only .php files // filter only .php files
$source = array_filter($source, function (string $file) { $generatedPhpFiles = array_filter($this->generatedFiles, function (string $file) {
return Strings::endsWith($file, '.php'); return Strings::endsWith($file, '.php');
}); });
$command = sprintf( $this->afterRectorCodingStyle->apply($generatedPhpFiles);
'vendor/bin/ecs check %s --config %s --fix',
implode(' ', $source),
__DIR__ . '/../../../../ecs-after-rector.yml'
);
$process = new Process($command);
$process->run();
if (! $process->isSuccessful()) {
$this->consoleStyle->error(
sprintf('Basic coding standard was not applied due to: "%s"', $process->getErrorOutput())
);
}
} }
} }

View File

@ -0,0 +1,41 @@
<?php declare(strict_types=1);
namespace Rector\CodingStyle;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
final class AfterRectorCodingStyle
{
/**
* @var string
*/
private const ECS_BIN_PATH = __DIR__ . '/../../ecs-after-rector.yml';
/**
* @param string[] $source
*/
public function apply(array $source): void
{
$this->validate();
$command = sprintf('vendor/bin/ecs check %s --config %s --fix', implode(' ', $source), self::ECS_BIN_PATH);
$process = new Process($command);
$process->run();
if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}
}
private function validate(): void
{
if (file_exists(self::ECS_BIN_PATH)) {
return;
}
throw new InvalidConfigurationException(sprintf('ECS bin file not found in "%s"', self::ECS_BIN_PATH));
}
}

View File

@ -8,6 +8,7 @@ use Rector\Application\Error;
use Rector\Application\ErrorCollector; use Rector\Application\ErrorCollector;
use Rector\Application\FileProcessor; use Rector\Application\FileProcessor;
use Rector\Autoloading\AdditionalAutoloader; use Rector\Autoloading\AdditionalAutoloader;
use Rector\CodingStyle\AfterRectorCodingStyle;
use Rector\Configuration\Option; use Rector\Configuration\Option;
use Rector\Console\ConsoleStyle; use Rector\Console\ConsoleStyle;
use Rector\Console\Output\ProcessCommandReporter; use Rector\Console\Output\ProcessCommandReporter;
@ -23,7 +24,6 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Symplify\PackageBuilder\Console\Command\CommandNaming; use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\FileSystem\SmartFileInfo; use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
use Symplify\PackageBuilder\Parameter\ParameterProvider; use Symplify\PackageBuilder\Parameter\ParameterProvider;
@ -97,6 +97,11 @@ final class ProcessCommand extends Command
*/ */
private $errorCollector; private $errorCollector;
/**
* @var AfterRectorCodingStyle
*/
private $afterRectorCodingStyle;
public function __construct( public function __construct(
FileProcessor $fileProcessor, FileProcessor $fileProcessor,
ConsoleStyle $consoleStyle, ConsoleStyle $consoleStyle,
@ -108,7 +113,8 @@ final class ProcessCommand extends Command
YamlFileProcessor $yamlFileProcessor, YamlFileProcessor $yamlFileProcessor,
RectorGuard $rectorGuard, RectorGuard $rectorGuard,
FileSystemFileProcessor $fileSystemFileProcessor, FileSystemFileProcessor $fileSystemFileProcessor,
ErrorCollector $errorCollector ErrorCollector $errorCollector,
AfterRectorCodingStyle $afterRectorCodingStyle
) { ) {
parent::__construct(); parent::__construct();
@ -123,6 +129,7 @@ final class ProcessCommand extends Command
$this->rectorGuard = $rectorGuard; $this->rectorGuard = $rectorGuard;
$this->fileSystemFileProcessor = $fileSystemFileProcessor; $this->fileSystemFileProcessor = $fileSystemFileProcessor;
$this->errorCollector = $errorCollector; $this->errorCollector = $errorCollector;
$this->afterRectorCodingStyle = $afterRectorCodingStyle;
} }
protected function configure(): void protected function configure(): void
@ -189,7 +196,7 @@ final class ProcessCommand extends Command
} }
if ($input->getOption(Option::OPTION_WITH_STYLE)) { if ($input->getOption(Option::OPTION_WITH_STYLE)) {
$this->runStyle($source); $this->afterRectorCodingStyle->apply($source);
} }
$this->consoleStyle->success('Rector is done!'); $this->consoleStyle->success('Rector is done!');
@ -282,28 +289,4 @@ final class ProcessCommand extends Command
} }
} }
} }
/**
* @param string[] $source
*/
private function runStyle(array $source): void
{
$command = sprintf(
'vendor/bin/ecs check %s --config %s --fix',
implode(' ', $source),
__DIR__ . '/../../../ecs-after-rector.yml'
);
$process = new Process($command);
$process->run();
if ($process->isSuccessful()) {
$this->consoleStyle->success('Basic coding standard is done');
return;
}
$this->consoleStyle->error(
sprintf('Basic coding standard was not applied due to: "%s"', $process->getErrorOutput())
);
}
} }