Skip to content

Commit 7e9d490

Browse files
authored
Merge pull request #821 from PHPCSStandards/feature/generators-no-empty-code-comparisons
Generators: don't print empty code comparisons/rows
2 parents 580a36c + 45e328f commit 7e9d490

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
@@ -415,16 +415,29 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
415415
$second = str_replace('<em>', '<span class="code-comparison-highlight">', $second);
416416
$second = str_replace('</em>', '</span>', $second);
417417

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

429442
return $output;
430443

src/Generators/Markdown.php

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

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

287300
return $output;
288301

src/Generators/Text.php

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

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

314-
for ($i = 0; $i < $maxCodeLines; $i++) {
315-
if (isset($firstLines[$i]) === true) {
316-
$firstLineText = $firstLines[$i];
317-
} else {
318-
$firstLineText = '';
319-
}
295+
if (isset($secondTitleLines[$i]) === true) {
296+
$secondLineText = $secondTitleLines[$i];
297+
} else {
298+
$secondLineText = '';
299+
}
320300

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

327-
$output .= '| ';
328-
$output .= $firstLineText.str_repeat(' ', max(0, (47 - strlen($firstLineText))));
329-
$output .= '| ';
330-
$output .= $secondLineText.str_repeat(' ', max(0, (48 - strlen($secondLineText))));
331-
$output .= '|'.PHP_EOL;
332-
}//end for
321+
if (isset($secondLines[$i]) === true) {
322+
$secondLineText = $secondLines[$i];
323+
} else {
324+
$secondLineText = '';
325+
}
333326

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

336346
return $output;
337347

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)