Skip to content

Generators: don't print empty code comparisons/rows #821

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

Merged
merged 1 commit into from
Feb 15, 2025
Merged
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
33 changes: 23 additions & 10 deletions src/Generators/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,16 +410,29 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
$second = str_replace('<em>', '<span class="code-comparison-highlight">', $second);
$second = str_replace('</em>', '</span>', $second);

$output = ' <table class="code-comparison">'.PHP_EOL;
$output .= ' <tr>'.PHP_EOL;
$output .= " <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL;
$output .= " <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL;
$output .= ' </tr>'.PHP_EOL;
$output .= ' <tr>'.PHP_EOL;
$output .= " <td class=\"code-comparison-code\">$first</td>".PHP_EOL;
$output .= " <td class=\"code-comparison-code\">$second</td>".PHP_EOL;
$output .= ' </tr>'.PHP_EOL;
$output .= ' </table>'.PHP_EOL;
$titleRow = '';
if ($firstTitle !== '' || $secondTitle !== '') {
$titleRow .= ' <tr>'.PHP_EOL;
$titleRow .= " <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL;
$titleRow .= " <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL;
$titleRow .= ' </tr>'.PHP_EOL;
}

$codeRow = '';
if ($first !== '' || $second !== '') {
$codeRow .= ' <tr>'.PHP_EOL;
$codeRow .= " <td class=\"code-comparison-code\">$first</td>".PHP_EOL;
$codeRow .= " <td class=\"code-comparison-code\">$second</td>".PHP_EOL;
$codeRow .= ' </tr>'.PHP_EOL;
}

$output = '';
if ($titleRow !== '' || $codeRow !== '') {
$output = ' <table class="code-comparison">'.PHP_EOL;
$output .= $titleRow;
$output .= $codeRow;
$output .= ' </table>'.PHP_EOL;
}

return $output;

Expand Down
41 changes: 27 additions & 14 deletions src/Generators/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,33 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
$second = str_replace('<em>', '', $second);
$second = str_replace('</em>', '', $second);

$output = ' <table>'.PHP_EOL;
$output .= ' <tr>'.PHP_EOL;
$output .= " <th>$firstTitle</th>".PHP_EOL;
$output .= " <th>$secondTitle</th>".PHP_EOL;
$output .= ' </tr>'.PHP_EOL;
$output .= ' <tr>'.PHP_EOL;
$output .= '<td>'.PHP_EOL.PHP_EOL;
$output .= " $first".PHP_EOL.PHP_EOL;
$output .= '</td>'.PHP_EOL;
$output .= '<td>'.PHP_EOL.PHP_EOL;
$output .= " $second".PHP_EOL.PHP_EOL;
$output .= '</td>'.PHP_EOL;
$output .= ' </tr>'.PHP_EOL;
$output .= ' </table>'.PHP_EOL;
$titleRow = '';
if ($firstTitle !== '' || $secondTitle !== '') {
$titleRow .= ' <tr>'.PHP_EOL;
$titleRow .= " <th>$firstTitle</th>".PHP_EOL;
$titleRow .= " <th>$secondTitle</th>".PHP_EOL;
$titleRow .= ' </tr>'.PHP_EOL;
}

$codeRow = '';
if ($first !== '' || $second !== '') {
$codeRow .= ' <tr>'.PHP_EOL;
$codeRow .= '<td>'.PHP_EOL.PHP_EOL;
$codeRow .= " $first".PHP_EOL.PHP_EOL;
$codeRow .= '</td>'.PHP_EOL;
$codeRow .= '<td>'.PHP_EOL.PHP_EOL;
$codeRow .= " $second".PHP_EOL.PHP_EOL;
$codeRow .= '</td>'.PHP_EOL;
$codeRow .= ' </tr>'.PHP_EOL;
}

$output = '';
if ($titleRow !== '' || $codeRow !== '') {
$output .= ' <table>'.PHP_EOL;
$output .= $titleRow;
$output .= $codeRow;
$output .= ' </table>'.PHP_EOL;
}

return $output;

Expand Down
102 changes: 56 additions & 46 deletions src/Generators/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,56 +277,66 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
$second = str_replace('</em>', '', $second);
$secondLines = explode("\n", $second);

$maxCodeLines = max(count($firstLines), count($secondLines));
$maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));

$output = str_repeat('-', 41);
$output .= ' CODE COMPARISON ';
$output .= str_repeat('-', 42).PHP_EOL;

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 = '';
}

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

