Skip to content

Commit 586f583

Browse files
committed
Updated Rector to commit 682d85bb311e2149790daccbb69e843a5ab1a215
rectorphp/rector-src@682d85b [ChangesReporting][Alternative] Collect changed_files on --no-diffs with json output with use of JsonOutputFactory (#7821)
1 parent 79bd1f8 commit 586f583

6 files changed

Lines changed: 69 additions & 46 deletions

File tree

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = 'cdd219c5341e7ef841c1567960e60a945e9b59f2';
22+
public const PACKAGE_VERSION = '682d85bb311e2149790daccbb69e843a5ab1a215';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-04-07 18:40:28';
27+
public const RELEASE_DATE = '2026-04-07 18:43:00';
2828
/**
2929
* @var int
3030
*/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\ChangesReporting\Output\Factory;
5+
6+
use RectorPrefix202604\Nette\Utils\Json;
7+
use Rector\Parallel\ValueObject\Bridge;
8+
use Rector\ValueObject\Configuration;
9+
use Rector\ValueObject\Error\SystemError;
10+
use Rector\ValueObject\ProcessResult;
11+
/**
12+
* @see \Rector\Tests\ChangesReporting\Output\Factory\JsonOutputFactoryTest
13+
*/
14+
final class JsonOutputFactory
15+
{
16+
public static function create(ProcessResult $processResult, Configuration $configuration): string
17+
{
18+
$errorsJson = ['totals' => ['changed_files' => $processResult->getTotalChanged()]];
19+
// We need onlyWithChanges: false to include all file diffs
20+
$fileDiffs = $processResult->getFileDiffs(\false);
21+
ksort($fileDiffs);
22+
foreach ($fileDiffs as $fileDiff) {
23+
$filePath = $configuration->isReportingWithRealPath() ? $fileDiff->getAbsoluteFilePath() ?? '' : $fileDiff->getRelativeFilePath();
24+
if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') {
25+
$errorsJson[Bridge::FILE_DIFFS][] = ['file' => $filePath, 'diff' => $fileDiff->getDiff(), 'applied_rectors' => $fileDiff->getRectorClasses()];
26+
}
27+
// for Rector CI
28+
$errorsJson['changed_files'][] = $filePath;
29+
}
30+
$systemErrors = $processResult->getSystemErrors();
31+
$errorsJson['totals']['errors'] = count($systemErrors);
32+
$errorsData = self::createErrorsData($systemErrors, $configuration->isReportingWithRealPath());
33+
if ($errorsData !== []) {
34+
$errorsJson['errors'] = $errorsData;
35+
}
36+
return Json::encode($errorsJson, \true);
37+
}
38+
/**
39+
* @param SystemError[] $errors
40+
* @return mixed[]
41+
*/
42+
private static function createErrorsData(array $errors, bool $absoluteFilePath): array
43+
{
44+
$errorsData = [];
45+
foreach ($errors as $error) {
46+
$errorDataJson = ['message' => $error->getMessage(), 'file' => $absoluteFilePath ? $error->getAbsoluteFilePath() : $error->getRelativeFilePath()];
47+
if ($error->getRectorClass() !== null) {
48+
$errorDataJson['caused_by'] = $error->getRectorClass();
49+
}
50+
if ($error->getLine() !== null) {
51+
$errorDataJson['line'] = $error->getLine();
52+
}
53+
$errorsData[] = $errorDataJson;
54+
}
55+
return $errorsData;
56+
}
57+
}

src/ChangesReporting/Output/JsonOutputFormatter.php

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
declare (strict_types=1);
44
namespace Rector\ChangesReporting\Output;
55

