mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
Add AfterRectorCodingStyle
This commit is contained in:
parent
161c3cd6fa
commit
985d38e267
@ -8,6 +8,7 @@ use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\ArrayItem;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use Rector\CodingStyle\AfterRectorCodingStyle;
|
||||
use Rector\Console\ConsoleStyle;
|
||||
use Rector\ContributorTools\Configuration\Configuration;
|
||||
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\Output\OutputInterface;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Symplify\PackageBuilder\Console\Command\CommandNaming;
|
||||
use Symplify\PackageBuilder\Console\ShellCode;
|
||||
use Symplify\PackageBuilder\FileSystem\FinderSanitizer;
|
||||
@ -57,17 +57,24 @@ final class CreateRectorCommand extends Command
|
||||
*/
|
||||
private $generatedFiles = [];
|
||||
|
||||
/**
|
||||
* @var AfterRectorCodingStyle
|
||||
*/
|
||||
private $afterRectorCodingStyle;
|
||||
|
||||
public function __construct(
|
||||
ConsoleStyle $consoleStyle,
|
||||
ConfigurationFactory $configurationFactory,
|
||||
BetterStandardPrinter $betterStandardPrinter,
|
||||
FinderSanitizer $finderSanitizer
|
||||
FinderSanitizer $finderSanitizer,
|
||||
AfterRectorCodingStyle $afterRectorCodingStyle
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->consoleStyle = $consoleStyle;
|
||||
$this->configurationFactory = $configurationFactory;
|
||||
$this->betterStandardPrinter = $betterStandardPrinter;
|
||||
$this->finderSanitizer = $finderSanitizer;
|
||||
$this->afterRectorCodingStyle = $afterRectorCodingStyle;
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
@ -97,7 +104,7 @@ final class CreateRectorCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
$this->runStyle($this->generatedFiles);
|
||||
$this->applyCodingStyle();
|
||||
|
||||
$this->printSuccess($configuration, $testCasePath);
|
||||
|
||||
@ -190,29 +197,13 @@ CODE_SAMPLE;
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $source
|
||||
*/
|
||||
private function runStyle(array $source): void
|
||||
private function applyCodingStyle(): void
|
||||
{
|
||||
// filter only .php files
|
||||
$source = array_filter($source, function (string $file) {
|
||||
$generatedPhpFiles = array_filter($this->generatedFiles, function (string $file) {
|
||||
return Strings::endsWith($file, '.php');
|
||||
});
|
||||
|
||||
$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->error(
|
||||
sprintf('Basic coding standard was not applied due to: "%s"', $process->getErrorOutput())
|
||||
);
|
||||
}
|
||||
$this->afterRectorCodingStyle->apply($generatedPhpFiles);
|
||||
}
|
||||
}
|
||||
|
41
src/CodingStyle/AfterRectorCodingStyle.php
Normal file
41
src/CodingStyle/AfterRectorCodingStyle.php
Normal 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));
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ use Rector\Application\Error;
|
||||
use Rector\Application\ErrorCollector;
|
||||
use Rector\Application\FileProcessor;
|
||||
use Rector\Autoloading\AdditionalAutoloader;
|
||||
use Rector\CodingStyle\AfterRectorCodingStyle;
|
||||
use Rector\Configuration\Option;
|
||||
use Rector\Console\ConsoleStyle;
|
||||
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\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Symplify\PackageBuilder\Console\Command\CommandNaming;
|
||||
use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
|
||||
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||
@ -97,6 +97,11 @@ final class ProcessCommand extends Command
|
||||
*/
|
||||
private $errorCollector;
|
||||
|
||||
/**
|
||||
* @var AfterRectorCodingStyle
|
||||
*/
|
||||
private $afterRectorCodingStyle;
|
||||
|
||||
public function __construct(
|
||||
FileProcessor $fileProcessor,
|
||||
ConsoleStyle $consoleStyle,
|
||||
@ -108,7 +113,8 @@ final class ProcessCommand extends Command
|
||||
YamlFileProcessor $yamlFileProcessor,
|
||||
RectorGuard $rectorGuard,
|
||||
FileSystemFileProcessor $fileSystemFileProcessor,
|
||||
ErrorCollector $errorCollector
|
||||
ErrorCollector $errorCollector,
|
||||
AfterRectorCodingStyle $afterRectorCodingStyle
|
||||
) {
|
||||
parent::__construct();
|
||||
|
||||
@ -123,6 +129,7 @@ final class ProcessCommand extends Command
|
||||
$this->rectorGuard = $rectorGuard;
|
||||
$this->fileSystemFileProcessor = $fileSystemFileProcessor;
|
||||
$this->errorCollector = $errorCollector;
|
||||
$this->afterRectorCodingStyle = $afterRectorCodingStyle;
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
@ -189,7 +196,7 @@ final class ProcessCommand extends Command
|
||||
}
|
||||
|
||||
if ($input->getOption(Option::OPTION_WITH_STYLE)) {
|
||||
$this->runStyle($source);
|
||||
$this->afterRectorCodingStyle->apply($source);
|
||||
}
|
||||
|
||||
$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())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user