@@ -32,53 +32,92 @@ public function generate()
32
32
}
33
33
34
34
ob_start ();
35
- $ this ->printHeader ();
36
-
37
35
foreach ($ this ->docFiles as $ file ) {
38
36
$ doc = new DOMDocument ();
39
37
$ doc ->load ($ file );
40
38
$ documentation = $ doc ->getElementsByTagName ('documentation ' )->item (0 );
41
39
$ this ->processSniff ($ documentation );
42
40
}
43
41
44
- $ this ->printFooter ();
45
42
$ content = ob_get_contents ();
46
43
ob_end_clean ();
47
44
48
- echo $ content ;
45
+ if (trim ($ content ) !== '' ) {
46
+ echo $ this ->getFormattedHeader ();
47
+ echo $ content ;
48
+ echo $ this ->getFormattedFooter ();
49
+ }
49
50
50
51
}//end generate()
51
52
52
53
53
54
/**
54
55
* Print the markdown header.
55
56
*
57
+ * @deprecated 3.12.0 Use Markdown::getFormattedHeader() instead.
58
+ *
59
+ * @codeCoverageIgnore
60
+ *
56
61
* @return void
57
62
*/
58
63
protected function printHeader ()
64
+ {
65
+ echo $ this ->getFormattedHeader ();
66
+
67
+ }//end printHeader()
68
+
69
+
70
+ /**
71
+ * Format the markdown header.
72
+ *
73
+ * @since 3.12.0 Replaces the deprecated Markdown::printHeader() method.
74
+ *
75
+ * @return string
76
+ */
77
+ protected function getFormattedHeader ()
59
78
{
60
79
$ standard = $ this ->ruleset ->name ;
61
80
62
- echo "# $ standard Coding Standard " .PHP_EOL ;
81
+ return "# $ standard Coding Standard " .PHP_EOL ;
63
82
64
- }//end printHeader ()
83
+ }//end getFormattedHeader ()
65
84
66
85
67
86
/**
68
87
* Print the markdown footer.
69
88
*
89
+ * @deprecated 3.12.0 Use Markdown::getFormattedFooter() instead.
90
+ *
91
+ * @codeCoverageIgnore
92
+ *
70
93
* @return void
71
94
*/
72
95
protected function printFooter ()
96
+ {
97
+ echo $ this ->getFormattedFooter ();
98
+
99
+ }//end printFooter()
100
+
101
+
102
+ /**
103
+ * Format the markdown footer.
104
+ *
105
+ * @since 3.12.0 Replaces the deprecated Markdown::printFooter() method.
106
+ *
107
+ * @return string
108
+ */
109
+ protected function getFormattedFooter ()
73
110
{
74
111
// Turn off errors so we don't get timezone warnings if people
75
112
// don't have their timezone set.
76
113
$ errorLevel = error_reporting (0 );
77
- echo PHP_EOL .'Documentation generated on ' .date ('r ' );
78
- echo ' by [PHP_CodeSniffer ' .Config::VERSION .'](https://github.com/PHPCSStandards/PHP_CodeSniffer) ' .PHP_EOL ;
114
+ $ output = PHP_EOL .'Documentation generated on ' .date ('r ' );
115
+ $ output .= ' by [PHP_CodeSniffer ' .Config::VERSION .'](https://github.com/PHPCSStandards/PHP_CodeSniffer) ' .PHP_EOL ;
79
116
error_reporting ($ errorLevel );
80
117
81
- }//end printFooter()
118
+ return $ output ;
119
+
120
+ }//end getFormattedFooter()
82
121
83
122
84
123
/**
@@ -92,17 +131,21 @@ protected function printFooter()
92
131
*/
93
132
protected function processSniff (DOMNode $ doc )
94
133
{
95
- $ title = $ this ->getTitle ($ doc );
96
- echo PHP_EOL ."## $ title " .PHP_EOL .PHP_EOL ;
97
-
134
+ $ content = '' ;
98
135
foreach ($ doc ->childNodes as $ node ) {
99
136
if ($ node ->nodeName === 'standard ' ) {
100
- $ this ->printTextBlock ($ node );
137
+ $ content .= $ this ->getFormattedTextBlock ($ node );
101
138
} else if ($ node ->nodeName === 'code_comparison ' ) {
102
- $ this ->printCodeComparisonBlock ($ node );
139
+ $ content .= $ this ->getFormattedCodeComparisonBlock ($ node );
103
140
}
104
141
}
105
142
143
+ if (trim ($ content ) !== '' ) {
144
+ $ title = $ this ->getTitle ($ doc );
145
+ echo PHP_EOL ."## $ title " .PHP_EOL .PHP_EOL ;
146
+ echo $ content ;
147
+ }
148
+
106
149
}//end processSniff()
107
150
108
151
@@ -111,9 +154,29 @@ protected function processSniff(DOMNode $doc)
111
154
*
112
155
* @param \DOMNode $node The DOMNode object for the text block.
113
156
*
157
+ * @deprecated 3.12.0 Use Markdown::getFormattedTextBlock() instead.
158
+ *
159
+ * @codeCoverageIgnore
160
+ *
114
161
* @return void
115
162
*/
116
163
protected function printTextBlock (DOMNode $ node )
164
+ {
165
+ echo $ this ->getFormattedTextBlock ($ node );
166
+
167
+ }//end printTextBlock()
168
+
169
+
170
+ /**
171
+ * Format a text block found in a standard.
172
+ *
173
+ * @param \DOMNode $node The DOMNode object for the text block.
174
+ *
175
+ * @since 3.12.0 Replaces the deprecated Markdown::printTextBlock() method.
176
+ *
177
+ * @return string
178
+ */
179
+ protected function getFormattedTextBlock (DOMNode $ node )
117
180
{
118
181
$ content = trim ($ node ->nodeValue );
119
182
$ content = htmlspecialchars ($ content , (ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ));
@@ -144,19 +207,39 @@ protected function printTextBlock(DOMNode $node)
144
207
}
145
208
}
146
209
147
- echo implode (PHP_EOL , $ lines ).PHP_EOL ;
210
+ return implode (PHP_EOL , $ lines ).PHP_EOL ;
148
211
149
- }//end printTextBlock ()
212
+ }//end getFormattedTextBlock ()
150
213
151
214
152
215
/**
153
216
* Print a code comparison block found in a standard.
154
217
*
155
218
* @param \DOMNode $node The DOMNode object for the code comparison block.
156
219
*
220
+ * @deprecated 3.12.0 Use Markdown::getFormattedCodeComparisonBlock() instead.
221
+ *
222
+ * @codeCoverageIgnore
223
+ *
157
224
* @return void
158
225
*/
159
226
protected function printCodeComparisonBlock (DOMNode $ node )
227
+ {
228
+ echo $ this ->getFormattedCodeComparisonBlock ($ node );
229
+
230
+ }//end printCodeComparisonBlock()
231
+
232
+
233
+ /**
234
+ * Format a code comparison block found in a standard.
235
+ *
236
+ * @param \DOMNode $node The DOMNode object for the code comparison block.
237
+ *
238
+ * @since 3.12.0 Replaces the deprecated Markdown::printCodeComparisonBlock() method.
239
+ *
240
+ * @return string
241
+ */
242
+ protected function getFormattedCodeComparisonBlock (DOMNode $ node )
160
243
{
161
244
$ codeBlocks = $ node ->getElementsByTagName ('code ' );
162
245
@@ -174,22 +257,24 @@ protected function printCodeComparisonBlock(DOMNode $node)
174
257
$ second = str_replace ('<em> ' , '' , $ second );
175
258
$ second = str_replace ('</em> ' , '' , $ second );
176
259
177
- echo ' <table> ' .PHP_EOL ;
178
- echo ' <tr> ' .PHP_EOL ;
179
- echo " <th> $ firstTitle</th> " .PHP_EOL ;
180
- echo " <th> $ secondTitle</th> " .PHP_EOL ;
181
- echo ' </tr> ' .PHP_EOL ;
182
- echo ' <tr> ' .PHP_EOL ;
183
- echo '<td> ' .PHP_EOL .PHP_EOL ;
184
- echo " $ first " .PHP_EOL .PHP_EOL ;
185
- echo '</td> ' .PHP_EOL ;
186
- echo '<td> ' .PHP_EOL .PHP_EOL ;
187
- echo " $ second " .PHP_EOL .PHP_EOL ;
188
- echo '</td> ' .PHP_EOL ;
189
- echo ' </tr> ' .PHP_EOL ;
190
- echo ' </table> ' .PHP_EOL ;
191
-
192
- }//end printCodeComparisonBlock()
260
+ $ output = ' <table> ' .PHP_EOL ;
261
+ $ output .= ' <tr> ' .PHP_EOL ;
262
+ $ output .= " <th> $ firstTitle</th> " .PHP_EOL ;
263
+ $ output .= " <th> $ secondTitle</th> " .PHP_EOL ;
264
+ $ output .= ' </tr> ' .PHP_EOL ;
265
+ $ output .= ' <tr> ' .PHP_EOL ;
266
+ $ output .= '<td> ' .PHP_EOL .PHP_EOL ;
267
+ $ output .= " $ first " .PHP_EOL .PHP_EOL ;
268
+ $ output .= '</td> ' .PHP_EOL ;
269
+ $ output .= '<td> ' .PHP_EOL .PHP_EOL ;
270
+ $ output .= " $ second " .PHP_EOL .PHP_EOL ;
271
+ $ output .= '</td> ' .PHP_EOL ;
272
+ $ output .= ' </tr> ' .PHP_EOL ;
273
+ $ output .= ' </table> ' .PHP_EOL ;
274
+
275
+ return $ output ;
276
+
277
+ }//end getFormattedCodeComparisonBlock()
193
278
194
279
195
280
}//end class
0 commit comments