diff --git a/phpstan.neon b/phpstan.neon index 1e399523bcd2..59ef8f43c635 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -259,3 +259,4 @@ parameters: # known value - "#^Parameter \\#1 \\$variable of class Rector\\\\Php70\\\\ValueObject\\\\VariableAssignPair constructor expects PhpParser\\\\Node\\\\Expr\\\\ArrayDimFetch\\|PhpParser\\\\Node\\\\Expr\\\\PropertyFetch\\|PhpParser\\\\Node\\\\Expr\\\\StaticPropertyFetch\\|PhpParser\\\\Node\\\\Expr\\\\Variable, PhpParser\\\\Node\\\\Expr given\\.$#" + - '#Cannot cast \(array\)\|string\|true to string#' diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 17a47e780480..03b4119b66ae 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -49,6 +49,11 @@ final class Configuration */ private $mustMatchGitDiff = false; + /** + * @var string + */ + private $outputFile; + /** * Needs to run in the start of the life cycle, since the rest of workflow uses it. */ @@ -59,6 +64,9 @@ public function resolveFromInput(InputInterface $input): void $this->mustMatchGitDiff = (bool) $input->getOption(Option::MATCH_GIT_DIFF); $this->showProgressBar = $this->canShowProgressBar($input); + $outputFileOption = $input->getOption(Option::OPTION_OUTPUT_FILE); + $this->outputFile = $outputFileOption ? (string) $outputFileOption : null; + /** @var string|null $onlyRector */ $onlyRector = $input->getOption(Option::OPTION_ONLY); @@ -129,6 +137,11 @@ public function getOnlyRector(): ?string return $this->onlyRector; } + public function getOutputFile(): ?string + { + return $this->outputFile; + } + private function setOnlyRector(?string $rector): void { if ($rector) { diff --git a/src/Configuration/Option.php b/src/Configuration/Option.php index 7a7b592f93c9..de8e2bc5716f 100644 --- a/src/Configuration/Option.php +++ b/src/Configuration/Option.php @@ -74,5 +74,5 @@ final class Option /** * @var string */ - public const DUMP = 'dump'; + public const OPTION_OUTPUT_FILE = 'output-file'; } diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 6034423ed4ce..fd5207d40e06 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -134,6 +134,7 @@ protected function configure(): void InputOption::VALUE_NONE, 'See diff of changes, do not save them to files.' ); + $this->addOption( Option::OPTION_AUTOLOAD_FILE, 'a', @@ -177,6 +178,13 @@ protected function configure(): void InputOption::VALUE_NONE, 'Hide progress bar. Useful e.g. for nicer CI output.' ); + + $this->addOption( + Option::OPTION_OUTPUT_FILE, + null, + InputOption::VALUE_REQUIRED, + 'Location for file to dump result in. Useful for Docker or automated processes' + ); } protected function execute(InputInterface $input, OutputInterface $output): int diff --git a/src/Console/Output/ConsoleOutputFormatter.php b/src/Console/Output/ConsoleOutputFormatter.php index c59f5cec173c..435c46de5fbe 100644 --- a/src/Console/Output/ConsoleOutputFormatter.php +++ b/src/Console/Output/ConsoleOutputFormatter.php @@ -7,6 +7,7 @@ use Nette\Utils\Strings; use Rector\Application\ErrorAndDiffCollector; use Rector\Configuration\Configuration; +use Rector\Configuration\Option; use Rector\Contract\Console\Output\OutputFormatterInterface; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Printer\BetterStandardPrinter; @@ -49,6 +50,15 @@ public function __construct( public function report(ErrorAndDiffCollector $errorAndDiffCollector): void { + if ($this->configuration->getConfigFilePath()) { + $this->symfonyStyle->error(sprintf( + 'Option "--%s" can be used only with "--%s %s"', + Option::OPTION_OUTPUT_FILE, + Option::OPTION_OUTPUT_FORMAT, + 'json' + )); + } + $this->reportFileDiffs($errorAndDiffCollector->getFileDiffs()); $this->reportErrors($errorAndDiffCollector->getErrors()); $this->reportRemovedFilesAndNodes($errorAndDiffCollector); diff --git a/src/Console/Output/JsonOutputFormatter.php b/src/Console/Output/JsonOutputFormatter.php index db953e220a91..58d7fe93195d 100644 --- a/src/Console/Output/JsonOutputFormatter.php +++ b/src/Console/Output/JsonOutputFormatter.php @@ -4,6 +4,7 @@ namespace Rector\Console\Output; +use Nette\Utils\FileSystem; use Nette\Utils\Json; use Rector\Application\ErrorAndDiffCollector; use Rector\Configuration\Configuration; @@ -89,6 +90,11 @@ public function report(ErrorAndDiffCollector $errorAndDiffCollector): void $json = Json::encode($errorsArray, Json::PRETTY); - $this->symfonyStyle->writeln($json); + $outputFile = $this->configuration->getOutputFile(); + if ($outputFile !== null) { + FileSystem::write($outputFile, $json . PHP_EOL); + } else { + $this->symfonyStyle->writeln($json); + } } }