@@ -108,95 +108,155 @@ public function generate()
108
108
}
109
109
110
110
ob_start ();
111
- $ this ->printHeader ();
112
- $ this ->printToc ();
113
-
114
111
foreach ($ this ->docFiles as $ file ) {
115
112
$ doc = new DOMDocument ();
116
113
$ doc ->load ($ file );
117
114
$ documentation = $ doc ->getElementsByTagName ('documentation ' )->item (0 );
118
115
$ this ->processSniff ($ documentation );
119
116
}
120
117
121
- $ this ->printFooter ();
122
-
123
118
$ content = ob_get_contents ();
124
119
ob_end_clean ();
125
120
126
- echo $ content ;
121
+ if (trim ($ content ) !== '' ) {
122
+ echo $ this ->getFormattedHeader ();
123
+ echo $ this ->getFormattedToc ();
124
+ echo $ content ;
125
+ echo $ this ->getFormattedFooter ();
126
+ }
127
127
128
128
}//end generate()
129
129
130
130
131
131
/**
132
132
* Print the header of the HTML page.
133
133
*
134
+ * @deprecated 3.12.0 Use HTML::getFormattedHeader() instead.
135
+ *
136
+ * @codeCoverageIgnore
137
+ *
134
138
* @return void
135
139
*/
136
140
protected function printHeader ()
137
141
{
138
- $ standard = $ this ->ruleset ->name ;
139
- echo '<html> ' .PHP_EOL ;
140
- echo ' <head> ' .PHP_EOL ;
141
- echo " <title> $ standard Coding Standards</title> " .PHP_EOL ;
142
- echo ' ' .str_replace ("\n" , PHP_EOL , self ::STYLESHEET ).PHP_EOL ;
143
- echo ' </head> ' .PHP_EOL ;
144
- echo ' <body> ' .PHP_EOL ;
145
- echo " <h1> $ standard Coding Standards</h1> " .PHP_EOL ;
142
+ echo $ this ->getFormattedHeader ();
146
143
147
144
}//end printHeader()
148
145
149
146
147
+ /**
148
+ * Format the header of the HTML page.
149
+ *
150
+ * @since 3.12.0 Replaces the deprecated HTML::printHeader() method.
151
+ *
152
+ * @return string
153
+ */
154
+ protected function getFormattedHeader ()
155
+ {
156
+ $ standard = $ this ->ruleset ->name ;
157
+ $ output = '<html> ' .PHP_EOL ;
158
+ $ output .= ' <head> ' .PHP_EOL ;
159
+ $ output .= " <title> $ standard Coding Standards</title> " .PHP_EOL ;
160
+ $ output .= ' ' .str_replace ("\n" , PHP_EOL , self ::STYLESHEET ).PHP_EOL ;
161
+ $ output .= ' </head> ' .PHP_EOL ;
162
+ $ output .= ' <body> ' .PHP_EOL ;
163
+ $ output .= " <h1> $ standard Coding Standards</h1> " .PHP_EOL ;
164
+
165
+ return $ output ;
166
+
167
+ }//end getFormattedHeader()
168
+
169
+
150
170
/**
151
171
* Print the table of contents for the standard.
152
172
*
153
- * The TOC is just an unordered list of bookmarks to sniffs on the page.
173
+ * @deprecated 3.12.0 Use HTML::getFormattedToc() instead.
174
+ *
175
+ * @codeCoverageIgnore
154
176
*
155
177
* @return void
156
178
*/
157
179
protected function printToc ()
180
+ {
181
+ echo $ this ->getFormattedToc ();
182
+
183
+ }//end printToc()
184
+
185
+
186
+ /**
187
+ * Format the table of contents for the standard.
188
+ *
189
+ * The TOC is just an unordered list of bookmarks to sniffs on the page.
190
+ *
191
+ * @since 3.12.0 Replaces the deprecated HTML::printToc() method.
192
+ *
193
+ * @return string
194
+ */
195
+ protected function getFormattedToc ()
158
196
{
159
197
// Only show a TOC when there are two or more docs to display.
160
198
if (count ($ this ->docFiles ) < 2 ) {
161
- return ;
199
+ return '' ;
162
200
}
163
201
164
- echo ' <h2>Table of Contents</h2> ' .PHP_EOL ;
165
- echo ' <ul class="toc"> ' .PHP_EOL ;
202
+ $ output = ' <h2>Table of Contents</h2> ' .PHP_EOL ;
203
+ $ output .= ' <ul class="toc"> ' .PHP_EOL ;
166
204
167
205
foreach ($ this ->docFiles as $ file ) {
168
206
$ doc = new DOMDocument ();
169
207
$ doc ->load ($ file );
170
208
$ documentation = $ doc ->getElementsByTagName ('documentation ' )->item (0 );
171
209
$ title = $ this ->getTitle ($ documentation );
172
- echo ' <li><a href="# ' .str_replace (' ' , '- ' , $ title )."\" > $ title</a></li> " .PHP_EOL ;
210
+ $ output .= ' <li><a href="# ' .str_replace (' ' , '- ' , $ title ).' "> ' . $ title. ' </a></li> ' .PHP_EOL ;
173
211
}
174
212
175
- echo ' </ul> ' .PHP_EOL ;
213
+ $ output .= ' </ul> ' .PHP_EOL ;
176
214
177
- }//end printToc()
215
+ return $ output ;
216
+
217
+ }//end getFormattedToc()
178
218
179
219
180
220
/**
181
221
* Print the footer of the HTML page.
182
222
*
223
+ * @deprecated 3.12.0 Use HTML::getFormattedFooter() instead.
224
+ *
225
+ * @codeCoverageIgnore
226
+ *
183
227
* @return void
184
228
*/
185
229
protected function printFooter ()
230
+ {
231
+ echo $ this ->getFormattedFooter ();
232
+
233
+ }//end printFooter()
234
+
235
+
236
+ /**
237
+ * Format the footer of the HTML page.
238
+ *
239
+ * @since 3.12.0 Replaces the deprecated HTML::printFooter() method.
240
+ *
241
+ * @return string
242
+ */
243
+ protected function getFormattedFooter ()
186
244
{
187
245
// Turn off errors so we don't get timezone warnings if people
188
246
// don't have their timezone set.
189
247
$ errorLevel = error_reporting (0 );
190
- echo ' <div class="tag-line"> ' ;
191
- echo 'Documentation generated on ' .date ('r ' );
192
- echo ' by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer ' .Config::VERSION .'</a> ' ;
193
- echo '</div> ' .PHP_EOL ;
248
+ $ output = ' <div class="tag-line"> ' ;
249
+ $ output .= 'Documentation generated on ' .date ('r ' );
250
+ $ output .= ' by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer ' .Config::VERSION .'</a> ' ;
251
+ $ output .= '</div> ' .PHP_EOL ;
194
252
error_reporting ($ errorLevel );
195
253
196
- echo ' </body> ' .PHP_EOL ;
197
- echo '</html> ' .PHP_EOL ;
254
+ $ output .= ' </body> ' .PHP_EOL ;
255
+ $ output .= '</html> ' .PHP_EOL ;
198
256
199
- }//end printFooter()
257
+ return $ output ;
258
+
259
+ }//end getFormattedFooter()
200
260
201
261
202
262
/**
@@ -210,18 +270,22 @@ protected function printFooter()
210
270
*/
211
271
public function processSniff (DOMNode $ doc )
212
272
{
213
- $ title = $ this ->getTitle ($ doc );
214
- echo ' <a name=" ' .str_replace (' ' , '- ' , $ title ).'" /> ' .PHP_EOL ;
215
- echo " <h2> $ title</h2> " .PHP_EOL ;
216
-
273
+ $ content = '' ;
217
274
foreach ($ doc ->childNodes as $ node ) {
218
275
if ($ node ->nodeName === 'standard ' ) {
219
- $ this ->printTextBlock ($ node );
276
+ $ content .= $ this ->getFormattedTextBlock ($ node );
220
277
} else if ($ node ->nodeName === 'code_comparison ' ) {
221
- $ this ->printCodeComparisonBlock ($ node );
278
+ $ content .= $ this ->getFormattedCodeComparisonBlock ($ node );
222
279
}
223
280
}
224
281
282
+ if (trim ($ content ) !== '' ) {
283
+ $ title = $ this ->getTitle ($ doc );
284
+ echo ' <a name=" ' .str_replace (' ' , '- ' , $ title ).'" /> ' .PHP_EOL ;
285
+ echo ' <h2> ' .$ title .'</h2> ' .PHP_EOL ;
286
+ echo $ content ;
287
+ }
288
+
225
289
}//end processSniff()
226
290
227
291
@@ -230,9 +294,29 @@ public function processSniff(DOMNode $doc)
230
294
*
231
295
* @param \DOMNode $node The DOMNode object for the text block.
232
296
*
297
+ * @deprecated 3.12.0 Use HTML::getFormattedTextBlock() instead.
298
+ *
299
+ * @codeCoverageIgnore
300
+ *
233
301
* @return void
234
302
*/
235
303
protected function printTextBlock (DOMNode $ node )
304
+ {
305
+ echo $ this ->getFormattedTextBlock ($ node );
306
+
307
+ }//end printTextBlock()
308
+
309
+
310
+ /**
311
+ * Format a text block found in a standard.
312
+ *
313
+ * @param \DOMNode $node The DOMNode object for the text block.
314
+ *
315
+ * @since 3.12.0 Replaces the deprecated HTML::printTextBlock() method.
316
+ *
317
+ * @return string
318
+ */
319
+ protected function getFormattedTextBlock (DOMNode $ node )
236
320
{
237
321
$ content = trim ($ node ->nodeValue );
238
322
$ content = htmlspecialchars ($ content , (ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ));
@@ -265,19 +349,39 @@ protected function printTextBlock(DOMNode $node)
265
349
}
266
350
}
267
351
268
- echo ' <p class="text"> ' .implode ('' , $ lines ).'</p> ' .PHP_EOL ;
352
+ return ' <p class="text"> ' .implode ('' , $ lines ).'</p> ' .PHP_EOL ;
269
353
270
- }//end printTextBlock ()
354
+ }//end getFormattedTextBlock ()
271
355
272
356
273
357
/**
274
358
* Print a code comparison block found in a standard.
275
359
*
276
360
* @param \DOMNode $node The DOMNode object for the code comparison block.
277
361
*
362
+ * @deprecated 3.12.0 Use HTML::getFormattedCodeComparisonBlock() instead.
363
+ *
364
+ * @codeCoverageIgnore
365
+ *
278
366
* @return void
279
367
*/
280
368
protected function printCodeComparisonBlock (DOMNode $ node )
369
+ {
370
+ echo $ this ->getFormattedCodeComparisonBlock ($ node );
371
+
372
+ }//end printCodeComparisonBlock()
373
+
374
+
375
+ /**
376
+ * Format a code comparison block found in a standard.
377
+ *
378
+ * @param \DOMNode $node The DOMNode object for the code comparison block.
379
+ *
380
+ * @since 3.12.0 Replaces the deprecated HTML::printCodeComparisonBlock() method.
381
+ *
382
+ * @return string
383
+ */
384
+ protected function getFormattedCodeComparisonBlock (DOMNode $ node )
281
385
{
282
386
$ codeBlocks = $ node ->getElementsByTagName ('code ' );
283
387
@@ -299,18 +403,20 @@ protected function printCodeComparisonBlock(DOMNode $node)
299
403
$ second = str_replace ('<em> ' , '<span class="code-comparison-highlight"> ' , $ second );
300
404
$ second = str_replace ('</em> ' , '</span> ' , $ second );
301
405
302
- echo ' <table class="code-comparison"> ' .PHP_EOL ;
303
- echo ' <tr> ' .PHP_EOL ;
304
- echo " <td class= \"code-comparison-title \"> $ firstTitle</td> " .PHP_EOL ;
305
- echo " <td class= \"code-comparison-title \"> $ secondTitle</td> " .PHP_EOL ;
306
- echo ' </tr> ' .PHP_EOL ;
307
- echo ' <tr> ' .PHP_EOL ;
308
- echo " <td class= \"code-comparison-code \"> $ first</td> " .PHP_EOL ;
309
- echo " <td class= \"code-comparison-code \"> $ second</td> " .PHP_EOL ;
310
- echo ' </tr> ' .PHP_EOL ;
311
- echo ' </table> ' .PHP_EOL ;
406
+ $ output = ' <table class="code-comparison"> ' .PHP_EOL ;
407
+ $ output .= ' <tr> ' .PHP_EOL ;
408
+ $ output .= " <td class= \"code-comparison-title \"> $ firstTitle</td> " .PHP_EOL ;
409
+ $ output .= " <td class= \"code-comparison-title \"> $ secondTitle</td> " .PHP_EOL ;
410
+ $ output .= ' </tr> ' .PHP_EOL ;
411
+ $ output .= ' <tr> ' .PHP_EOL ;
412
+ $ output .= " <td class= \"code-comparison-code \"> $ first</td> " .PHP_EOL ;
413
+ $ output .= " <td class= \"code-comparison-code \"> $ second</td> " .PHP_EOL ;
414
+ $ output .= ' </tr> ' .PHP_EOL ;
415
+ $ output .= ' </table> ' .PHP_EOL ;
312
416
313
- }//end printCodeComparisonBlock()
417
+ return $ output ;
418
+
419
+ }//end getFormattedCodeComparisonBlock()
314
420
315
421
316
422
}//end class
0 commit comments