Skip to content

Commit 45e328f

Browse files
committed
Generators: don't print empty code comparisons/rows
When the documentation contained a `<code_comparison>`, but either both `<code>` `title` attributes were missing or both code blocks were empty, a table would be printed with an empty row. Along the same lines, if a `<code_comparison>` exists, but both the `<code>` `title` attributes are missing AND both code blocks are empty, a complete empty table would be printed. Neither is useful. This commit cleans up the table output to only print the table and rows when there is information to display. Includes updated test expectations.
1 parent a7d77cf commit 45e328f

15 files changed

+106
-143
lines changed

src/Generators/HTML.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,16 +410,29 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
410410
$second = str_replace('<em>', '<span class="code-comparison-highlight">', $second);
411411
$second = str_replace('</em>', '</span>', $second);
412412

413-
$output = ' <table class="code-comparison">'.PHP_EOL;
414-
$output .= ' <tr>'.PHP_EOL;
415-
$output .= " <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL;
416-
$output .= " <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL;
417-
$output .= ' </tr>'.PHP_EOL;
418-
$output .= ' <tr>'.PHP_EOL;
419-
$output .= " <td class=\"code-comparison-code\">$first</td>".PHP_EOL;
420-
$output .= " <td class=\"code-comparison-code\">$second</td>".PHP_EOL;
421-
$output .= ' </tr>'.PHP_EOL;
422-
$output .= ' </table>'.PHP_EOL;
413+
$titleRow = '';
414+
if ($firstTitle !== '' || $secondTitle !== '') {
415+
$titleRow .= ' <tr>'.PHP_EOL;
416+
$titleRow .= " <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL;
417+
$titleRow .= " <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL;
418+
$titleRow .= ' </tr>'.PHP_EOL;
419+
}
420+
421+
$codeRow = '';
422+
if ($first !== '' || $second !== '') {
423+
$codeRow .= ' <tr>'.PHP_EOL;
424+
$codeRow .= " <td class=\"code-comparison-code\">$first</td>".PHP_EOL;
425+
$codeRow .= " <td class=\"code-comparison-code\">$second</td>".PHP_EOL;
426+
$codeRow .= ' </tr>'.PHP_EOL;
427+
}
428+
429+
$output = '';
430+
if ($titleRow !== '' || $codeRow !== '') {
431+
$output = ' <table class="code-comparison">'.PHP_EOL;
432+
$output .= $titleRow;
433+
$output .= $codeRow;
434+
$output .= ' </table>'.PHP_EOL;
435+
}
423436

424437
return $output;
425438

src/Generators/Markdown.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,20 +264,33 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
264264
$second = str_replace('<em>', '', $second);
265265
$second = str_replace('</em>', '', $second);
266266

267-
$output = ' <table>'.PHP_EOL;
268-
$output .= ' <tr>'.PHP_EOL;
269-
$output .= " <th>$firstTitle</th>".PHP_EOL;
270-
$output .= " <th>$secondTitle</th>".PHP_EOL;
271-
$output .= ' </tr>'.PHP_EOL;
272-
$output .= ' <tr>'.PHP_EOL;
273-
$output .= '<td>'.PHP_EOL.PHP_EOL;
274-
$output .= " $first".PHP_EOL.PHP_EOL;
275-
$output .= '</td>'.PHP_EOL;
276-
$output .= '<td>'.PHP_EOL.PHP_EOL;
277-
$output .= " $second".PHP_EOL.PHP_EOL;
278-
$output .= '</td>'.PHP_EOL;
279-
$output .= ' </tr>'.PHP_EOL;
280-
$output .= ' </table>'.PHP_EOL;
267+
$titleRow = '';
268+
if ($firstTitle !== '' || $secondTitle !== '') {
269+
$titleRow .= ' <tr>'.PHP_EOL;
270+
$titleRow .= " <th>$firstTitle</th>".PHP_EOL;
271+
$titleRow .= " <th>$secondTitle</th>".PHP_EOL;
272+
$titleRow .= ' </tr>'.PHP_EOL;
273+
}
274+
275+
$codeRow = '';
276+
if ($first !== '' || $second !== '') {
277+
$codeRow .= ' <tr>'.PHP_EOL;
278+
$codeRow .= '<td>'.PHP_EOL.PHP_EOL;
279+
$codeRow .= " $first".PHP_EOL.PHP_EOL;
280+
$codeRow .= '</td>'.PHP_EOL;
281+
$codeRow .= '<td>'.PHP_EOL.PHP_EOL;
282+
$codeRow .= " $second".PHP_EOL.PHP_EOL;
283+
$codeRow .= '</td>'.PHP_EOL;
284+
$codeRow .= ' </tr>'.PHP_EOL;
285+
}
286+
287+
$output = '';
288+
if ($titleRow !== '' || $codeRow !== '') {
289+
$output .= ' <table>'.PHP_EOL;
290+
$output .= $titleRow;
291+
$output .= $codeRow;
292+
$output .= ' </table>'.PHP_EOL;
293+
}
281294

