Skip to content

Commit

Permalink
Generators/Text::getFormattedCodeComparisonBlock(): minor refactor [2]
Browse files Browse the repository at this point in the history
Move yet more duplicate code to a dedicated `private` method.
  • Loading branch information
jrfnl committed Nov 6, 2024
1 parent 2537413 commit f147089
Showing 1 changed file with 40 additions and 42 deletions.
82 changes: 40 additions & 42 deletions src/Generators/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,53 +188,13 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)

$titleRow = '';
if ($firstTitleLines !== [] || $secondTitleLines !== []) {
$maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
for ($i = 0; $i < $maxTitleLines; $i++) {
if (isset($firstTitleLines[$i]) === true) {
$firstLineText = $firstTitleLines[$i];
} else {
$firstLineText = '';
}

if (isset($secondTitleLines[$i]) === true) {
$secondLineText = $secondTitleLines[$i];
} else {
$secondLineText = '';
}

$titleRow .= '| ';
$titleRow .= $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
$titleRow .= ' | ';
$titleRow .= $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
$titleRow .= ' |'.PHP_EOL;
}//end for

$titleRow = $this->linesToTableRows($firstTitleLines, $secondTitleLines);
$titleRow .= str_repeat('-', 100).PHP_EOL;
}//end if

$codeRow = '';
if ($firstLines !== [] || $secondLines !== []) {
$maxCodeLines = max(count($firstLines), count($secondLines));
for ($i = 0; $i < $maxCodeLines; $i++) {
if (isset($firstLines[$i]) === true) {
$firstLineText = $firstLines[$i];
} else {
$firstLineText = '';
}

if (isset($secondLines[$i]) === true) {
$secondLineText = $secondLines[$i];
} else {
$secondLineText = '';
}

$codeRow .= '| ';
$codeRow .= $firstLineText.str_repeat(' ', max(0, (47 - strlen($firstLineText))));
$codeRow .= '| ';
$codeRow .= $secondLineText.str_repeat(' ', max(0, (48 - strlen($secondLineText))));
$codeRow .= '|'.PHP_EOL;
}//end for

$codeRow = $this->linesToTableRows($firstLines, $secondLines);
$codeRow .= str_repeat('-', 100).PHP_EOL.PHP_EOL;
}//end if

Expand Down Expand Up @@ -297,4 +257,42 @@ private function codeToLines(DOMElement $codeElm)
}//end codeToLines()


/**
* Transform two sets of text lines into rows for use in an ASCII table.
*
* The sets may not contains an equal amount of lines, while the resulting rows should.
*
* @param array<string> $column1Lines Lines of text to place in column 1.
* @param array<string> $column2Lines Lines of text to place in column 2.
*
* @return string
*/
private function linesToTableRows(array $column1Lines, array $column2Lines)
{
$maxLines = max(count($column1Lines), count($column2Lines));

$rows = '';
for ($i = 0; $i < $maxLines; $i++) {
$column1Text = '';
if (isset($column1Lines[$i]) === true) {
$column1Text = $column1Lines[$i];
}

$column2Text = '';
if (isset($column2Lines[$i]) === true) {
$column2Text = $column2Lines[$i];
}

$rows .= '| ';
$rows .= $column1Text.str_repeat(' ', max(0, (47 - strlen($column1Text))));
$rows .= '| ';
$rows .= $column2Text.str_repeat(' ', max(0, (48 - strlen($column2Text))));
$rows .= '|'.PHP_EOL;
}//end for

return $rows;

}//end linesToTableRows()


}//end class

0 comments on commit f147089

Please sign in to comment.