Skip to content

Commit 8ae740c

Browse files
schlndhondrejmirtes
authored andcommitted
fix unnecessary wrapping in TableErrorFormatter
Fixes #13317
1 parent 6a97443 commit 8ae740c

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

patches/OutputFormatter.patch

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,37 @@
3131

3232
return strtr($output, ["\0" => '\\', '\\<' => '<', '\\>' => '>']);
3333
}
34-
@@ -253,8 +256,8 @@ class OutputFormatter implements WrappableOutputFormatterInterface
34+
@@ -253,8 +256,20 @@ class OutputFormatter implements WrappableOutputFormatterInterface
3535
}
3636

3737
if ($currentLineLength) {
3838
- $prefix = substr($text, 0, $i = $width - $currentLineLength)."\n";
3939
- $text = substr($text, $i);
40-
+ $prefix = Helper::substr($text, 0, $i = $width - $currentLineLength)."\n";
41-
+ $text = Helper::substr($text, $i);
40+
+ $lines = explode("\n", $text, 2);
41+
+ $prefix = Helper::substr($lines[0], 0, $i = $width - $currentLineLength)."\n";
42+
+ $text = Helper::substr($lines[0], $i);
43+
+
44+
+ if (isset($lines[1])) {
45+
+ // $prefix may contain the full first line in which the \n is already a part of $prefix.
46+
+ if ('' !== $text) {
47+
+ $text .= "\n";
48+
+ }
49+
+
50+
+ $text .= $lines[1];
51+
+ }
52+
+
53+
+ unset($lines);
4254
} else {
4355
$prefix = '';
4456
}
45-
@@ -270,7 +273,7 @@ class OutputFormatter implements WrappableOutputFormatterInterface
57+
@@ -269,8 +284,8 @@ class OutputFormatter implements WrappableOutputFormatterInterface
58+
4659
$lines = explode("\n", $text);
4760

48-
foreach ($lines as $line) {
61+
- foreach ($lines as $line) {
4962
- $currentLineLength += \strlen($line);
50-
+ $currentLineLength += Helper::length($line);
63+
+ foreach ($lines as $i => $line) {
64+
+ $currentLineLength = 0 === $i ? $currentLineLength + Helper::length($line) : Helper::length($line);
5165
if ($width <= $currentLineLength) {
5266
$currentLineLength = 0;
5367
}

tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,51 @@ public function testBug13292(): void
350350
self::expectNotToPerformAssertions();
351351
}
352352

353+
public function testBug13317(): void
354+
{
355+
putenv('COLUMNS=170');
356+
$formatter = $this->createErrorFormatter(null);
357+
$formatter->formatErrors(
358+
new AnalysisResult(
359+
[
360+
new Error(
361+
'Property bla::$error_params (non-empty-list<string>|null) is never assigned non-empty-list<string> so it can be removed from the property type.',
362+
'bla.php',
363+
6,
364+
identifier: 'property.unusedType',
365+
),
366+
],
367+
[],
368+
[],
369+
[],
370+
[],
371+
false,
372+
null,
373+
true,
374+
0,
375+
false,
376+
[],
377+
),
378+
$this->getOutput(),
379+
);
380+
$this->assertSame(
381+
<<<'TABLE'
382+
------ -------------------------------------------------------------------------------------------------------------------------------------------------
383+
Line bla.php
384+
------ -------------------------------------------------------------------------------------------------------------------------------------------------
385+
6 Property bla::$error_params (non-empty-list<string>|null) is never assigned non-empty-list<string> so it can be removed from the property type.
386+
🪪 property.unusedType
387+
------ -------------------------------------------------------------------------------------------------------------------------------------------------
388+
389+
390+
[ERROR] Found 1 error
391+
392+
393+
TABLE,
394+
$this->getOutputContent(),
395+
);
396+
}
397+
353398
private function createErrorFormatter(?string $editorUrl, ?string $editorUrlTitle = null): TableErrorFormatter
354399
{
355400
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');

0 commit comments

Comments
 (0)