Skip to content

Commit 9c6ad66

Browse files
committed
Generators/Text::getFormattedCodeComparisonBlock(): minor refactor [2]
Move yet more duplicate code to a dedicated `private` method.
1 parent 97b965d commit 9c6ad66

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

src/Generators/Text.php

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -188,53 +188,13 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
188188

189189
$titleRow = '';
190190
if ($firstTitleLines !== [] || $secondTitleLines !== []) {
191-
$maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
192-
for ($i = 0; $i < $maxTitleLines; $i++) {
193-
if (isset($firstTitleLines[$i]) === true) {
194-
$firstLineText = $firstTitleLines[$i];
195-
} else {
196-
$firstLineText = '';
197-
}
198-
199-
if (isset($secondTitleLines[$i]) === true) {
200-
$secondLineText = $secondTitleLines[$i];
201-
} else {
202-
$secondLineText = '';
203-
}
204-
205-
$titleRow .= '| ';
206-
$titleRow .= $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
207-
$titleRow .= ' | ';
208-
$titleRow .= $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
209-
$titleRow .= ' |'.PHP_EOL;
210-
}//end for
211-
191+
$titleRow = $this->linesToTableRows($firstTitleLines, $secondTitleLines);
212192
$titleRow .= str_repeat('-', 100).PHP_EOL;
213193
}//end if
214194

215195
$codeRow = '';
216196
if ($firstLines !== [] || $secondLines !== []) {
217-
$maxCodeLines = max(count($firstLines), count($secondLines));
218-
for ($i = 0; $i < $maxCodeLines; $i++) {
219-
if (isset($firstLines[$i]) === true) {
220-
$firstLineText = $firstLines[$i];
221-
} else {
222-
$firstLineText = '';
223-
}
224-
225-
if (isset($secondLines[$i]) === true) {
226-
$secondLineText = $secondLines[$i];
227-
} else {
228-
$secondLineText = '';
229-
}
230-
231-
$codeRow .= '| ';
232-
$codeRow .= $firstLineText.str_repeat(' ', max(0, (47 - strlen($firstLineText))));
233-
$codeRow .= '| ';
234-
$codeRow .= $secondLineText.str_repeat(' ', max(0, (48 - strlen($secondLineText))));
235-
$codeRow .= '|'.PHP_EOL;
236-
}//end for
237-
197+
$codeRow = $this->linesToTableRows($firstLines, $secondLines);
238198
$codeRow .= str_repeat('-', 100).PHP_EOL.PHP_EOL;
239199
}//end if
240200

@@ -297,4 +257,42 @@ private function codeToLines(DOMElement $codeElm)
297257
}//end codeToLines()
298258

299259

260+
/**
261+
* Transform two sets of text lines into rows for use in an ASCII table.
262+
*
263+
* The sets may not contains an equal amount of lines, while the resulting rows should.
264+
*
265+
* @param array<string> $column1Lines Lines of text to place in column 1.
266+
* @param array<string> $column2Lines Lines of text to place in column 2.
267+
*
268+
* @return string
269+
*/
270+
private function linesToTableRows(array $column1Lines, array $column2Lines)
271+
{
272+
$maxLines = max(count($column1Lines), count($column2Lines));
273+
274+
$rows = '';
275+
for ($i = 0; $i < $maxLines; $i++) {
276+
$column1Text = '';
277+
if (isset($column1Lines[$i]) === true) {
278+
$column1Text = $column1Lines[$i];
279+
}
280+
281+
$column2Text = '';
282+
if (isset($column2Lines[$i]) === true) {
283+
$column2Text = $column2Lines[$i];
284+
}
285+
286+
$rows .= '| ';
287+
$rows .= $column1Text.str_repeat(' ', max(0, (47 - strlen($column1Text))));
288+
$rows .= '| ';
289+
$rows .= $column2Text.str_repeat(' ', max(0, (48 - strlen($column2Text))));
290+
$rows .= '|'.PHP_EOL;
291+
}//end for
292+
293+
return $rows;
294+
295+
}//end linesToTableRows()
296+
297+
300298
}//end class

0 commit comments

Comments
 (0)