Skip to content

Commit b270e99

Browse files
committed
Generators/Text: don't print title if there is no content
This refactors the class to _retrieve_ the intended output, instead of echo-ing it out directly and validates whether it makes sense to print anything at all about a sniff before sending the output to screen. It deprecates the following methods, which will be removed in PHPCS 4.0: * `printTitle()` in favour of `getFormattedTitle()` * `printTextBlock()` in favour of `getFormattedTextBlock()` * `printCodeComparisonBlock()` in favour of `getFormattedCodeComparisonBlock()`
1 parent dc996b9 commit b270e99

File tree

5 files changed

+99
-45
lines changed

5 files changed

+99
-45
lines changed

src/Generators/Text.php

Lines changed: 97 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ class Text extends Generator
3030
*/
3131
public function processSniff(DOMNode $doc)
3232
{
33-
$this->printTitle($doc);
34-
33+
$content = '';
3534
foreach ($doc->childNodes as $node) {
3635
if ($node->nodeName === 'standard') {
37-
$this->printTextBlock($node);
36+
$content .= $this->getFormattedTextBlock($node);
3837
} else if ($node->nodeName === 'code_comparison') {
39-
$this->printCodeComparisonBlock($node);
38+
$content .= $this->getFormattedCodeComparisonBlock($node);
4039
}
4140
}
4241

42+
if (trim($content) !== '') {
43+
echo $this->getFormattedTitle($doc), $content;
44+
}
45+
4346
}//end processSniff()
4447

4548

@@ -50,32 +53,76 @@ public function processSniff(DOMNode $doc)
5053
* It represents the "documentation" tag in the XML
5154
* standard file.
5255
*
56+
* @deprecated 3.12.0 Use Text::getFormattedTitle() instead.
57+
*
58+
* @codeCoverageIgnore
59+
*
5360
* @return void
5461
*/
5562
protected function printTitle(DOMNode $doc)
63+
{
64+
echo $this->getFormattedTitle($doc);
65+
66+
}//end printTitle()
67+
68+
69+
/**
70+
* Format the title area for a single sniff.
71+
*
72+
* @param \DOMNode $doc The DOMNode object for the sniff.
73+
* It represents the "documentation" tag in the XML
74+
* standard file.
75+
*
76+
* @since 3.12.0 Replaces the deprecated Text::printTitle() method.
77+
*
78+
* @return string
79+
*/
80+
protected function getFormattedTitle(DOMNode $doc)
5681
{
5782
$title = $this->getTitle($doc);
5883
$standard = $this->ruleset->name;
5984
$displayTitle = "$standard CODING STANDARD: $title";
6085
$titleLength = strlen($displayTitle);
6186

62-
echo PHP_EOL;
63-
echo str_repeat('-', ($titleLength + 4));
64-
echo strtoupper(PHP_EOL."| $displayTitle |".PHP_EOL);
65-
echo str_repeat('-', ($titleLength + 4));
66-
echo PHP_EOL.PHP_EOL;
87+
$output = PHP_EOL;
88+
$output .= str_repeat('-', ($titleLength + 4));
89+
$output .= strtoupper(PHP_EOL."| $displayTitle |".PHP_EOL);
90+
$output .= str_repeat('-', ($titleLength + 4));
91+
$output .= PHP_EOL.PHP_EOL;
6792

68-
}//end printTitle()
93+
return $output;
94+
95+
}//end getFormattedTitle()
6996

7097

7198
/**
7299
* Print a text block found in a standard.
73100
*
74101
* @param \DOMNode $node The DOMNode object for the text block.
75102
*
103+
* @deprecated 3.12.0 Use Text::getFormattedTextBlock() instead.
104+
*
105+
* @codeCoverageIgnore
106+
*
76107
* @return void
77108
*/
78109
protected function printTextBlock(DOMNode $node)
110+
{
111+
echo $this->getFormattedTextBlock($node);
112+
113+
}//end printTextBlock()
114+
115+
116+
/**
117+
* Format a text block found in a standard.
118+
*
119+
* @param \DOMNode $node The DOMNode object for the text block.
120+
*
121+
* @since 3.12.0 Replaces the deprecated Text::printTextBlock() method.
122+
*
123+
* @return string
124+
*/
125+
protected function getFormattedTextBlock(DOMNode $node)
79126
{
80127
$text = trim($node->nodeValue);
81128
$text = str_replace('<em>', '*', $text);
@@ -117,19 +164,39 @@ protected function printTextBlock(DOMNode $node)
117164
}
118165
}//end foreach
119166

120-
echo implode(PHP_EOL, $lines).PHP_EOL.PHP_EOL;
167+
return implode(PHP_EOL, $lines).PHP_EOL.PHP_EOL;
121168

122-
}//end printTextBlock()
169+
}//end getFormattedTextBlock()
123170

124171

