From 0febbfbc9f61555c00c7c1084270b2b2c90a6f8c Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 14 Nov 2023 16:49:25 -0500 Subject: [PATCH] Fix two line wrapping bugs in default report formatter The first is that the ANSI escape codes applied to bold the message when `-s` is used were not being taken into account when wrapping the lines for width, causing some lines to be wrapped unnecessarily. The second is that when lines were wrapped in the middle of a long message, the `|` characters making up the table were being bolded along with the message. --- src/Reports/Full.php | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/Reports/Full.php b/src/Reports/Full.php index f6db5833be..b0666aedd2 100644 --- a/src/Reports/Full.php +++ b/src/Reports/Full.php @@ -115,6 +115,14 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, // The maximum amount of space an error message can use. $maxErrorSpace = ($width - $paddingLength - 1); + if ($showSources === true) { + $beforeMsg = "\033[1m"; + $afterMsg = "\033[0m"; + } else { + $beforeMsg = ''; + $afterMsg = ''; + } + foreach ($report['messages'] as $line => $lineErrors) { foreach ($lineErrors as $column => $colErrors) { foreach ($colErrors as $error) { @@ -128,23 +136,35 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, $lastLine = (count($msgLines) - 1); foreach ($msgLines as $k => $msgLine) { if ($k === 0) { - if ($showSources === true) { - $errorMsg .= "\033[1m"; - } + $errorMsg .= $beforeMsg; } else { - $errorMsg .= PHP_EOL.$paddingLine2; - } - - if ($k === $lastLine && $showSources === true) { - $msgLine .= "\033[0m".' ('.$error['source'].')'; + $errorMsg .= $afterMsg.PHP_EOL.$paddingLine2.$beforeMsg; } - $errorMsg .= wordwrap( + $wrappedLines = wordwrap( $msgLine, $maxErrorSpace, - PHP_EOL.$paddingLine2 + $afterMsg.PHP_EOL.$paddingLine2.$beforeMsg ); - } + $errorMsg .= $wrappedLines; + + if ($k === $lastLine) { + $errorMsg .= $afterMsg; + if ($showSources === true) { + $lastLineLength = strlen($wrappedLines); + $lastNewlinePos = strrpos($wrappedLines, PHP_EOL); + if ($lastNewlinePos !== false) { + $lastLineLength -= ($lastNewlinePos + strlen(PHP_EOL.$paddingLine2.$beforeMsg)); + } + + if (($lastLineLength + strlen($error['source']) + 3) > $maxErrorSpace) { + $errorMsg .= PHP_EOL.$paddingLine2.'('.$error['source'].')'; + } else { + $errorMsg .= ' ('.$error['source'].')'; + } + } + } + }//end foreach // The padding that goes on the front of the line. $padding = ($maxLineNumLength - strlen($line));