diff --git a/src/ChangesReporting/Output/Factory/JsonOutputFactory.php b/src/ChangesReporting/Output/Factory/JsonOutputFactory.php index fb8b09c3ee9..de0bf4e0d11 100644 --- a/src/ChangesReporting/Output/Factory/JsonOutputFactory.php +++ b/src/ChangesReporting/Output/Factory/JsonOutputFactory.php @@ -39,10 +39,19 @@ public static function create( ; if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') { + $changes = []; + foreach ($fileDiff->getRectorChanges() as $rectorWithLineChange) { + $changes[] = [ + 'rector' => $rectorWithLineChange->getRectorClass(), + 'line' => $rectorWithLineChange->getLine(), + ]; + } + $errorsJson[Bridge::FILE_DIFFS][] = [ 'file' => $filePath, 'diff' => $fileDiff->getDiff(), 'applied_rectors' => $fileDiff->getRectorClasses(), + 'changes' => $changes, ]; } diff --git a/src/ChangesReporting/ValueObject/RectorWithLineChange.php b/src/ChangesReporting/ValueObject/RectorWithLineChange.php index a69910a16c6..09f5680d6bb 100644 --- a/src/ChangesReporting/ValueObject/RectorWithLineChange.php +++ b/src/ChangesReporting/ValueObject/RectorWithLineChange.php @@ -32,6 +32,11 @@ public function getRectorClass(): string return $this->rectorClass; } + public function getLine(): int + { + return $this->line; + } + /** * @param array $json */ diff --git a/tests/ChangesReporting/Output/Factory/JsonOutputFactoryTest.php b/tests/ChangesReporting/Output/Factory/JsonOutputFactoryTest.php index 5f7a467b910..66ad7769cdf 100644 --- a/tests/ChangesReporting/Output/Factory/JsonOutputFactoryTest.php +++ b/tests/ChangesReporting/Output/Factory/JsonOutputFactoryTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase; use Rector\ChangesReporting\Output\Factory\JsonOutputFactory; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; +use Rector\Php80\Rector\Identical\StrEndsWithRector; use Rector\Php80\Rector\Identical\StrStartsWithRector; use Rector\ValueObject\Configuration; use Rector\ValueObject\Error\SystemError; @@ -44,4 +45,33 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void $expectedOutput = (string) file_get_contents(__DIR__ . '/../Fixtures/without_diffs.json'); $this->assertSame($expectedOutput, $actualOutput); } + + public function testReportShouldAttributeEachRectorToItsLine(): void + { + $diff = '--- Original' . PHP_EOL . '+++ New' . PHP_EOL . + '@@ -38,5 +39,6 @@' . PHP_EOL . + 'return true;' . PHP_EOL . '}' . PHP_EOL; + + $actualOutput = JsonOutputFactory::create( + new ProcessResult( + [], + [ + new FileDiff( + 'some/file.php', + $diff, + 'diff console formatted', + [ + new RectorWithLineChange(StrStartsWithRector::class, 38), + new RectorWithLineChange(StrEndsWithRector::class, 42), + ] + ), + ], + 1 + ), + new Configuration(showDiffs: true) + ); + + $expectedOutput = (string) file_get_contents(__DIR__ . '/../Fixtures/with_diffs.json'); + $this->assertSame($expectedOutput, $actualOutput); + } } diff --git a/tests/ChangesReporting/Output/Fixtures/with_diffs.json b/tests/ChangesReporting/Output/Fixtures/with_diffs.json new file mode 100644 index 00000000000..853978643fe --- /dev/null +++ b/tests/ChangesReporting/Output/Fixtures/with_diffs.json @@ -0,0 +1,29 @@ +{ + "totals": { + "changed_files": 1, + "errors": 0 + }, + "file_diffs": [ + { + "file": "some/file.php", + "diff": "--- Original\n+++ New\n@@ -38,5 +39,6 @@\nreturn true;\n}\n", + "applied_rectors": [ + "Rector\\Php80\\Rector\\Identical\\StrEndsWithRector", + "Rector\\Php80\\Rector\\Identical\\StrStartsWithRector" + ], + "changes": [ + { + "rector": "Rector\\Php80\\Rector\\Identical\\StrStartsWithRector", + "line": 38 + }, + { + "rector": "Rector\\Php80\\Rector\\Identical\\StrEndsWithRector", + "line": 42 + } + ] + } + ], + "changed_files": [ + "some/file.php" + ] +} \ No newline at end of file