6-
use RectorPrefix202604\Nette\Utils\Json;
76
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
8-
use Rector\Parallel\ValueObject\Bridge;
7+
use Rector\ChangesReporting\Output\Factory\JsonOutputFactory;
98
use Rector\ValueObject\Configuration;
10-
use Rector\ValueObject\Error\SystemError;
119
use Rector\ValueObject\ProcessResult;
1210
final class JsonOutputFormatter implements OutputFormatterInterface
1311
{
@@ -21,41 +19,6 @@ public function getName(): string
2119
}
2220
public function report(ProcessResult $processResult, Configuration $configuration): void
2321
{
24-
$errorsJson = ['totals' => ['changed_files' => $processResult->getTotalChanged()]];
25-
$fileDiffs = $processResult->getFileDiffs();
26-
ksort($fileDiffs);
27-
foreach ($fileDiffs as $fileDiff) {
28-
$filePath = $configuration->isReportingWithRealPath() ? $fileDiff->getAbsoluteFilePath() ?? '' : $fileDiff->getRelativeFilePath();
29-
$errorsJson[Bridge::FILE_DIFFS][] = ['file' => $filePath, 'diff' => $fileDiff->getDiff(), 'applied_rectors' => $fileDiff->getRectorClasses()];
30-
// for Rector CI
31-
$errorsJson['changed_files'][] = $filePath;
32-
}
33-
$systemErrors = $processResult->getSystemErrors();
34-
$errorsJson['totals']['errors'] = count($systemErrors);
35-
$errorsData = $this->createErrorsData($systemErrors, $configuration->isReportingWithRealPath());
36-
if ($errorsData !== []) {
37-
$errorsJson['errors'] = $errorsData;
38-
}
39-
$json = Json::encode($errorsJson, \true);
40-
echo $json . \PHP_EOL;
41-
}
42-
/**
43-
* @param SystemError[] $errors
44-
* @return mixed[]
45-
*/
46-
private function createErrorsData(array $errors, bool $absoluteFilePath): array
47-
{
48-
$errorsData = [];
49-
foreach ($errors as $error) {
50-
$errorDataJson = ['message' => $error->getMessage(), 'file' => $absoluteFilePath ? $error->getAbsoluteFilePath() : $error->getRelativeFilePath()];
51-
if ($error->getRectorClass() !== null) {
52-
$errorDataJson['caused_by'] = $error->getRectorClass();
53-
}
54-
if ($error->getLine() !== null) {
55-
$errorDataJson['line'] = $error->getLine();
56-
}
57-
$errorsData[] = $errorDataJson;
58-
}
59-
return $errorsData;
22+
echo JsonOutputFactory::create($processResult, $configuration) . \PHP_EOL;
6023
}
6124
}