$output .= str_repeat('-', 100).PHP_EOL;
$titleRow = '';
if ($firstTitle !== '' || $secondTitle !== '') {
$maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
for ($i = 0; $i < $maxTitleLines; $i++) {
if (isset($firstTitleLines[$i]) === true) {
$firstLineText = $firstTitleLines[$i];
} else {
$firstLineText = '';
}

for ($i = 0; $i < $maxCodeLines; $i++) {
if (isset($firstLines[$i]) === true) {
$firstLineText = $firstLines[$i];
} else {
$firstLineText = '';
}
if (isset($secondTitleLines[$i]) === true) {
$secondLineText = $secondTitleLines[$i];
} else {
$secondLineText = '';
}

if (isset($secondLines[$i]) === true) {
$secondLineText = $secondLines[$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 .= str_repeat('-', 100).PHP_EOL;
}//end if

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

$output .= '| ';
$output .= $firstLineText.str_repeat(' ', max(0, (47 - strlen($firstLineText))));
$output .= '| ';
$output .= $secondLineText.str_repeat(' ', max(0, (48 - strlen($secondLineText))));
$output .= '|'.PHP_EOL;
}//end for
if (isset($secondLines[$i]) === true) {
$secondLineText = $secondLines[$i];
} else {
$secondLineText = '';
}

$output .= str_repeat('-', 100).PHP_EOL.PHP_EOL;
$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 .= str_repeat('-', 100).PHP_EOL.PHP_EOL;
}//end if

$output = '';
if ($titleRow !== '' || $codeRow !== '') {
$output = str_repeat('-', 41);
$output .= ' CODE COMPARISON ';
$output .= str_repeat('-', 42).PHP_EOL;
$output .= $titleRow;
$output .= $codeRow;
}

return $output;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ <h2>Code Comparison, no code</h2>
<td class="code-comparison-title">Valid: no code.</td>
<td class="code-comparison-title">Invalid: no code.</td>
</tr>
<tr>
<td class="code-comparison-code"></td>
<td class="code-comparison-code"></td>
</tr>
</table>
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ This is a standard block.
<th>Valid: no code.</th>
<th>Invalid: no code.</th>
</tr>
<tr>
<td>



</td>
<td>



</td>
</tr>
</table>

Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ This is a standard block.
----------------------------------------- CODE COMPARISON ------------------------------------------
| Valid: no code. | Invalid: no code. |
----------------------------------------------------------------------------------------------------
| | |
----------------------------------------------------------------------------------------------------

Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,6 @@ <h1>GeneratorTest Coding Standards</h1>
<a name="Code-Comparison,-two-empty-code-elements" />
<h2>Code Comparison, two empty code elements</h2>
<p class="text">This doc has two code elements, but neither of them contain any information.</p>
<table class="code-comparison">
<tr>
<td class="code-comparison-title"></td>
<td class="code-comparison-title"></td>
</tr>
<tr>
<td class="code-comparison-code"></td>
<td class="code-comparison-code"></td>
</tr>
</table>
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,5 @@
## Code Comparison, two empty code elements

This doc has two code elements, but neither of them contain any information.
<table>
<tr>
<th></th>
<th></th>
</tr>
<tr>
<td>



</td>
<td>



</td>
</tr>
</table>

Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,3 @@

This doc has two code elements, but neither of them contain any information.

----------------------------------------- CODE COMPARISON ------------------------------------------
| | |
----------------------------------------------------------------------------------------------------
| | |
----------------------------------------------------------------------------------------------------

Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ <h1>GeneratorTest Coding Standards</h1>
<h2>Code Title, empty</h2>
<p class="text">This is a standard block.</p>
<table class="code-comparison">
<tr>
<td class="code-comparison-title"></td>
<td class="code-comparison-title"></td>
</tr>
<tr>
<td class="code-comparison-code">//&nbsp;Dummy.</td>
<td class="code-comparison-code">//&nbsp;Dummy.</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
This is a standard block.
<table>
<tr>
<th></th>
<th></th>
</tr>
<tr>
<td>

// Dummy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
This is a standard block.

----------------------------------------- CODE COMPARISON ------------------------------------------
| | |
----------------------------------------------------------------------------------------------------
| // Dummy. | // Dummy. |
----------------------------------------------------------------------------------------------------

Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ <h1>GeneratorTest Coding Standards</h1>
<h2>Code Title, missing</h2>
<p class="text">This is a standard block.</p>
<table class="code-comparison">
<tr>
<td class="code-comparison-title"></td>
<td class="code-comparison-title"></td>
</tr>
<tr>
<td class="code-comparison-code">//&nbsp;Dummy.</td>
<td class="code-comparison-code">//&nbsp;Dummy.</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
This is a standard block.
<table>
<tr>
<th></th>
<th></th>
</tr>
<tr>
<td>

// Dummy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
This is a standard block.

----------------------------------------- CODE COMPARISON ------------------------------------------
| | |
----------------------------------------------------------------------------------------------------
| // Dummy. | // Dummy. |
----------------------------------------------------------------------------------------------------

Loading