diff --git a/src/Reports/Full.php b/src/Reports/Full.php index f6db5833be..ee2b5af203 100644 --- a/src/Reports/Full.php +++ b/src/Reports/Full.php @@ -115,36 +115,51 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, // The maximum amount of space an error message can use. $maxErrorSpace = ($width - $paddingLength - 1); + $beforeMsg = ''; + $afterMsg = ''; + if ($showSources === true) { + $beforeMsg = "\033[1m"; + $afterMsg = "\033[0m"; + } + + $beforeAfterLength = strlen($beforeMsg.$afterMsg); + foreach ($report['messages'] as $line => $lineErrors) { foreach ($lineErrors as $column => $colErrors) { foreach ($colErrors as $error) { - $message = $error['message']; - $msgLines = [$message]; - if (strpos($message, "\n") !== false) { - $msgLines = explode("\n", $message); - } + $errorMsg = wordwrap( + $error['message'], + $maxErrorSpace + ); - $errorMsg = ''; - $lastLine = (count($msgLines) - 1); - foreach ($msgLines as $k => $msgLine) { - if ($k === 0) { - if ($showSources === true) { - $errorMsg .= "\033[1m"; - } - } else { - $errorMsg .= PHP_EOL.$paddingLine2; - } + // Add the padding _after_ the wordwrap as the message itself may contain line breaks + // and those lines will also need to receive padding. + $errorMsg = str_replace("\n", $afterMsg.PHP_EOL.$paddingLine2.$beforeMsg, $errorMsg); + $errorMsg = $beforeMsg.$errorMsg.$afterMsg; - if ($k === $lastLine && $showSources === true) { - $msgLine .= "\033[0m".' ('.$error['source'].')'; + if ($showSources === true) { + $lastMsg = $errorMsg; + $startPosLastLine = strrpos($errorMsg, PHP_EOL.$paddingLine2.$beforeMsg); + if ($startPosLastLine !== false) { + // Message is multiline. Grab the text of last line of the message, including the color codes. + $lastMsg = substr($errorMsg, ($startPosLastLine + strlen(PHP_EOL.$paddingLine2))); } - $errorMsg .= wordwrap( - $msgLine, - $maxErrorSpace, - PHP_EOL.$paddingLine2 - ); - } + // When show sources is used, the message itself will be bolded, so we need to correct the length. + $sourceSuffix = '('.$error['source'].')'; + + $lastMsgPlusSourceLength = strlen($lastMsg); + // Add space + source suffix length. + $lastMsgPlusSourceLength += (1 + strlen($sourceSuffix)); + // Correct for the color codes. + $lastMsgPlusSourceLength -= $beforeAfterLength; + + if ($lastMsgPlusSourceLength > $maxErrorSpace) { + $errorMsg .= PHP_EOL.$paddingLine2.$sourceSuffix; + } else { + $errorMsg .= ' '.$sourceSuffix; + } + }//end if // The padding that goes on the front of the line. $padding = ($maxLineNumLength - strlen($line));