Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\>\)\|string\|true to string#'
13 changes: 13 additions & 0 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ final class Option
/**
* @var string
*/
public const DUMP = 'dump';
public const OPTION_OUTPUT_FILE = 'output-file';
}
8 changes: 8 additions & 0 deletions src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/Console/Output/ConsoleOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/Console/Output/JsonOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Console\Output;

use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Rector\Application\ErrorAndDiffCollector;
use Rector\Configuration\Configuration;
Expand Down Expand Up @@ -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);
}
}
}