Skip to content

Commit 4bc8fb3

Browse files
LeoVieondrejmirtes
authored andcommitted
#8086 Add colon as prefix of line number in table report, when running in VSCode terminal
1 parent da248b2 commit 4bc8fb3

2 files changed

Lines changed: 75 additions & 31 deletions

File tree

‎src/Command/ErrorFormatter/TableErrorFormatter.php‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Symfony\Component\Console\Formatter\OutputFormatter;
1212
use function array_map;
1313
use function count;
14+
use function getenv;
1415
use function is_string;
1516
use function sprintf;
1617
use function str_replace;
@@ -88,7 +89,7 @@ public function formatErrors(
8889
$message .= "\n✏️ <href=" . OutputFormatter::escape($url) . '>' . $this->relativePathHelper->getRelativePath($editorFile) . '</>';
8990
}
9091
$rows[] = [
91-
(string) $error->getLine(),
92+
$this->formatLineNumber($error->getLine()),
9293
$message,
9394
];
9495
}
@@ -119,4 +120,18 @@ public function formatErrors(
119120
return $analysisResult->getTotalErrorsCount() > 0 ? 1 : 0;
120121
}
121122

123+
private function formatLineNumber(?int $lineNumber): string
124+
{
125+
if ($lineNumber === null) {
126+
return '';
127+
}
128+
129+
$isRunningInVSCodeTerminal = getenv('TERM_PROGRAM') === 'vscode';
130+
if ($isRunningInVSCodeTerminal) {
131+
return ':' . $lineNumber;
132+
}
133+
134+
return (string) $lineNumber;
135+
}
136+
122137
}

‎tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php‎

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,24 @@ protected function tearDown(): void
2828
public function dataFormatterOutputProvider(): iterable
2929
{
3030
yield [
31-
'No errors',
32-
0,
33-
0,
34-
0,
35-
'
31+
'message' => 'No errors',
32+
'exitCode' => 0,
33+
'numFileErrors' => 0,
34+
'numGenericErrors' => 0,
35+
'extraEnvVars' => [],
36+
'expected' => '
3637
[OK] No errors
3738
3839
',
3940
];
4041

4142
yield [
42-
'One file error',
43-
1,
44-
1,
45-
0,
46-
' ------ -------------------------------------------------------------------
43+
'message' => 'One file error',
44+
'exitCode' => 1,
45+
'numFileErrors' => 1,
46+
'numGenericErrors' => 0,
47+
'extraEnvVars' => [],
48+
'expected' => ' ------ -------------------------------------------------------------------
4749
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
4850
------ -------------------------------------------------------------------
4951
4 Foo
@@ -56,11 +58,12 @@ public function dataFormatterOutputProvider(): iterable
5658
];
5759

5860
yield [
59-
'One generic error',
60-
1,
61-
0,
62-
1,
63-
' -- ---------------------
61+
'message' => 'One generic error',
62+
'exitCode' => 1,
63+
'numFileErrors' => 0,
64+
'numGenericErrors' => 1,
65+
'extraEnvVars' => [],
66+
'expected' => ' -- ---------------------
6467
Error
6568
-- ---------------------
6669
first generic error
@@ -73,11 +76,12 @@ public function dataFormatterOutputProvider(): iterable
7376
];
7477

7578
yield [
76-
'Multiple file errors',
77-
1,
78-
4,
79-
0,
80-
' ------ -------------------------------------------------------------------
79+
'message' => 'Multiple file errors',
80+
'exitCode' => 1,
81+
'numFileErrors' => 4,
82+
'numGenericErrors' => 0,
83+
'extraEnvVars' => [],
84+
'expected' => ' ------ -------------------------------------------------------------------
8185
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
8286
------ -------------------------------------------------------------------
8387
2 Bar
@@ -100,11 +104,12 @@ public function dataFormatterOutputProvider(): iterable
100104
];
101105

102106
yield [
103-
'Multiple generic errors',
104-
1,
105-
0,
106-
2,
107-
' -- ----------------------
107+
'message' => 'Multiple generic errors',
108+
'exitCode' => 1,
109+
'numFileErrors' => 0,
110+
'numGenericErrors' => 2,
111+
'extraEnvVars' => [],
112+
'expected' => ' -- ----------------------
108113
Error
109114
-- ----------------------
110115
first generic error
@@ -118,11 +123,12 @@ public function dataFormatterOutputProvider(): iterable
118123
];
119124

120125
yield [
121-
'Multiple file, multiple generic errors',
122-
1,
123-
4,
124-
2,
125-
' ------ -------------------------------------------------------------------
126+
'message' => 'Multiple file, multiple generic errors',
127+
'exitCode' => 1,
128+
'numFileErrors' => 4,
129+
'numGenericErrors' => 2,
130+
'extraEnvVars' => [],
131+
'expected' => ' ------ -------------------------------------------------------------------
126132
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
127133
------ -------------------------------------------------------------------
128134
2 Bar
@@ -148,6 +154,24 @@ public function dataFormatterOutputProvider(): iterable
148154
149155
[ERROR] Found 6 errors
150156
157+
',
158+
];
159+
160+
yield [
161+
'message' => 'One file error, called via Visual Studio Code',
162+
'exitCode' => 1,
163+
'numFileErrors' => 1,
164+
'numGenericErrors' => 0,
165+
'extraEnvVars' => ['TERM_PROGRAM=vscode'],
166+
'expected' => ' ------ -------------------------------------------------------------------
167+
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
168+
------ -------------------------------------------------------------------
169+
:4 Foo
170+
------ -------------------------------------------------------------------
171+
172+
173+
[ERROR] Found 1 error
174+
151175
',
152176
];
153177
}
@@ -161,6 +185,7 @@ public function testFormatErrors(
161185
int $exitCode,
162186
int $numFileErrors,
163187
int $numGenericErrors,
188+
array $extraEnvVars,
164189
string $expected,
165190
): void
166191
{
@@ -169,6 +194,10 @@ public function testFormatErrors(
169194
}
170195
$formatter = $this->createErrorFormatter(null);
171196

197+
foreach ($extraEnvVars as $envVar) {
198+
putenv($envVar);
199+
}
200+
172201
$this->assertSame($exitCode, $formatter->formatErrors(
173202
$this->getAnalysisResult($numFileErrors, $numGenericErrors),
174203
$this->getOutput(),

0 commit comments

Comments
 (0)