Skip to content

Commit b44b30f

Browse files
committed
Reports: Fix word wrapping edge cases
The word wrapping in Code and Full reports did not correctly account for the ANSI color codes (producing differently wrapped text depending on the value of $showSources) and for the PHP_EOL constant values (producing differently wrapped text on Linux and Windows). Rewrite the code so that word wrapping is done first, and padding and colors are added afterwards. Also harmonize the implementation between the two reports.
1 parent d02c686 commit b44b30f

File tree

2 files changed

+45
-52
lines changed

2 files changed

+45
-52
lines changed

src/Reports/Code.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
153153

154154
// The maximum amount of space an error message can use.
155155
$maxErrorSpace = ($width - $errorPaddingLength);
156-
if ($showSources === true) {
157-
// Account for the chars used to print colors.
158-
$maxErrorSpace += 8;
159-
}
160156

161157
// Figure out the max report width we need and can use.
162158
$fileLength = strlen($file);
@@ -291,19 +287,33 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
291287
echo '] ';
292288
}
293289

294-
$message = $error['message'];
295-
$message = str_replace("\n", "\n".$errorPadding, $message);
296-
if ($showSources === true) {
297-
$message = "\033[1m".$message."\033[0m".' ('.$error['source'].')';
290+
$message = wordwrap($error['message'], $maxErrorSpace, PHP_EOL);
291+
$paddedMessage = '';
292+
// Add padding and colors to each line of the output.
293+
foreach (explode(PHP_EOL, $message) as $i => $msgLine) {
294+
if ($i !== 0) {
295+
$paddedMessage .= PHP_EOL.$errorPadding;
296+
}
297+
298+
if ($showSources === true) {
299+
$paddedMessage .= "\033[1m".$msgLine."\033[0m";
300+
} else {
301+
$paddedMessage .= $msgLine;
302+
}
298303
}
299304

300-
$errorMsg = wordwrap(
301-
$message,
302-
$maxErrorSpace,
303-
PHP_EOL.$errorPadding
304-
);
305+
if ($showSources === true) {
306+
// Add sniff code, taking care to manually wrap the line if needed.
307+
if ((strlen($msgLine) + strlen($error['source']) + 3) > $maxErrorSpace) {
308+
$paddedMessage .= PHP_EOL.$errorPadding;
309+
} else {
310+
$paddedMessage .= ' ';
311+
}
312+
313+
$paddedMessage .= '('.$error['source'].')';
314+
}
305315

306-
echo $errorMsg.PHP_EOL;
316+
echo $paddedMessage.PHP_EOL;
307317
}//end foreach
308318
}//end foreach
309319

src/Reports/Full.php

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -129,51 +129,34 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
129129
// The maximum amount of space an error message can use.
130130
$maxErrorSpace = ($width - $paddingLength - 1);
131131

132-
$beforeMsg = '';
133-
$afterMsg = '';
134-
if ($showSources === true) {
135-
$beforeMsg = "\033[1m";
136-
$afterMsg = "\033[0m";
137-
}
138-
139-
$beforeAfterLength = strlen($beforeMsg.$afterMsg);
140-
141132
foreach ($report['messages'] as $line => $lineErrors) {
142133
foreach ($lineErrors as $colErrors) {
143134
foreach ($colErrors as $error) {
144-
$errorMsg = wordwrap(
145-
$error['message'],
146-
$maxErrorSpace
147-
);
148-
149-
// Add the padding _after_ the wordwrap as the message itself may contain line breaks
150-
// and those lines will also need to receive padding.
151-
$errorMsg = str_replace("\n", $afterMsg.PHP_EOL.$paddingLine2.$beforeMsg, $errorMsg);
152-
$errorMsg = $beforeMsg.$errorMsg.$afterMsg;
153-
154-
if ($showSources === true) {
155-
$lastMsg = $errorMsg;
156-
$startPosLastLine = strrpos($errorMsg, PHP_EOL.$paddingLine2.$beforeMsg);
157-
if ($startPosLastLine !== false) {
158-
// Message is multiline. Grab the text of last line of the message, including the color codes.
159-
$lastMsg = substr($errorMsg, ($startPosLastLine + strlen(PHP_EOL.$paddingLine2)));
135+
$message = wordwrap($error['message'], $maxErrorSpace, PHP_EOL);
136+
$paddedMessage = '';
137+
// Add padding and colors to each line of the output.
138+
foreach (explode(PHP_EOL, $message) as $i => $msgLine) {
139+
if ($i !== 0) {
140+
$paddedMessage .= PHP_EOL.$paddingLine2;
160141
}
161142

162-
// When show sources is used, the message itself will be bolded, so we need to correct the length.
163-
$sourceSuffix = '('.$error['source'].')';
164-
165-
$lastMsgPlusSourceLength = strlen($lastMsg);
166-
// Add space + source suffix length.
167-
$lastMsgPlusSourceLength += (1 + strlen($sourceSuffix));
168-
// Correct for the color codes.
169-
$lastMsgPlusSourceLength -= $beforeAfterLength;
143+
if ($showSources === true) {
144+
$paddedMessage .= "\033[1m".$msgLine."\033[0m";
145+
} else {
146+
$paddedMessage .= $msgLine;
147+
}
148+
}
170149

171-
if ($lastMsgPlusSourceLength > $maxErrorSpace) {
172-
$errorMsg .= PHP_EOL.$paddingLine2.$sourceSuffix;
150+
if ($showSources === true) {
151+
// Add sniff code, taking care to manually wrap the line if needed.
152+
if ((strlen($msgLine) + strlen($error['source']) + 3) > $maxErrorSpace) {
153+
$paddedMessage .= PHP_EOL.$paddingLine2;
173154
} else {
174-
$errorMsg .= ' '.$sourceSuffix;
155+
$paddedMessage .= ' ';
175156
}
176-
}//end if
157+
158+
$paddedMessage .= '('.$error['source'].')';
159+
}
177160

178161
// The padding that goes on the front of the line.
179162
$padding = ($maxLineNumLength - strlen($line));
@@ -200,7 +183,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
200183
echo '] ';
201184
}
202185

203-
echo $errorMsg.PHP_EOL;
186+
echo $paddedMessage.PHP_EOL;
204187
}//end foreach
205188
}//end foreach
206189
}//end foreach

0 commit comments

Comments
 (0)