Skip to content

Commit c7b8f8a

Browse files
authored
UX: better gitlab reporting - add content field (#9152)
1 parent d21ef17 commit c7b8f8a

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Console/Report/FixReport/GitlabReporter.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
namespace PhpCsFixer\Console\Report\FixReport;
1616

1717
use PhpCsFixer\Console\Application;
18+
use PhpCsFixer\Documentation\DocumentationLocator;
19+
use PhpCsFixer\Fixer\FixerInterface;
20+
use PhpCsFixer\FixerFactory;
1821
use SebastianBergmann\Diff\Chunk;
1922
use SebastianBergmann\Diff\Diff;
2023
use SebastianBergmann\Diff\Line;
@@ -25,6 +28,7 @@
2528
* Generates a report according to gitlabs subset of codeclimate json files.
2629
*
2730
* @author Hans-Christian Otto <c.otto@suora.com>
31+
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
2832
*
2933
* @see https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types
3034
*
@@ -37,10 +41,23 @@
3741
final class GitlabReporter implements ReporterInterface
3842
{
3943
private Parser $diffParser;
44+
private DocumentationLocator $documentationLocator;
45+
private FixerFactory $fixerFactory;
46+
47+
/**
48+
* @var array<string, FixerInterface>
49+
*/
50+
private array $fixers;
4051

4152
public function __construct()
4253
{
4354
$this->diffParser = new Parser();
55+
$this->documentationLocator = new DocumentationLocator();
56+
57+
$this->fixerFactory = new FixerFactory();
58+
$this->fixerFactory->registerBuiltInFixers();
59+
60+
$this->fixers = $this->createFixers();
4461
}
4562

4663
public function getFormat(): string
@@ -58,9 +75,23 @@ public function generate(ReportSummary $reportSummary): string
5875
$report = [];
5976
foreach ($reportSummary->getChanged() as $fileName => $change) {
6077
foreach ($change['appliedFixers'] as $fixerName) {
78+
$fixer = $this->fixers[$fixerName] ?? null;
79+
6180
$report[] = [
6281
'check_name' => 'PHP-CS-Fixer.'.$fixerName,
6382
'description' => 'PHP-CS-Fixer.'.$fixerName.' by '.$about,
83+
'content' => [
84+
'body' => \sprintf(
85+
"%s\n%s",
86+
$about,
87+
null !== $fixer
88+
? \sprintf(
89+
'Check [docs](https://cs.symfony.com/doc/rules/%s.html) for more information.',
90+
substr($this->documentationLocator->getFixerDocumentationFileRelativePath($fixer), 0, -4) // -4 to drop `.rst`
91+
)
92+
: 'Check performed with a custom rule.'
93+
),
94+
],
6495
'categories' => ['Style'],
6596
'fingerprint' => md5($fileName.$fixerName),
6697
'severity' => 'minor',
@@ -122,4 +153,20 @@ private static function getBeginEndForDiffChunk(Chunk $chunk): array
122153
'end' => $start + $startRange,
123154
];
124155
}
156+
157+
/**
158+
* @return array<string, FixerInterface>
159+
*/
160+
private function createFixers(): array
161+
{
162+
$fixers = [];
163+
164+
foreach ($this->fixerFactory->getFixers() as $fixer) {
165+
$fixers[$fixer->getName()] = $fixer;
166+
}
167+
168+
ksort($fixers);
169+
170+
return $fixers;
171+
}
125172
}

tests/Console/Report/FixReport/GitlabReporterTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ protected static function createSimpleReport(): string
5656
"categories": ["Style"],
5757
"check_name": "PHP-CS-Fixer.some_fixer_name_here",
5858
"description": "PHP-CS-Fixer.some_fixer_name_here by {$about}",
59+
"content": {
60+
"body": "{$about}\\nCheck performed with a custom rule."
61+
},
5962
"fingerprint": "ad098ea6ea7a28dd85dfcdfc9e2bded0",
6063
"severity": "minor",
6164
"location": {
@@ -83,6 +86,9 @@ protected static function createWithAppliedFixersReport(): string
8386
"categories": ["Style"],
8487
"check_name": "PHP-CS-Fixer.some_fixer_name_here_1",
8588
"description": "PHP-CS-Fixer.some_fixer_name_here_1 by {$about}",
89+
"content": {
90+
"body": "{$about}\\nCheck performed with a custom rule."
91+
},
8692
"fingerprint": "b74e9385c8ae5b1f575c9c8226c7deff",
8793
"severity": "minor",
8894
"location": {
@@ -96,6 +102,9 @@ protected static function createWithAppliedFixersReport(): string
96102
"categories": ["Style"],
97103
"check_name": "PHP-CS-Fixer.some_fixer_name_here_2",
98104
"description": "PHP-CS-Fixer.some_fixer_name_here_2 by {$about}",
105+
"content": {
106+
"body": "{$about}\\nCheck performed with a custom rule."
107+
},
99108
"fingerprint": "acad4672140c737a83c18d1474d84074",
100109
"severity": "minor",
101110
"location": {
@@ -123,6 +132,9 @@ protected static function createComplexReport(): string
123132
"categories": ["Style"],
124133
"check_name": "PHP-CS-Fixer.some_fixer_name_here_1",
125134
"description": "PHP-CS-Fixer.some_fixer_name_here_1 by {$about}",
135+
"content": {
136+
"body": "{$about}\\nCheck performed with a custom rule."
137+
},
126138
"fingerprint": "b74e9385c8ae5b1f575c9c8226c7deff",
127139
"severity": "minor",
128140
"location": {
@@ -136,6 +148,9 @@ protected static function createComplexReport(): string
136148
"categories": ["Style"],
137149
"check_name": "PHP-CS-Fixer.some_fixer_name_here_2",
138150
"description": "PHP-CS-Fixer.some_fixer_name_here_2 by {$about}",
151+
"content": {
152+
"body": "{$about}\\nCheck performed with a custom rule."
153+
},
139154
"fingerprint": "acad4672140c737a83c18d1474d84074",
140155
"severity": "minor",
141156
"location": {
@@ -149,6 +164,9 @@ protected static function createComplexReport(): string
149164
"categories": ["Style"],
150165
"check_name": "PHP-CS-Fixer.another_fixer_name_here",
151166
"description": "PHP-CS-Fixer.another_fixer_name_here by {$about}",
167+
"content": {
168+
"body": "{$about}\\nCheck performed with a custom rule."
169+
},
152170
"fingerprint": "30e86e533dac0f1b93bbc3a55c6908f8",
153171
"severity": "minor",
154172
"location": {

0 commit comments

Comments
 (0)