Skip to content

Commit 9121852

Browse files
committed
Merge branch 'feature/generator-text-line-wrapping-bug-fix' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 234688d + b109358 commit 9121852

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/Generators/Text.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,42 @@ protected function printTextBlock(\DOMNode $node)
7575
$text = str_replace('<em>', '*', $text);
7676
$text = str_replace('</em>', '*', $text);
7777

78-
$lines = [];
79-
$tempLine = '';
80-
$words = explode(' ', $text);
78+
$nodeLines = explode("\n", $text);
79+
$lines = [];
80+
81+
foreach ($nodeLines as $currentLine) {
82+
$currentLine = trim($currentLine);
83+
if ($currentLine === '') {
84+
// The text contained a blank line. Respect this.
85+
$lines[] = '';
86+
continue;
87+
}
8188

82-
foreach ($words as $word) {
83-
if (strlen($tempLine.$word) >= 99) {
84-
if (strlen($tempLine.$word) === 99) {
85-
// Adding the extra space will push us to the edge
86-
// so we are done.
87-
$lines[] = $tempLine.$word;
88-
$tempLine = '';
89-
} else if (strlen($tempLine.$word) === 100) {
89+
$tempLine = '';
90+
$words = explode(' ', $currentLine);
91+
92+
foreach ($words as $word) {
93+
$currentLength = strlen($tempLine.$word);
94+
if ($currentLength < 99) {
95+
$tempLine .= $word.' ';
96+
continue;
97+
}
98+
99+
if ($currentLength === 99 || $currentLength === 100) {
90100
// We are already at the edge, so we are done.
91101
$lines[] = $tempLine.$word;
92102
$tempLine = '';
93103
} else {
94104
$lines[] = rtrim($tempLine);
95105
$tempLine = $word.' ';
96106
}
97-
} else {
98-
$tempLine .= $word.' ';
107+
}//end foreach
108+
109+
if ($tempLine !== '') {
110+
$lines[] = rtrim($tempLine);
99111
}
100112
}//end foreach
101113

102-
if ($tempLine !== '') {
103-
$lines[] = rtrim($tempLine);
104-
}
105-
106114
echo implode(PHP_EOL, $lines).PHP_EOL.PHP_EOL;
107115

108116
}//end printTextBlock()

0 commit comments

Comments
 (0)