Skip to content

Commit e08d14d

Browse files
committed
[ChangesReporting] Add per-line rule attribution to JSON output
1 parent 1b5127a commit e08d14d

4 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎src/ChangesReporting/Output/Factory/JsonOutputFactory.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ public static function create(
3939
;
4040

4141
if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') {
42+
$changes = [];
43+
foreach ($fileDiff->getRectorChanges() as $rectorWithLineChange) {
44+
$changes[] = [
45+
'rector' => $rectorWithLineChange->getRectorClass(),
46+
'line' => $rectorWithLineChange->getLine(),
47+
];
48+
}
49+
4250
$errorsJson[Bridge::FILE_DIFFS][] = [
4351
'file' => $filePath,
4452
'diff' => $fileDiff->getDiff(),
4553
'applied_rectors' => $fileDiff->getRectorClasses(),
54+
'changes' => $changes,
4655
];
4756
}
4857

‎src/ChangesReporting/ValueObject/RectorWithLineChange.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function getRectorClass(): string
3232
return $this->rectorClass;
3333
}
3434

35+
public function getLine(): int
36+
{
37+
return $this->line;
38+
}
39+
3540
/**
3641
* @param array<string, mixed> $json
3742
*/

‎tests/ChangesReporting/Output/Factory/JsonOutputFactoryTest.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Rector\ChangesReporting\Output\Factory\JsonOutputFactory;
99
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
10+
use Rector\Php80\Rector\Identical\StrEndsWithRector;
1011
use Rector\Php80\Rector\Identical\StrStartsWithRector;
1112
use Rector\ValueObject\Configuration;
1213
use Rector\ValueObject\Error\SystemError;
@@ -44,4 +45,33 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void
4445
$expectedOutput = (string) file_get_contents(__DIR__ . '/../Fixtures/without_diffs.json');
4546
$this->assertSame($expectedOutput, $actualOutput);
4647
}
48+
49+
public function testReportShouldAttributeEachRectorToItsLine(): void
50+
{
51+
$diff = '--- Original' . PHP_EOL . '+++ New' . PHP_EOL .
52+
'@@ -38,5 +39,6 @@' . PHP_EOL .
53+
'return true;' . PHP_EOL . '}' . PHP_EOL;
54+
55+
$actualOutput = JsonOutputFactory::create(
56+
new ProcessResult(
57+
[],
58+
[
59+
new FileDiff(
60+
'some/file.php',
61+
$diff,
62+
'diff console formatted',
63+
[
64+
new RectorWithLineChange(StrStartsWithRector::class, 38),
65+
new RectorWithLineChange(StrEndsWithRector::class, 42),
66+
]
67+
),
68+
],
69+
1
70+
),
71+
new Configuration(showDiffs: true)
72+
);
73+
74+
$expectedOutput = (string) file_get_contents(__DIR__ . '/../Fixtures/with_diffs.json');
75+
$this->assertSame($expectedOutput, $actualOutput);
76+
}
4777
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"totals": {
3+
"changed_files": 1,
4+
"errors": 0
5+
},
6+
"file_diffs": [
7+
{
8+
"file": "some/file.php",
9+
"diff": "--- Original\n+++ New\n@@ -38,5 +39,6 @@\nreturn true;\n}\n",
10+
"applied_rectors": [
11+
"Rector\\Php80\\Rector\\Identical\\StrEndsWithRector",
12+
"Rector\\Php80\\Rector\\Identical\\StrStartsWithRector"
13+
],
14+
"changes": [
15+
{
16+
"rector": "Rector\\Php80\\Rector\\Identical\\StrStartsWithRector",
17+
"line": 38
18+
},
19+
{
20+
"rector": "Rector\\Php80\\Rector\\Identical\\StrEndsWithRector",
21+
"line": 42
22+
}
23+
]
24+
}
25+
],
26+
"changed_files": [
27+
"some/file.php"
28+
]
29+
}

0 commit comments

Comments
 (0)