Skip to content

Commit 35a66a2

Browse files
authored
Assure GitLab error formatter have integer line properties
1 parent 07c542f commit 35a66a2

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

src/Command/ErrorFormatter/GitlabErrorFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
4141
'location' => [
4242
'path' => $this->relativePathHelper->getRelativePath($fileSpecificError->getFile()),
4343
'lines' => [
44-
'begin' => $fileSpecificError->getLine(),
44+
'begin' => $fileSpecificError->getLine() ?? 0,
4545
],
4646
],
4747
];

src/Testing/ErrorFormatterTestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected function getOutputContent(): string
6060

6161
protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors): AnalysisResult
6262
{
63-
if ($numFileErrors > 4 || $numFileErrors < 0 || $numGenericErrors > 2 || $numGenericErrors < 0) {
63+
if ($numFileErrors > 5 || $numFileErrors < 0 || $numGenericErrors > 2 || $numGenericErrors < 0) {
6464
throw new \PHPStan\ShouldNotHappenException();
6565
}
6666

@@ -69,6 +69,7 @@ protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors):
6969
new Error('Foo', self::DIRECTORY_PATH . '/foo.php', 1),
7070
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/foo.php', 5),
7171
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/folder with unicode 😃/file name with "spaces" and unicode 😃.php', 2),
72+
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/foo.php', null),
7273
], 0, $numFileErrors);
7374

7475
$genericErrors = array_slice([

tests/PHPStan/Command/ErrorFormatter/GitlabFormatterTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,70 @@ public function dataFormatterOutputProvider(): iterable
111111
]',
112112
];
113113

114+
yield [
115+
'Multiple file errors, including error with line=null',
116+
1,
117+
5,
118+
0,
119+
'[
120+
{
121+
"description": "Bar\nBar2",
122+
"fingerprint": "034b4afbfb347494c14e396ed8327692f58be4cd27e8aff5f19f4194934db7c9",
123+
"severity": "major",
124+
"location": {
125+
"path": "with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
126+
"lines": {
127+
"begin": 2
128+
}
129+
}
130+
},
131+
{
132+
"description": "Foo",
133+
"fingerprint": "e82b7e1f1d4255352b19ecefa9116a12f129c7edb4351cf2319285eccdb1565e",
134+
"severity": "major",
135+
"location": {
136+
"path": "with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
137+
"lines": {
138+
"begin": 4
139+
}
140+
}
141+
},
142+
{
143+
"description": "Bar\nBar2",
144+
"fingerprint": "52d22d9e64bd6c6257b7a0d170ed8c99482043aeedd68c52bac081a80da9800a",
145+
"severity": "major",
146+
"location": {
147+
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
148+
"lines": {
149+
"begin": 0
150+
}
151+
}
152+
},
153+
{
154+
"description": "Foo",
155+
"fingerprint": "93c79740ed8c6fbaac2087e54d6f6f67fc0918e3ff77840530f32e19857ef63c",
156+
"severity": "major",
157+
"location": {
158+
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
159+
"lines": {
160+
"begin": 1
161+
}
162+
}
163+
},
164+
{
165+
"description": "Bar\nBar2",
166+
"fingerprint": "829f6c782152fdac840b39208c5b519d18e51bff2c601b6197812fffb8bcd9ed",
167+
"severity": "major",
168+
"location": {
169+
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
170+
"lines": {
171+
"begin": 5
172+
}
173+
}
174+
}
175+
]',
176+
];
177+
114178
yield [
115179
'Multiple generic errors',
116180
1,

0 commit comments

Comments
 (0)