|
11 | 11 |
|
12 | 12 | namespace PHP_CodeSniffer\Generators;
|
13 | 13 |
|
| 14 | +use DOMElement; |
14 | 15 | use DOMNode;
|
15 | 16 | use PHP_CodeSniffer\Config;
|
16 | 17 |
|
@@ -249,19 +250,11 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
|
249 | 250 | return '';
|
250 | 251 | }
|
251 | 252 |
|
252 |
| - $firstTitle = trim($firstCodeElm->getAttribute('title')); |
253 |
| - $firstTitle = str_replace(' ', ' ', $firstTitle); |
254 |
| - $first = trim($firstCodeElm->nodeValue); |
255 |
| - $first = str_replace("\n", PHP_EOL.' ', $first); |
256 |
| - $first = str_replace('<em>', '', $first); |
257 |
| - $first = str_replace('</em>', '', $first); |
| 253 | + $firstTitle = $this->formatCodeTitle($firstCodeElm); |
| 254 | + $first = $this->formatCodeSample($firstCodeElm); |
258 | 255 |
|
259 |
| - $secondTitle = trim($secondCodeElm->getAttribute('title')); |
260 |
| - $secondTitle = str_replace(' ', ' ', $secondTitle); |
261 |
| - $second = trim($secondCodeElm->nodeValue); |
262 |
| - $second = str_replace("\n", PHP_EOL.' ', $second); |
263 |
| - $second = str_replace('<em>', '', $second); |
264 |
| - $second = str_replace('</em>', '', $second); |
| 256 | + $secondTitle = $this->formatCodeTitle($secondCodeElm); |
| 257 | + $second = $this->formatCodeSample($secondCodeElm); |
265 | 258 |
|
266 | 259 | $titleRow = '';
|
267 | 260 | if ($firstTitle !== '' || $secondTitle !== '') {
|
@@ -296,4 +289,42 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
|
296 | 289 | }//end getFormattedCodeComparisonBlock()
|
297 | 290 |
|
298 | 291 |
|
| 292 | + /** |
| 293 | + * Retrieve a code block title and prepare it for output as HTML. |
| 294 | + * |
| 295 | + * @param \DOMElement $codeElm The DOMElement object for a code block. |
| 296 | + * |
| 297 | + * @since 3.12.0 |
| 298 | + * |
| 299 | + * @return string |
| 300 | + */ |
| 301 | + private function formatCodeTitle(DOMElement $codeElm) |
| 302 | + { |
| 303 | + $title = trim($codeElm->getAttribute('title')); |
| 304 | + return str_replace(' ', ' ', $title); |
| 305 | + |
| 306 | + }//end formatCodeTitle() |
| 307 | + |
| 308 | + |
| 309 | + /** |
| 310 | + * Retrieve a code block contents and prepare it for output as HTML. |
| 311 | + * |
| 312 | + * @param \DOMElement $codeElm The DOMElement object for a code block. |
| 313 | + * |
| 314 | + * @since 3.12.0 |
| 315 | + * |
| 316 | + * @return string |
| 317 | + */ |
| 318 | + private function formatCodeSample(DOMElement $codeElm) |
| 319 | + { |
| 320 | + $code = (string) $codeElm->nodeValue; |
| 321 | + $code = trim($code); |
| 322 | + $code = str_replace("\n", PHP_EOL.' ', $code); |
| 323 | + $code = str_replace(['<em>', '</em>'], '', $code); |
| 324 | + |
| 325 | + return $code; |
| 326 | + |
| 327 | + }//end formatCodeSample() |
| 328 | + |
| 329 | + |
299 | 330 | }//end class
|
0 commit comments