From 4cb02aed17e9397da4ccfc7c39f65afd59e63f85 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 6 Nov 2024 19:00:41 +0100 Subject: [PATCH 1/2] Generators/Text: minor simplification No need for multiple calls to `str_replace()` when a single call will do. --- src/Generators/Text.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Generators/Text.php b/src/Generators/Text.php index 10507e7adf..e2ebd9032c 100644 --- a/src/Generators/Text.php +++ b/src/Generators/Text.php @@ -130,8 +130,7 @@ protected function getFormattedTextBlock(DOMNode $node) } $text = trim($text); - $text = str_replace('', '*', $text); - $text = str_replace('', '*', $text); + $text = str_replace(['', ''], '*', $text); $nodeLines = explode("\n", $text); $lines = []; @@ -243,8 +242,7 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node) $firstTitleLines[] = $tempTitle; } - $first = str_replace('', '', $first); - $first = str_replace('', '', $first); + $first = str_replace(['', ''], '', $first); $firstLines = explode("\n", $first); $second = trim($secondCodeElm->nodeValue); @@ -278,8 +276,7 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node) $secondTitleLines[] = $tempTitle; } - $second = str_replace('', '', $second); - $second = str_replace('', '', $second); + $second = str_replace(['', ''], '', $second); $secondLines = explode("\n", $second); $titleRow = ''; From 4e8113f80712686fdd82472eca3bdf5dd871b7c6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 6 Nov 2024 19:17:07 +0100 Subject: [PATCH 2/2] Generators/Text::getFormattedTextBlock(): simplify the logic There's absolutely no need for custom word-wrapping logic when PHP contains a function which can do this perfectly well. --- src/Generators/Text.php | 38 +++----------------------------------- 1 file changed, 3 insertions(+), 35 deletions(-) diff --git a/src/Generators/Text.php b/src/Generators/Text.php index e2ebd9032c..8d10e55470 100644 --- a/src/Generators/Text.php +++ b/src/Generators/Text.php @@ -133,42 +133,10 @@ protected function getFormattedTextBlock(DOMNode $node) $text = str_replace(['', ''], '*', $text); $nodeLines = explode("\n", $text); - $lines = []; - - foreach ($nodeLines as $currentLine) { - $currentLine = trim($currentLine); - if ($currentLine === '') { - // The text contained a blank line. Respect this. - $lines[] = ''; - continue; - } - - $tempLine = ''; - $words = explode(' ', $currentLine); - - foreach ($words as $word) { - $currentLength = strlen($tempLine.$word); - if ($currentLength < 99) { - $tempLine .= $word.' '; - continue; - } - - if ($currentLength === 99 || $currentLength === 100) { - // We are already at the edge, so we are done. - $lines[] = $tempLine.$word; - $tempLine = ''; - } else { - $lines[] = rtrim($tempLine); - $tempLine = $word.' '; - } - }//end foreach - - if ($tempLine !== '') { - $lines[] = rtrim($tempLine); - } - }//end foreach + $nodeLines = array_map('trim', $nodeLines); + $text = implode(PHP_EOL, $nodeLines); - return implode(PHP_EOL, $lines).PHP_EOL.PHP_EOL; + return wordwrap($text, 100, PHP_EOL).PHP_EOL.PHP_EOL; }//end getFormattedTextBlock()