src/Console/Command/WorkerCommand.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Rector\Autoloading\AdditionalAutoloader;
1313
use Rector\Configuration\ConfigurationFactory;
1414
use Rector\Configuration\ConfigurationRuleFilter;
15+
use Rector\Configuration\Option;
1516
use Rector\Console\ProcessConfigureDecorator;
1617
use Rector\Parallel\ValueObject\Bridge;
1718
use Rector\StaticReflection\DynamicSourceLocatorDecorator;
@@ -89,16 +90,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8990
$parallelIdentifier = $configuration->getParallelIdentifier();
9091
$tcpConnector = new TcpConnector($streamSelectLoop);
9192
$promise = $tcpConnector->connect('127.0.0.1:' . $configuration->getParallelPort());
92-
$promise->then(function (ConnectionInterface $connection) use ($parallelIdentifier, $configuration, $output): void {
93+
$promise->then(function (ConnectionInterface $connection) use ($parallelIdentifier, $configuration, $input, $output): void {
9394
$inDecoder = new Decoder($connection, \true, 512, \JSON_INVALID_UTF8_IGNORE);
9495
$outEncoder = new Encoder($connection, \JSON_INVALID_UTF8_IGNORE);
9596
$outEncoder->write([ReactCommand::ACTION => Action::HELLO, ReactCommand::IDENTIFIER => $parallelIdentifier]);
96-
$this->runWorker($outEncoder, $inDecoder, $configuration, $output);
97+
$this->runWorker($outEncoder, $inDecoder, $configuration, $input, $output);
9798
});
9899
$streamSelectLoop->run();
99100
return self::SUCCESS;
100101
}
101-
private function runWorker(Encoder $encoder, Decoder $decoder, Configuration $configuration, OutputInterface $output): void
102+
private function runWorker(Encoder $encoder, Decoder $decoder, Configuration $configuration, InputInterface $input, OutputInterface $output): void
102103
{
103104
$this->additionalAutoloader->autoloadPaths();
104105
$this->dynamicSourceLocatorDecorator->addPaths($configuration->getPaths());
@@ -117,7 +118,7 @@ private function runWorker(Encoder $encoder, Decoder $decoder, Configuration $co
117118
};
118119
$encoder->on(ReactEvent::ERROR, $handleErrorCallback);
119120
// 2. collect diffs + errors from file processor
120-
$decoder->on(ReactEvent::DATA, function (array $json) use ($preFileCallback, $encoder, $configuration): void {
121+
$decoder->on(ReactEvent::DATA, function (array $json) use ($preFileCallback, $encoder, $configuration, $input): void {
121122
$action = $json[ReactCommand::ACTION];
122123
if ($action !== Action::MAIN) {
123124
return;
@@ -129,7 +130,7 @@ private function runWorker(Encoder $encoder, Decoder $decoder, Configuration $co
129130
/**
130131
* this invokes all listeners listening $decoder->on(...) @see \Symplify\EasyParallel\Enum\ReactEvent::DATA
131132
*/
132-
$encoder->write([ReactCommand::ACTION => Action::RESULT, self::RESULT => [Bridge::FILE_DIFFS => $processResult->getFileDiffs(), Bridge::FILES_COUNT => count($filePaths), Bridge::SYSTEM_ERRORS => $processResult->getSystemErrors(), Bridge::SYSTEM_ERRORS_COUNT => count($processResult->getSystemErrors()), Bridge::TOTAL_CHANGED => $processResult->getTotalChanged()]]);
133+
$encoder->write([ReactCommand::ACTION => Action::RESULT, self::RESULT => [Bridge::FILE_DIFFS => $processResult->getFileDiffs($input->getOption(Option::OUTPUT_FORMAT) !== 'json'), Bridge::FILES_COUNT => count($filePaths), Bridge::SYSTEM_ERRORS => $processResult->getSystemErrors(), Bridge::SYSTEM_ERRORS_COUNT => count($processResult->getSystemErrors()), Bridge::TOTAL_CHANGED => $processResult->getTotalChanged()]]);
133134
});
134135
$decoder->on(ReactEvent::ERROR, $handleErrorCallback);
135136
}

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@
11531153
'Rector\\Carbon\\Rector\\New_\\DateTimeInstanceToCarbonRector' => $baseDir . '/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php',
11541154
'Rector\\ChangesReporting\\Contract\\Output\\OutputFormatterInterface' => $baseDir . '/src/ChangesReporting/Contract/Output/OutputFormatterInterface.php',
11551155
'Rector\\ChangesReporting\\Output\\ConsoleOutputFormatter' => $baseDir . '/src/ChangesReporting/Output/ConsoleOutputFormatter.php',
1156+
'Rector\\ChangesReporting\\Output\\Factory\\JsonOutputFactory' => $baseDir . '/src/ChangesReporting/Output/Factory/JsonOutputFactory.php',
11561157
'Rector\\ChangesReporting\\Output\\GitHubOutputFormatter' => $baseDir . '/src/ChangesReporting/Output/GitHubOutputFormatter.php',
11571158
'Rector\\ChangesReporting\\Output\\GitlabOutputFormatter' => $baseDir . '/src/ChangesReporting/Output/GitlabOutputFormatter.php',
11581159
'Rector\\ChangesReporting\\Output\\JUnitOutputFormatter' => $baseDir . '/src/ChangesReporting/Output/JUnitOutputFormatter.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,7 @@ class ComposerStaticInitc9819cb6f85619d8e1cbb4426044dd33
14131413
'Rector\\Carbon\\Rector\\New_\\DateTimeInstanceToCarbonRector' => __DIR__ . '/../..' . '/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php',
14141414
'Rector\\ChangesReporting\\Contract\\Output\\OutputFormatterInterface' => __DIR__ . '/../..' . '/src/ChangesReporting/Contract/Output/OutputFormatterInterface.php',
14151415
'Rector\\ChangesReporting\\Output\\ConsoleOutputFormatter' => __DIR__ . '/../..' . '/src/ChangesReporting/Output/ConsoleOutputFormatter.php',
1416+
'Rector\\ChangesReporting\\Output\\Factory\\JsonOutputFactory' => __DIR__ . '/../..' . '/src/ChangesReporting/Output/Factory/JsonOutputFactory.php',
14161417
'Rector\\ChangesReporting\\Output\\GitHubOutputFormatter' => __DIR__ . '/../..' . '/src/ChangesReporting/Output/GitHubOutputFormatter.php',
14171418
'Rector\\ChangesReporting\\Output\\GitlabOutputFormatter' => __DIR__ . '/../..' . '/src/ChangesReporting/Output/GitlabOutputFormatter.php',
14181419
'Rector\\ChangesReporting\\Output\\JUnitOutputFormatter' => __DIR__ . '/../..' . '/src/ChangesReporting/Output/JUnitOutputFormatter.php',

0 commit comments

Comments
 (0)