Skip to content

Commit 696d5be

Browse files
committed
feat: Assure Gitlab reports have integer line properties
Ref: phpstan/phpstan-src#616
1 parent 6202def commit 696d5be

File tree

4 files changed

+72
-8
lines changed

4 files changed

+72
-8
lines changed

src/Error.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Error
1010
protected $message;
1111
/** @var string|null */
1212
protected $file;
13-
/** @var int */
13+
/** @var int|null */
1414
protected $line;
1515
/** @var int */
1616
protected $severity;
@@ -29,7 +29,7 @@ class Error
2929
public function __construct(
3030
string $message,
3131
?string $file,
32-
int $line,
32+
?int $line,
3333
int $severity = Error::LEVEL_ERROR,
3434
?string $tip = null
3535
) {
@@ -50,7 +50,7 @@ public function getFile(): ?string
5050
return $this->file;
5151
}
5252

53-
public function getLine(): int
53+
public function getLine(): ?int
5454
{
5555
return $this->line;
5656
}

src/ErrorFormatter/GitlabErrorFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types = 1);
44

55
/*
6-
* (c) Copyright (c) 2016-2020 Ondřej Mirtes <[email protected]>
6+
* (c) Copyright (c) 2016-2021 Ondřej Mirtes <[email protected]>
77
*
88
* This source file is subject to the MIT license.
99
*
@@ -72,7 +72,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
7272
'location' => [
7373
'path' => $this->relativePathHelper->getRelativePath($file),
7474
'lines' => [
75-
'begin' => $fileSpecificError->getLine(),
75+
'begin' => $fileSpecificError->getLine() ?? 0,
7676
],
7777
],
7878
];

tests/ErrorFormatter/GitlabFormatterTest.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types = 1);
44

55
/*
6-
* (c) Copyright (c) 2016-2020 Ondřej Mirtes <[email protected]>
6+
* (c) Copyright (c) 2016-2021 Ondřej Mirtes <[email protected]>
77
*
88
* This source file is subject to the MIT license.
99
*
@@ -134,6 +134,69 @@ public function dataFormatterOutputProvider(): iterable
134134
}
135135
]',
136136
];
137+
yield [
138+
'Multiple file errors, including error with line=null',
139+
1,
140+
5,
141+
0,
142+
'[
143+
{
144+
"description": "Bar\nBar2",
145+
"fingerprint": "034b4afbfb347494c14e396ed8327692f58be4cd27e8aff5f19f4194934db7c9",
146+
"severity": "major",
147+
"location": {
148+
"path": "with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
149+
"lines": {
150+
"begin": 2
151+
}
152+
}
153+
},
154+
{
155+
"description": "Foo",
156+
"fingerprint": "e82b7e1f1d4255352b19ecefa9116a12f129c7edb4351cf2319285eccdb1565e",
157+
"severity": "major",
158+
"location": {
159+
"path": "with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
160+
"lines": {
161+
"begin": 4
162+
}
163+
}
164+
},
165+
{
166+
"description": "Bar\nBar2",
167+
"fingerprint": "52d22d9e64bd6c6257b7a0d170ed8c99482043aeedd68c52bac081a80da9800a",
168+
"severity": "major",
169+
"location": {
170+
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
171+
"lines": {
172+
"begin": 0
173+
}
174+
}
175+
},
176+
{
177+
"description": "Foo",
178+
"fingerprint": "93c79740ed8c6fbaac2087e54d6f6f67fc0918e3ff77840530f32e19857ef63c",
179+
"severity": "major",
180+
"location": {
181+
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
182+
"lines": {
183+
"begin": 1
184+
}
185+
}
186+
},
187+
{
188+
"description": "Bar\nBar2",
189+
"fingerprint": "829f6c782152fdac840b39208c5b519d18e51bff2c601b6197812fffb8bcd9ed",
190+
"severity": "major",
191+
"location": {
192+
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
193+
"lines": {
194+
"begin": 5
195+
}
196+
}
197+
}
198+
]',
199+
];
137200

138201
yield [
139202
'Multiple generic errors',

tests/ErrorFormatterTestCase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types = 1);
44

55
/*
6-
* (c) Copyright (c) 2016-2020 Ondřej Mirtes <[email protected]>
6+
* (c) Copyright (c) 2016-2021 Ondřej Mirtes <[email protected]>
77
*
88
* This source file is subject to the MIT license.
99
*
@@ -98,7 +98,7 @@ protected function getOutputContent(): string
9898

9999
protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors, int $numWarnings = 0): AnalysisResult
100100
{
101-
if ($numFileErrors > 4 || $numFileErrors < 0 || $numGenericErrors > 2 || $numGenericErrors < 0) {
101+
if ($numFileErrors > 5 || $numFileErrors < 0 || $numGenericErrors > 2 || $numGenericErrors < 0) {
102102
throw new \Exception('Test case error');
103103
}
104104

@@ -108,6 +108,7 @@ protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors,
108108
new Error('Foo', self::DIRECTORY_PATH . '/foo.php', 1),
109109
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/foo.php', 5),
110110
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/folder with unicode 😃/file name with "spaces" and unicode 😃.php', 2),
111+
new Error("Bar\nBar2", self::DIRECTORY_PATH . '/foo.php', null),
111112
],
112113
0,
113114
$numFileErrors

0 commit comments

Comments
 (0)