Skip to content

Commit 5b83e1f

Browse files
committed
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.
1 parent 7763e2e commit 5b83e1f

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

src/Reports/Full.php

+31-11
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
115115
// The maximum amount of space an error message can use.
116116
$maxErrorSpace = ($width - $paddingLength - 1);
117117

118+
if ($showSources === true) {
119+
$beforeMsg = "\033[1m";
120+
$afterMsg = "\033[0m";
121+
} else {
122+
$beforeMsg = '';
123+
$afterMsg = '';
124+
}
125+
118126
foreach ($report['messages'] as $line => $lineErrors) {
119127
foreach ($lineErrors as $column => $colErrors) {
120128
foreach ($colErrors as $error) {
@@ -128,23 +136,35 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
128136
$lastLine = (count($msgLines) - 1);
129137
foreach ($msgLines as $k => $msgLine) {
130138
if ($k === 0) {
131-
if ($showSources === true) {
132-
$errorMsg .= "\033[1m";
133-
}
139+
$errorMsg .= $beforeMsg;
134140
} else {
135-
$errorMsg .= PHP_EOL.$paddingLine2;
136-
}
137-
138-
if ($k === $lastLine && $showSources === true) {
139-
$msgLine .= "\033[0m".' ('.$error['source'].')';
141+
$errorMsg .= $afterMsg.PHP_EOL.$paddingLine2.$beforeMsg;
140142
}
141143

142-
$errorMsg .= wordwrap(
144+
$wrappedLines = wordwrap(
143145
$msgLine,
144146
$maxErrorSpace,
145-
PHP_EOL.$paddingLine2
147+
$afterMsg.PHP_EOL.$paddingLine2.$beforeMsg
146148
);
147-
}
149+
$errorMsg .= $wrappedLines;
150+
151+
if ($k === $lastLine) {
152+
$errorMsg .= $afterMsg;
153+
if ($showSources === true) {
154+
$lastLineLength = strlen($wrappedLines);
155+
$lastNewlinePos = strrpos($wrappedLines, PHP_EOL);
156+
if ($lastNewlinePos !== false) {
157+
$lastLineLength -= ($lastNewlinePos + strlen(PHP_EOL.$paddingLine2.$beforeMsg));
158+
}
159+
160+
if (($lastLineLength + strlen($error['source']) + 3) > $maxErrorSpace) {
161+
$errorMsg .= PHP_EOL.$paddingLine2.'('.$error['source'].')';
162+
} else {
163+
$errorMsg .= ' ('.$error['source'].')';
164+
}
165+
}
166+
}
167+
}//end foreach
148168

149169
// The padding that goes on the front of the line.
150170
$padding = ($maxLineNumLength - strlen($line));

0 commit comments

Comments
 (0)