125172
/**
126173
* Print a code comparison block found in a standard.
127174
*
128175
* @param \DOMNode $node The DOMNode object for the code comparison block.
129176
*
177+
* @deprecated 3.12.0 Use Text::getFormattedCodeComparisonBlock() instead.
178+
*
179+
* @codeCoverageIgnore
180+
*
130181
* @return void
131182
*/
132183
protected function printCodeComparisonBlock(DOMNode $node)
184+
{
185+
echo $this->getFormattedCodeComparisonBlock($node);
186+
187+
}//end printCodeComparisonBlock()
188+
189+
190+
/**
191+
* Format a code comparison block found in a standard.
192+
*
193+
* @param \DOMNode $node The DOMNode object for the code comparison block.
194+
*
195+
* @since 3.12.0 Replaces the deprecated Text::printCodeComparisonBlock() method.
196+
*
197+
* @return string
198+
*/
199+
protected function getFormattedCodeComparisonBlock(DOMNode $node)
133200
{
134201
$codeBlocks = $node->getElementsByTagName('code');
135202
$first = trim($codeBlocks->item(0)->nodeValue);
@@ -205,9 +272,9 @@ protected function printCodeComparisonBlock(DOMNode $node)
205272
$maxCodeLines = max(count($firstLines), count($secondLines));
206273
$maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
207274

208-
echo str_repeat('-', 41);
209-
echo ' CODE COMPARISON ';
210-
echo str_repeat('-', 42).PHP_EOL;
275+
$output = str_repeat('-', 41);
276+
$output .= ' CODE COMPARISON ';
277+
$output .= str_repeat('-', 42).PHP_EOL;
211278

212279
for ($i = 0; $i < $maxTitleLines; $i++) {
213280
if (isset($firstTitleLines[$i]) === true) {
@@ -222,14 +289,14 @@ protected function printCodeComparisonBlock(DOMNode $node)
222289
$secondLineText = '';
223290
}
224291

225-
echo '| ';
226-
echo $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
227-
echo ' | ';
228-
echo $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
229-
echo ' |'.PHP_EOL;
292+
$output .= '| ';
293+
$output .= $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
294+
$output .= ' | ';
295+
$output .= $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
296+
$output .= ' |'.PHP_EOL;
230297
}//end for
231298

232-
echo str_repeat('-', 100).PHP_EOL;
299+
$output .= str_repeat('-', 100).PHP_EOL;
233300

234301
for ($i = 0; $i < $maxCodeLines; $i++) {
235302
if (isset($firstLines[$i]) === true) {
@@ -244,16 +311,18 @@ protected function printCodeComparisonBlock(DOMNode $node)
244311
$secondLineText = '';
245312
}
246313

247-
echo '| ';
248-
echo $firstLineText.str_repeat(' ', max(0, (47 - strlen($firstLineText))));
249-
echo '| ';
250-
echo $secondLineText.str_repeat(' ', max(0, (48 - strlen($secondLineText))));
251-
echo '|'.PHP_EOL;
314+
$output .= '| ';
315+
$output .= $firstLineText.str_repeat(' ', max(0, (47 - strlen($firstLineText))));
316+
$output .= '| ';
317+
$output .= $secondLineText.str_repeat(' ', max(0, (48 - strlen($secondLineText))));
318+
$output .= '|'.PHP_EOL;
252319
}//end for
253320

254-
echo str_repeat('-', 100).PHP_EOL.PHP_EOL;
321+
$output .= str_repeat('-', 100).PHP_EOL.PHP_EOL;
255322

256-
}//end printCodeComparisonBlock()
323+
return $output;
324+
325+
}//end getFormattedCodeComparisonBlock()
257326

258327

259328
}//end class

tests/Core/Generators/Expectations/ExpectedOutputStructureDocs.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11

2-
---------------------------------------------
3-
| GENERATORTEST CODING STANDARD: NO CONTENT |
4-
---------------------------------------------
5-
6-
72
-------------------------------------------------------------------------------
83
| GENERATORTEST CODING STANDARD: CODE COMPARISON ONLY, MISSING STANDARD BLOCK |
94
-------------------------------------------------------------------------------

tests/Core/Generators/Expectations/ExpectedOutputUnsupportedElementAtWrongLevel.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/Core/Generators/Expectations/ExpectedOutputUnsupportedUnknownElement.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/Core/Generators/TextTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static function dataDocSpecifics()
175175
],
176176
'Unsupported: <code> element at the wrong level' => [
177177
'sniffs' => 'StandardWithDocs.Unsupported.ElementAtWrongLevel',
178-
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputUnsupportedElementAtWrongLevel.txt',
178+
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputEmpty.txt',
179179
],
180180
'Unsupported: one correct elm, one at wrong level' => [
181181
'sniffs' => 'StandardWithDocs.Unsupported.OneElmAtWrongLevel',
@@ -187,7 +187,7 @@ public static function dataDocSpecifics()
187187
],
188188
'Unsupported: unknown element' => [
189189
'sniffs' => 'StandardWithDocs.Unsupported.UnknownElement',
190-
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputUnsupportedUnknownElement.txt',
190+
'pathToExpected' => __DIR__.'/Expectations/ExpectedOutputEmpty.txt',
191191
],
192192
];
193193

0 commit comments

Comments
 (0)