From 45e328fdd1d6894ccf13c61a3b69511817df35c8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 6 Nov 2024 11:37:28 +0100 Subject: [PATCH] Generators: don't print empty code comparisons/rows When the documentation contained a ``, but either both `` `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 `` exists, but both the `` `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. --- src/Generators/HTML.php | 33 ++++-- src/Generators/Markdown.php | 41 ++++--- src/Generators/Text.php | 102 ++++++++++-------- ...ctedOutputInvalidCodeComparisonNoCode.html | 4 - ...pectedOutputInvalidCodeComparisonNoCode.md | 12 --- ...ectedOutputInvalidCodeComparisonNoCode.txt | 3 - ...InvalidCodeComparisonTwoEmptyCodeElms.html | 10 -- ...utInvalidCodeComparisonTwoEmptyCodeElms.md | 18 ---- ...tInvalidCodeComparisonTwoEmptyCodeElms.txt | 6 -- .../ExpectedOutputInvalidCodeTitleEmpty.html | 4 - .../ExpectedOutputInvalidCodeTitleEmpty.md | 4 - .../ExpectedOutputInvalidCodeTitleEmpty.txt | 2 - ...ExpectedOutputInvalidCodeTitleMissing.html | 4 - .../ExpectedOutputInvalidCodeTitleMissing.md | 4 - .../ExpectedOutputInvalidCodeTitleMissing.txt | 2 - 15 files changed, 106 insertions(+), 143 deletions(-) diff --git a/src/Generators/HTML.php b/src/Generators/HTML.php index b7e209f46a..5290280b75 100644 --- a/src/Generators/HTML.php +++ b/src/Generators/HTML.php @@ -410,16 +410,29 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node) $second = str_replace('', '', $second); $second = str_replace('', '', $second); - $output = ' '.PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= " ".PHP_EOL; - $output .= " ".PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= " ".PHP_EOL; - $output .= " ".PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= '
$firstTitle$secondTitle
$first$second
'.PHP_EOL; + $titleRow = ''; + if ($firstTitle !== '' || $secondTitle !== '') { + $titleRow .= ' '.PHP_EOL; + $titleRow .= " $firstTitle".PHP_EOL; + $titleRow .= " $secondTitle".PHP_EOL; + $titleRow .= ' '.PHP_EOL; + } + + $codeRow = ''; + if ($first !== '' || $second !== '') { + $codeRow .= ' '.PHP_EOL; + $codeRow .= " $first".PHP_EOL; + $codeRow .= " $second".PHP_EOL; + $codeRow .= ' '.PHP_EOL; + } + + $output = ''; + if ($titleRow !== '' || $codeRow !== '') { + $output = ' '.PHP_EOL; + $output .= $titleRow; + $output .= $codeRow; + $output .= '
'.PHP_EOL; + } return $output; diff --git a/src/Generators/Markdown.php b/src/Generators/Markdown.php index d03d06a115..0fb40a4e29 100644 --- a/src/Generators/Markdown.php +++ b/src/Generators/Markdown.php @@ -264,20 +264,33 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node) $second = str_replace('', '', $second); $second = str_replace('', '', $second); - $output = ' '.PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= " ".PHP_EOL; - $output .= " ".PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= ''.PHP_EOL; - $output .= ''.PHP_EOL; - $output .= ' '.PHP_EOL; - $output .= '
$firstTitle$secondTitle
'.PHP_EOL.PHP_EOL; - $output .= " $first".PHP_EOL.PHP_EOL; - $output .= ''.PHP_EOL.PHP_EOL; - $output .= " $second".PHP_EOL.PHP_EOL; - $output .= '
'.PHP_EOL; + $titleRow = ''; + if ($firstTitle !== '' || $secondTitle !== '') { + $titleRow .= ' '.PHP_EOL; + $titleRow .= " $firstTitle".PHP_EOL; + $titleRow .= " $secondTitle".PHP_EOL; + $titleRow .= ' '.PHP_EOL; + } + + $codeRow = ''; + if ($first !== '' || $second !== '') { + $codeRow .= ' '.PHP_EOL; + $codeRow .= ''.PHP_EOL.PHP_EOL; + $codeRow .= " $first".PHP_EOL.PHP_EOL; + $codeRow .= ''.PHP_EOL; + $codeRow .= ''.PHP_EOL.PHP_EOL; + $codeRow .= " $second".PHP_EOL.PHP_EOL; + $codeRow .= ''.PHP_EOL; + $codeRow .= ' '.PHP_EOL; + } + + $output = ''; + if ($titleRow !== '' || $codeRow !== '') { + $output .= ' '.PHP_EOL; + $output .= $titleRow; + $output .= $codeRow; + $output .= '
'.PHP_EOL; + } return $output; diff --git a/src/Generators/Text.php b/src/Generators/Text.php index 486e76c7f9..7dc519012e 100644 --- a/src/Generators/Text.php +++ b/src/Generators/Text.php @@ -277,56 +277,66 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node) $second = str_replace('', '', $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; diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.html b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.html index c1db1a7bb7..ff4d115361 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.html +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.html @@ -78,10 +78,6 @@