282295
return $output;
283296

src/Generators/Text.php

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -277,56 +277,66 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
277277
$second = str_replace('</em>', '', $second);
278278
$secondLines = explode("\n", $second);
279279

280-
$maxCodeLines = max(count($firstLines), count($secondLines));
281-
$maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
282-
283-
$output = str_repeat('-', 41);
284-
$output .= ' CODE COMPARISON ';
285-
$output .= str_repeat('-', 42).PHP_EOL;
286-
287-
for ($i = 0; $i < $maxTitleLines; $i++) {
288-
if (isset($firstTitleLines[$i]) === true) {
289-
$firstLineText = $firstTitleLines[$i];
290-
} else {
291-
$firstLineText = '';
292-
}
293-
294-
if (isset($secondTitleLines[$i]) === true) {
295-
$secondLineText = $secondTitleLines[$i];
296-
} else {
297-
$secondLineText = '';
298-
}
299-
300-
$output .= '| ';
301-
$output .= $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
302-
$output .= ' | ';
303-
$output .= $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
304-
$output .= ' |'.PHP_EOL;
305-
}//end for
306-
307-
$output .= str_repeat('-', 100).PHP_EOL;
280+
$titleRow = '';
281+
if ($firstTitle !== '' || $secondTitle !== '') {
282+
$maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
283+
for ($i = 0; $i < $maxTitleLines; $i++) {
284+
if (isset($firstTitleLines[$i]) === true) {
285+
$firstLineText = $firstTitleLines[$i];
286+
} else {
287+
$firstLineText = '';
288+
}
308289

309-
for ($i = 0; $i < $maxCodeLines; $i++) {
310-
if (isset($firstLines[$i]) === true) {
311-
$firstLineText = $firstLines[$i];
312-
} else {
313-
$firstLineText = '';
314-
}
290+
if (isset($secondTitleLines[$i]) === true) {
291+
$secondLineText = $secondTitleLines[$i];
292+
} else {
293+
$secondLineText = '';
294+
}
315295

316-
if (isset($secondLines[$i]) === true) {
317-
$secondLineText = $secondLines[$i];
318-
} else {
319-
$secondLineText = '';
320-
}
296+
$titleRow .= '| ';
297+
$titleRow .= $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
298+
$titleRow .= ' | ';
299+
$titleRow .= $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
300+
$titleRow .= ' |'.PHP_EOL;
301+
}//end for
302+
303+
$titleRow .= str_repeat('-', 100).PHP_EOL;
304+
}//end if
305+
306+
$codeRow = '';
307+
if ($first !== '' || $second !== '') {
308+
$maxCodeLines = max(count($firstLines), count($secondLines));
309+
for ($i = 0; $i < $maxCodeLines; $i++) {
310+
if (isset($firstLines[$i]) === true) {
311+
$firstLineText = $firstLines[$i];
312+
} else {
313+
$firstLineText = '';
314+
}
321315

322-
$output .= '| ';
323-
$output .= $firstLineText.str_repeat(' ', max(0, (47 - strlen($firstLineText))));
324-
$output .= '| ';
325-
$output .= $secondLineText.str_repeat(' ', max(0, (48 - strlen($secondLineText))));
326-
$output .= '|'.PHP_EOL;
327-
}//end for
316+
if (isset($secondLines[$i]) === true) {
317+
$secondLineText = $secondLines[$i];
318+
} else {
319+
$secondLineText = '';
320+
}
328321

329-
$output .= str_repeat('-', 100).PHP_EOL.PHP_EOL;
322+
$codeRow .= '| ';
323+
$codeRow .= $firstLineText.str_repeat(' ', max(0, (47 - strlen($firstLineText))));
324+
$codeRow .= '| ';
325+
$codeRow .= $secondLineText.str_repeat(' ', max(0, (48 - strlen($secondLineText))));
326+
$codeRow .= '|'.PHP_EOL;
327+
}//end for
328+
329+
$codeRow .= str_repeat('-', 100).PHP_EOL.PHP_EOL;
330+
}//end if
331+
332+
$output = '';
333+
if ($titleRow !== '' || $codeRow !== '') {
334+
$output = str_repeat('-', 41);
335+
$output .= ' CODE COMPARISON ';
336+
$output .= str_repeat('-', 42).PHP_EOL;
337+
$output .= $titleRow;
338+
$output .= $codeRow;
339+
}
330340

