|
16 | 16 | namespace PHP_CodeSniffer\Generators;
|
17 | 17 |
|
18 | 18 | use DOMDocument;
|
| 19 | +use DOMElement; |
19 | 20 | use DOMNode;
|
20 | 21 | use PHP_CodeSniffer\Config;
|
21 | 22 |
|
@@ -400,23 +401,11 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
|
400 | 401 | return '';
|
401 | 402 | }
|
402 | 403 |
|
403 |
| - $firstTitle = trim($firstCodeElm->getAttribute('title')); |
404 |
| - $firstTitle = str_replace(' ', ' ', $firstTitle); |
405 |
| - $first = trim($firstCodeElm->nodeValue); |
406 |
| - $first = str_replace('<?php', '<?php', $first); |
407 |
| - $first = str_replace("\n", '</br>', $first); |
408 |
| - $first = str_replace(' ', ' ', $first); |
409 |
| - $first = str_replace('<em>', '<span class="code-comparison-highlight">', $first); |
410 |
| - $first = str_replace('</em>', '</span>', $first); |
411 |
| - |
412 |
| - $secondTitle = trim($secondCodeElm->getAttribute('title')); |
413 |
| - $secondTitle = str_replace(' ', ' ', $secondTitle); |
414 |
| - $second = trim($secondCodeElm->nodeValue); |
415 |
| - $second = str_replace('<?php', '<?php', $second); |
416 |
| - $second = str_replace("\n", '</br>', $second); |
417 |
| - $second = str_replace(' ', ' ', $second); |
418 |
| - $second = str_replace('<em>', '<span class="code-comparison-highlight">', $second); |
419 |
| - $second = str_replace('</em>', '</span>', $second); |
| 404 | + $firstTitle = $this->formatCodeTitle($firstCodeElm); |
| 405 | + $first = $this->formatCodeSample($firstCodeElm); |
| 406 | + |
| 407 | + $secondTitle = $this->formatCodeTitle($secondCodeElm); |
| 408 | + $second = $this->formatCodeSample($secondCodeElm); |
420 | 409 |
|
421 | 410 | $titleRow = '';
|
422 | 411 | if ($firstTitle !== '' || $secondTitle !== '') {
|
@@ -447,4 +436,43 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
|
447 | 436 | }//end getFormattedCodeComparisonBlock()
|
448 | 437 |
|
449 | 438 |
|
| 439 | + /** |
| 440 | + * Retrieve a code block title and prepare it for output as HTML. |
| 441 | + * |
| 442 | + * @param \DOMElement $codeElm The DOMElement object for a code block. |
| 443 | + * |
| 444 | + * @since 3.12.0 |
| 445 | + * |
| 446 | + * @return string |
| 447 | + */ |
| 448 | + private function formatCodeTitle(DOMElement $codeElm) |
| 449 | + { |
| 450 | + $title = trim($codeElm->getAttribute('title')); |
| 451 | + return str_replace(' ', ' ', $title); |
| 452 | + |
| 453 | + }//end formatCodeTitle() |
| 454 | + |
| 455 | + |
| 456 | + /** |
| 457 | + * Retrieve a code block contents and prepare it for output as HTML. |
| 458 | + * |
| 459 | + * @param \DOMElement $codeElm The DOMElement object for a code block. |
| 460 | + * |
| 461 | + * @since 3.12.0 |
| 462 | + * |
| 463 | + * @return string |
| 464 | + */ |
| 465 | + private function formatCodeSample(DOMElement $codeElm) |
| 466 | + { |
| 467 | + $code = (string) $codeElm->nodeValue; |
| 468 | + $code = trim($code); |
| 469 | + $code = str_replace('<?php', '<?php', $code); |
| 470 | + $code = str_replace(["\n", ' '], ['</br>', ' '], $code); |
| 471 | + $code = str_replace(['<em>', '</em>'], ['<span class="code-comparison-highlight">', '</span>'], $code); |
| 472 | + |
| 473 | + return $code; |
| 474 | + |
| 475 | + }//end formatCodeSample() |
| 476 | + |
| 477 | + |
450 | 478 | }//end class
|
0 commit comments