Code Comparison, no code

Valid: no code. Invalid: no code. - - - -
Documentation generated on #REDACTED# by PHP_CodeSniffer #VERSION#
diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.md b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.md index b64dbf01f2..afbf106352 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.md +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.md @@ -8,18 +8,6 @@ This is a standard block. Valid: no code. Invalid: no code. - - - - - - - - - - - - Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer) diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.txt b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.txt index a99fdbc6c3..f6091cabdc 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.txt +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonNoCode.txt @@ -8,6 +8,3 @@ This is a standard block. ----------------------------------------- CODE COMPARISON ------------------------------------------ | Valid: no code. | Invalid: no code. | ---------------------------------------------------------------------------------------------------- -| | | ----------------------------------------------------------------------------------------------------- - diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.html b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.html index f1c8f75bad..f51eeac69a 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.html +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.html @@ -73,16 +73,6 @@

GeneratorTest Coding Standards

Code Comparison, two empty code elements

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

- - - - - - - - - -
diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.md b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.md index 424c38af72..7d1387677e 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.md +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.md @@ -3,23 +3,5 @@ ## Code Comparison, two empty code elements This doc has two code elements, but neither of them contain any information. - - - - - - - - - -
- - - - - - - -
Documentation generated on *REDACTED* by [PHP_CodeSniffer *VERSION*](https://github.com/PHPCSStandards/PHP_CodeSniffer) diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.txt b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.txt index 4508467076..775e7ceebf 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.txt +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeComparisonTwoEmptyCodeElms.txt @@ -5,9 +5,3 @@ This doc has two code elements, but neither of them contain any information. ------------------------------------------ CODE COMPARISON ------------------------------------------ -| | | ----------------------------------------------------------------------------------------------------- -| | | ----------------------------------------------------------------------------------------------------- - diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.html b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.html index ae9fb7aa68..f73849e5d0 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.html +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.html @@ -74,10 +74,6 @@

GeneratorTest Coding Standards

Code Title, empty

This is a standard block.

- - - - diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.md b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.md index 312c65d486..728ccc0bcf 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.md +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.md @@ -5,10 +5,6 @@ This is a standard block.
// Dummy. // Dummy.
- - - -
// Dummy. diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.txt b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.txt index 904a1485ea..d3ac256b11 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.txt +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.txt @@ -6,8 +6,6 @@ This is a standard block. ----------------------------------------- CODE COMPARISON ------------------------------------------ -| | | ----------------------------------------------------------------------------------------------------- | // Dummy. | // Dummy. | ---------------------------------------------------------------------------------------------------- diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.html b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.html index 9121273f3d..7b8060cda2 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.html +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.html @@ -74,10 +74,6 @@

GeneratorTest Coding Standards

Code Title, missing

This is a standard block.

- - - - diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.md b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.md index ddd6f6b7a4..98e5822951 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.md +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.md @@ -5,10 +5,6 @@ This is a standard block.
// Dummy. // Dummy.
- - - -
// Dummy. diff --git a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.txt b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.txt index 95adef296c..3de51f08d7 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.txt +++ b/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleMissing.txt @@ -6,8 +6,6 @@ This is a standard block. ----------------------------------------- CODE COMPARISON ------------------------------------------ -| | | ----------------------------------------------------------------------------------------------------- | // Dummy. | // Dummy. | ----------------------------------------------------------------------------------------------------