Skip to content

Reports: Fix word wrapping edge cases #654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/Reports/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,

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

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

$message = $error['message'];
$message = str_replace("\n", "\n".$errorPadding, $message);
if ($showSources === true) {
$message = "\033[1m".$message."\033[0m".' ('.$error['source'].')';
$message = wordwrap($error['message'], $maxErrorSpace, PHP_EOL);
$paddedMessage = '';
// Add padding and colors to each line of the output.
foreach (explode(PHP_EOL, $message) as $i => $msgLine) {
if ($i !== 0) {
$paddedMessage .= PHP_EOL.$errorPadding;
}

if ($showSources === true) {
$paddedMessage .= "\033[1m".$msgLine."\033[0m";
} else {
$paddedMessage .= $msgLine;
}
}

$errorMsg = wordwrap(
$message,
$maxErrorSpace,
PHP_EOL.$errorPadding
);
if ($showSources === true) {
// Add sniff code, taking care to manually wrap the line if needed.
if ((strlen($msgLine) + strlen($error['source']) + 3) > $maxErrorSpace) {
$paddedMessage .= PHP_EOL.$errorPadding;
} else {
$paddedMessage .= ' ';
}

$paddedMessage .= '('.$error['source'].')';
}

echo $errorMsg.PHP_EOL;
echo $paddedMessage.PHP_EOL;
}//end foreach
}//end foreach

Expand Down
59 changes: 21 additions & 38 deletions src/Reports/Full.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,51 +129,34 @@ 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 $colErrors) {
foreach ($colErrors as $error) {
$errorMsg = wordwrap(
$error['message'],
$maxErrorSpace
);

// 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 ($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)));
$message = wordwrap($error['message'], $maxErrorSpace, PHP_EOL);
$paddedMessage = '';
// Add padding and colors to each line of the output.
foreach (explode(PHP_EOL, $message) as $i => $msgLine) {
if ($i !== 0) {
$paddedMessage .= 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 ($showSources === true) {
$paddedMessage .= "\033[1m".$msgLine."\033[0m";
} else {
$paddedMessage .= $msgLine;
}
}

if ($lastMsgPlusSourceLength > $maxErrorSpace) {
$errorMsg .= PHP_EOL.$paddingLine2.$sourceSuffix;
if ($showSources === true) {
// Add sniff code, taking care to manually wrap the line if needed.
if ((strlen($msgLine) + strlen($error['source']) + 3) > $maxErrorSpace) {
$paddedMessage .= PHP_EOL.$paddingLine2;
} else {
$errorMsg .= ' '.$sourceSuffix;
$paddedMessage .= ' ';
}
}//end if

$paddedMessage .= '('.$error['source'].')';
}

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

echo $errorMsg.PHP_EOL;
echo $paddedMessage.PHP_EOL;
}//end foreach
}//end foreach
}//end foreach
Expand Down