331341
return $output;
332342

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ <h2>Code Comparison, no code</h2>
7878
<td class="code-comparison-title">Valid: no code.</td>
7979
<td class="code-comparison-title">Invalid: no code.</td>
8080
</tr>
81-
<tr>
82-
<td class="code-comparison-code"></td>
83-
<td class="code-comparison-code"></td>
84-
</tr>
8581
</table>
8682
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
8783
</body>

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ This is a standard block.
88
<th>Valid: no code.</th>
99
<th>Invalid: no code.</th>
1010
</tr>
11-
<tr>
12-
<td>
13-
14-
15-
16-
</td>
17-
<td>
18-
19-
20-
21-
</td>
22-
</tr>
2311
</table>
2412

2513
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,3 @@ This is a standard block.
88
----------------------------------------- CODE COMPARISON ------------------------------------------
99
| Valid: no code. | Invalid: no code. |
1010
----------------------------------------------------------------------------------------------------
11-
| | |
12-
----------------------------------------------------------------------------------------------------
13-

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.html

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,6 @@ <h1>GeneratorTest Coding Standards</h1>
7373
<a name="Code-Comparison,-two-empty-code-elements" />
7474
<h2>Code Comparison, two empty code elements</h2>
7575
<p class="text">This doc has two code elements, but neither of them contain any information.</p>
76-
<table class="code-comparison">
77-
<tr>
78-
<td class="code-comparison-title"></td>
79-
<td class="code-comparison-title"></td>
80-
</tr>
81-
<tr>
82-
<td class="code-comparison-code"></td>
83-
<td class="code-comparison-code"></td>
84-
</tr>
85-
</table>
8676
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
8777
</body>
8878
</html>

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,5 @@
33
## Code Comparison, two empty code elements
44

55
This doc has two code elements, but neither of them contain any information.
6-
<table>
7-
<tr>
8-
<th></th>
9-
<th></th>
10-
</tr>
11-
<tr>
12-
<td>
13-
14-
15-
16-
</td>
17-
<td>
18-
19-
20-
21-
</td>
22-
</tr>
23-
</table>
246

257
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer)

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,3 @@
55

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

8-
----------------------------------------- CODE COMPARISON ------------------------------------------
9-
| | |
10-
----------------------------------------------------------------------------------------------------
11-
| | |
12-
----------------------------------------------------------------------------------------------------
13-

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ <h1>GeneratorTest Coding Standards</h1>
7474
<h2>Code Title, empty</h2>
7575
<p class="text">This is a standard block.</p>
7676
<table class="code-comparison">
77-
<tr>
78-
<td class="code-comparison-title"></td>
79-
<td class="code-comparison-title"></td>
80-
</tr>
8177
<tr>
8278
<td class="code-comparison-code">//&nbsp;Dummy.</td>
8379
<td class="code-comparison-code">//&nbsp;Dummy.</td>

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
This is a standard block.
66
<table>
77
<tr>
8-
<th></th>
9-
<th></th>
10-
</tr>
11-
<tr>
128
<td>
139

1410
// Dummy.

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
This is a standard block.
77

88
----------------------------------------- CODE COMPARISON ------------------------------------------
9-
| | |
10-
----------------------------------------------------------------------------------------------------
119
| // Dummy. | // Dummy. |
1210
----------------------------------------------------------------------------------------------------
1311

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ <h1>GeneratorTest Coding Standards</h1>
7474
<h2>Code Title, missing</h2>
7575
<p class="text">This is a standard block.</p>
7676
<table class="code-comparison">
77-
<tr>
78-
<td class="code-comparison-title"></td>
79-
<td class="code-comparison-title"></td>
80-
</tr>
8177
<tr>
8278
<td class="code-comparison-code">//&nbsp;Dummy.</td>
8379
<td class="code-comparison-code">//&nbsp;Dummy.</td>

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
This is a standard block.
66
<table>
77
<tr>
8-
<th></th>
9-
<th></th>
10-
</tr>
11-
<tr>
128
<td>
139

1410
// Dummy.

tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
This is a standard block.
77

88
----------------------------------------- CODE COMPARISON ------------------------------------------
9-
| | |
10-
----------------------------------------------------------------------------------------------------
119
| // Dummy. | // Dummy. |
1210
----------------------------------------------------------------------------------------------------
1311

0 commit comments

Comments
 (0)