Skip to content

fix unnecessary wrapping in TableErrorFormatter #4170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions patches/OutputFormatter.patch
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,37 @@

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

if ($currentLineLength) {
- $prefix = substr($text, 0, $i = $width - $currentLineLength)."\n";
- $text = substr($text, $i);
+ $prefix = Helper::substr($text, 0, $i = $width - $currentLineLength)."\n";
+ $text = Helper::substr($text, $i);
+ $lines = explode("\n", $text, 2);
+ $prefix = Helper::substr($lines[0], 0, $i = $width - $currentLineLength)."\n";
+ $text = Helper::substr($lines[0], $i);
+
+ if (isset($lines[1])) {
+ // $prefix may contain the full first line in which the \n is already a part of $prefix.
+ if ('' !== $text) {
+ $text .= "\n";
+ }
+
+ $text .= $lines[1];
+ }
+
+ unset($lines);
} else {
$prefix = '';
}
@@ -270,7 +273,7 @@ class OutputFormatter implements WrappableOutputFormatterInterface
@@ -269,8 +284,8 @@ class OutputFormatter implements WrappableOutputFormatterInterface

$lines = explode("\n", $text);

foreach ($lines as $line) {
- foreach ($lines as $line) {
- $currentLineLength += \strlen($line);
+ $currentLineLength += Helper::length($line);
+ foreach ($lines as $i => $line) {
+ $currentLineLength = 0 === $i ? $currentLineLength + Helper::length($line) : Helper::length($line);
if ($width <= $currentLineLength) {
$currentLineLength = 0;
}
Expand Down
45 changes: 45 additions & 0 deletions tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,51 @@ public function testBug13292(): void
self::expectNotToPerformAssertions();
}

public function testBug13317(): void
{
putenv('COLUMNS=170');
$formatter = $this->createErrorFormatter(null);
$formatter->formatErrors(
new AnalysisResult(
[
new Error(
'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.',
'bla.php',
6,
identifier: 'property.unusedType',
),
],
[],
[],
[],
[],
false,
null,
true,
0,
false,
[],
),
$this->getOutput(),
);
$this->assertSame(
<<<'TABLE'
------ -------------------------------------------------------------------------------------------------------------------------------------------------
Line bla.php
------ -------------------------------------------------------------------------------------------------------------------------------------------------
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.
🪪 property.unusedType
------ -------------------------------------------------------------------------------------------------------------------------------------------------


[ERROR] Found 1 error


TABLE,
$this->getOutputContent(),
);
}

private function createErrorFormatter(?string $editorUrl, ?string $editorUrlTitle = null): TableErrorFormatter
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
Expand Down
Loading