From d921d067702acb1cc0451753dcdf3be142b29aed Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 3 Mar 2022 17:13:18 +0100 Subject: [PATCH] Fix Incorrectly nested style tag found for narrow terminals --- src/Command/ErrorsConsoleStyle.php | 15 ++++++++- .../TableErrorFormatterTest.php | 31 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Command/ErrorsConsoleStyle.php b/src/Command/ErrorsConsoleStyle.php index ad8a6342b2..a128fe24e5 100644 --- a/src/Command/ErrorsConsoleStyle.php +++ b/src/Command/ErrorsConsoleStyle.php @@ -4,12 +4,15 @@ use OndraM\CiDetector\CiDetector; use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Terminal; +use function array_unshift; use function explode; use function implode; +use function sprintf; use function str_starts_with; use function strlen; use function wordwrap; @@ -65,12 +68,22 @@ public function table(array $headers, array $rows): void // https://github.com/symfony/symfony/issues/45520 // https://github.com/symfony/symfony/issues/45521 $headers = $this->wrap($headers, $terminalWidth, $maxHeaderWidth); + foreach ($headers as $i => $header) { + $newHeader = []; + foreach (explode("\n", $header) as $h) { + $newHeader[] = sprintf('%s', $h); + } + + $headers[$i] = implode("\n", $newHeader); + } + foreach ($rows as $i => $row) { $rows[$i] = $this->wrap($row, $terminalWidth, $maxHeaderWidth); } $table = $this->createTable(); - $table->setHeaders($headers); + array_unshift($rows, new TableSeparator()); + array_unshift($rows, $headers); $table->setRows($rows); $table->render(); diff --git a/tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php index cc6cfc4e3f..0e3e23acc0 100644 --- a/tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php @@ -7,12 +7,18 @@ use PHPStan\File\FuzzyRelativePathHelper; use PHPStan\File\NullRelativePathHelper; use PHPStan\Testing\ErrorFormatterTestCase; +use function putenv; use function sprintf; use const PHP_VERSION_ID; class TableErrorFormatterTest extends ErrorFormatterTestCase { + protected function tearDown(): void + { + putenv('COLUMNS'); + } + public function dataFormatterOutputProvider(): iterable { yield [ @@ -172,4 +178,29 @@ public function testEditorUrlWithTrait(): void $this->assertStringContainsString('Bar.php', $this->getOutputContent()); } + public function testBug6727(): void + { + putenv('COLUMNS=30'); + $formatter = new TableErrorFormatter(new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/'), false, null); + $formatter->formatErrors( + new AnalysisResult( + [ + new Error( + 'Method MissingTypehintPromotedProperties\Foo::__construct() has parameter $foo with no value type specified in iterable type array.', + '/var/www/html/app/src/Foo.php (in context of class App\Foo\Bar)', + 5, + ), + ], + [], + [], + [], + false, + null, + true, + ), + $this->getOutput(), + ); + self::expectNotToPerformAssertions(); + } + }