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
9 changes: 9 additions & 0 deletions src/ChangesReporting/Output/Factory/JsonOutputFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/ChangesReporting/ValueObject/RectorWithLineChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function getRectorClass(): string
return $this->rectorClass;
}

public function getLine(): int
{
return $this->line;
}

/**
* @param array<string, mixed> $json
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/ChangesReporting/Output/Factory/JsonOutputFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
29 changes: 29 additions & 0 deletions tests/ChangesReporting/Output/Fixtures/with_diffs.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
Loading