Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CTextFilter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CTextFilter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class CTextFilter |
||
| 10 | { |
||
| 11 | use TTextUtilities; |
||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | /** |
||
| 16 | * Supported filters. |
||
| 17 | */ |
||
| 18 | private $filters = [ |
||
| 19 | "jsonfrontmatter", |
||
| 20 | "yamlfrontmatter", |
||
| 21 | "bbcode", |
||
| 22 | "clickable", |
||
| 23 | "shortcode", |
||
| 24 | "markdown", |
||
| 25 | "geshi", |
||
| 26 | "nl2br", |
||
| 27 | "purify", |
||
| 28 | "titlefromh1", |
||
| 29 | "anchor4Header", |
||
| 30 | ]; |
||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * Current document parsed. |
||
| 36 | */ |
||
| 37 | private $current; |
||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Hold meta information for filters to use. |
||
| 43 | */ |
||
| 44 | private $meta = []; |
||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Call each filter. |
||
| 50 | * |
||
| 51 | * @deprecated deprecated since version 1.2 in favour of parse(). |
||
| 52 | * |
||
| 53 | * @param string $text the text to filter. |
||
| 54 | * @param string|array $filters as comma separated list of filter, |
||
| 55 | * or filters sent in as array. |
||
| 56 | * |
||
| 57 | * @return string the formatted text. |
||
| 58 | */ |
||
| 59 | public function doFilter($text, $filters) |
||
| 90 | |||
| 91 | 1 | ||
| 92 | |||
| 93 | 1 | /** |
|
| 94 | * Set meta information that some filters can use. |
||
| 95 | * |
||
| 96 | * @param array $meta values for filters to use. |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public function setMeta($meta) |
||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | 2 | /** |
|
| 108 | * Return an array of all filters supported. |
||
| 109 | 2 | * |
|
| 110 | * @return array with strings of filters supported. |
||
| 111 | */ |
||
| 112 | public function getFilters() |
||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Check if filter is supported. |
||
| 121 | * |
||
| 122 | 3 | * @param string $filter to use. |
|
| 123 | * |
||
| 124 | 3 | * @throws mos/TextFilter/Exception when filter does not exists. |
|
| 125 | 2 | * |
|
| 126 | * @return boolean true if filter exists, false othwerwise. |
||
| 127 | */ |
||
| 128 | 2 | public function hasFilter($filter) |
|
| 132 | 2 | ||
| 133 | 2 | ||
| 134 | |||
| 135 | /** |
||
| 136 | * Add array items to frontmatter. |
||
| 137 | * |
||
| 138 | * @param array|null $matter key value array with items to add |
||
| 139 | * or null if empty. |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | private function addToFrontmatter($matter) |
||
| 156 | 6 | ||
| 157 | 6 | ||
| 158 | 6 | ||
| 159 | 6 | /** |
|
| 160 | * Call a specific filter and store its details. |
||
| 161 | * |
||
| 162 | 6 | * @param string $filter to use. |
|
| 163 | * |
||
| 164 | 6 | * @throws mos/TextFilter/Exception when filter does not exists. |
|
| 165 | 3 | * |
|
| 166 | 3 | * @return string the formatted text. |
|
| 167 | 3 | */ |
|
| 168 | 3 | private function parseFactory($filter) |
|
| 223 | 8 | ||
| 224 | |||
| 225 | 8 | ||
| 226 | 8 | /** |
|
| 227 | 8 | * Call each filter and return array with details of the formatted content. |
|
| 228 | 8 | * |
|
| 229 | * @param string $text the text to filter. |
||
| 230 | 8 | * @param array $filter array of filters to use. |
|
| 231 | * |
||
| 232 | * @throws mos/TextFilter/Exception when filterd does not exists. |
||
| 233 | * |
||
| 234 | * @return array with the formatted text and additional details. |
||
| 235 | */ |
||
| 236 | public function parse($text, $filter) |
||
| 250 | 3 | ||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Add excerpt as short version of text if available. |
||
| 255 | * |
||
| 256 | 3 | * @param object &$current same structure as returned by parse(). |
|
| 257 | 3 | * |
|
| 258 | 3 | * @return void. |
|
|
|
|||
| 259 | */ |
||
| 260 | 3 | public function addExcerpt($current) |
|
| 266 | 2 | ||
| 267 | 3 | ||
| 268 | |||
| 269 | 3 | /** |
|
| 270 | * Extract front matter from text. |
||
| 271 | * |
||
| 272 | * @param string $text the text to be parsed. |
||
| 273 | * @param string $startToken the start token. |
||
| 274 | * @param string $stopToken the stop token. |
||
| 275 | * |
||
| 276 | * @return array with the formatted text and the front matter. |
||
| 277 | */ |
||
| 278 | private function extractFrontMatter($text, $startToken, $stopToken) |
||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Extract JSON front matter from text. |
||
| 310 | * |
||
| 311 | * @param string $text the text to be parsed. |
||
| 312 | * |
||
| 313 | * @return array with the formatted text and the front matter. |
||
| 314 | */ |
||
| 315 | View Code Duplication | public function jsonFrontMatter($text) |
|
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | 1 | * Extract YAML front matter from text. |
|
| 337 | * |
||
| 338 | 1 | * @param string $text the text to be parsed. |
|
| 339 | 1 | * |
|
| 340 | * @return array with the formatted text and the front matter. |
||
| 341 | 1 | */ |
|
| 342 | 1 | View Code Duplication | public function yamlFrontMatter($text) |
| 359 | 3 | ||
| 360 | |||
| 361 | |||
| 362 | 3 | /** |
|
| 363 | 3 | * Get the title from the first H1. |
|
| 364 | 3 | * |
|
| 365 | 3 | * @param string $text the text to be parsed. |
|
| 366 | 3 | * |
|
| 367 | * @return string|null with the title, if its found. |
||
| 368 | 3 | */ |
|
| 369 | public function getTitleFromFirstH1($text) |
||
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Helper, BBCode formatting converting to HTML. |
||
| 385 | * |
||
| 386 | * @param string $text The text to be converted. |
||
| 387 | * |
||
| 388 | * @return string the formatted text. |
||
| 389 | * |
||
| 390 | * @link http://dbwebb.se/coachen/reguljara-uttryck-i-php-ger-bbcode-formattering |
||
| 391 | */ |
||
| 392 | public function bbcode2html($text) |
||
| 414 | 2 | ||
| 415 | |||
| 416 | 2 | ||
| 417 | 2 | /** |
|
| 418 | 2 | * Make clickable links from URLs in text. |
|
| 419 | 2 | * |
|
| 420 | 2 | * @param string $text the text that should be formatted. |
|
| 421 | * |
||
| 422 | * @return string with formatted anchors. |
||
| 423 | * |
||
| 424 | * @link http://dbwebb.se/coachen/lat-php-funktion-make-clickable-automatiskt-skapa-klickbara-lankar |
||
| 425 | 2 | */ |
|
| 426 | public function makeClickable($text) |
||
| 436 | |||
| 437 | 1 | ||
| 438 | |||
| 439 | 1 | /** |
|
| 440 | 1 | * Syntax highlighter using GeSHi http://qbnz.com/highlighter/. |
|
| 441 | * |
||
| 442 | * @param string $text text to be converted. |
||
| 443 | 1 | * @param string $language which language to use for highlighting syntax. |
|
| 444 | * |
||
| 445 | 1 | * @return string the formatted text. |
|
| 446 | */ |
||
| 447 | public function syntaxHighlightGeSHi($text, $language = "text") |
||
| 464 | |||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * Format text according to HTML Purifier. |
||
| 469 | * |
||
| 470 | * @param string $text that should be formatted. |
||
| 471 | 1 | * |
|
| 472 | * @return string as the formatted html-text. |
||
| 473 | 1 | */ |
|
| 474 | public function purify($text) |
||
| 484 | |||
| 485 | 2 | ||
| 486 | |||
| 487 | /** |
||
| 488 | * Format text according to Markdown syntax. |
||
| 489 | * |
||
| 490 | * @param string $text the text that should be formatted. |
||
| 491 | * |
||
| 492 | * @return string as the formatted html-text. |
||
| 493 | */ |
||
| 494 | public function markdown($text) |
||
| 498 | |||
| 499 | |||
| 500 | |||
| 501 | 2 | /** |
|
| 502 | 2 | * For convenience access to nl2br |
|
| 503 | 2 | * |
|
| 504 | * @param string $text text to be converted. |
||
| 505 | 2 | * |
|
| 506 | 2 | * @return string the formatted text. |
|
| 507 | 2 | */ |
|
| 508 | 2 | public function nl2br($text) |
|
| 512 | |||
| 513 | |||
| 514 | 1 | ||
| 515 | 1 | /** |
|
| 516 | * Shortcode to to quicker format text as HTML. |
||
| 517 | * |
||
| 518 | * @param string $text text to be converted. |
||
| 519 | * |
||
| 520 | * @return string the formatted text. |
||
| 521 | 2 | */ |
|
| 522 | public function shortCode($text) |
||
| 567 | |||
| 568 | 1 | ||
| 569 | |||
| 570 | 1 | /** |
|
| 571 | 1 | * Init shortcode handling by preparing the option list to an array, for those using arguments. |
|
| 572 | 1 | * |
|
| 573 | 1 | * @param string $options for the shortcode. |
|
| 574 | 1 | * |
|
| 575 | 1 | * @return array with all the options. |
|
| 576 | 1 | */ |
|
| 577 | 1 | public static function shortCodeInit($options) |
|
| 595 | |||
| 596 | 1 | ||
| 597 | 1 | ||
| 598 | 1 | /** |
|
| 599 | 1 | * Shortcode for <figure>. |
|
| 600 | 1 | * |
|
| 601 | 1 | * Usage example: [FIGURE src="img/home/me.jpg" caption="Me" alt="Bild på mig" nolink="nolink"] |
|
| 602 | * |
||
| 603 | * @param string $options for the shortcode. |
||
| 604 | 1 | * |
|
| 605 | 1 | * @return array with all the options. |
|
| 606 | 1 | */ |
|
| 607 | 1 | public static function shortCodeFigure($options) |
|
| 654 | |||
| 655 | |||
| 656 | |||
| 657 | /** |
||
| 658 | * Shortcode for [asciinema]. |
||
| 659 | * |
||
| 660 | * @param string $code the code to process. |
||
| 661 | * @param string $options for the shortcode. |
||
| 662 | * @return array with all the options. |
||
| 663 | */ |
||
| 664 | public static function ShortCodeAsciinema($options) { |
||
| 691 | |||
| 692 | |||
| 693 | |||
| 694 | /** |
||
| 695 | * Shortcode for including a SVG-image inside a <figure>. |
||
| 696 | * |
||
| 697 | * @param string $code the code to process. |
||
| 698 | * @param string $options for the shortcode. |
||
| 699 | * @return array with all the options. |
||
| 700 | */ |
||
| 701 | /*public static function ShortCodeSVGFigure($options) { |
||
| 702 | extract(array_merge(array( |
||
| 703 | 'id' => null, |
||
| 704 | 'class' => null, |
||
| 705 | 'src' => null, |
||
| 706 | 'path' => null, |
||
| 707 | 'title' => null, |
||
| 708 | 'alt' => null, |
||
| 709 | 'caption' => null, |
||
| 710 | 'href' => null, |
||
| 711 | 'nolink' => false, |
||
| 712 | ), CTextFilter::ShortCodeInit($options)), EXTR_SKIP); |
||
| 713 | |||
| 714 | $id = $id ? " id='$id'" : null; |
||
| 715 | //$class = $class ? " class='$class'" : null; |
||
| 716 | $class = $class ? " class='figure $class'" : " class='figure'"; |
||
| 717 | $title = $title ? " title='$title'" : null; |
||
| 718 | |||
| 719 | if(!$alt && $caption) { |
||
| 720 | $alt = $caption; |
||
| 721 | } |
||
| 722 | |||
| 723 | if(!$href) { |
||
| 724 | $pos = strpos($src, '?'); |
||
| 725 | $href = $pos ? substr($src, 0, $pos) : $src; |
||
| 726 | } |
||
| 727 | |||
| 728 | if(!$nolink) { |
||
| 729 | $a_start = "<a href='{$href}'>"; |
||
| 730 | $a_end = "</a>"; |
||
| 731 | } |
||
| 732 | |||
| 733 | // Import the file containing the svg-image |
||
| 734 | $svg = null; |
||
| 735 | |||
| 736 | if($path[0] != '/') { |
||
| 737 | $path = self::$dir . '/' . $path; |
||
| 738 | } |
||
| 739 | |||
| 740 | if(is_file($path)) { |
||
| 741 | $svg = file_get_contents($path); |
||
| 742 | } |
||
| 743 | else { |
||
| 744 | $svg = "No such file: $path"; |
||
| 745 | } |
||
| 746 | $html = <<<EOD |
||
| 747 | <figure{$id}{$class}> |
||
| 748 | {$svg} |
||
| 749 | <figcaption markdown=1>{$caption}</figcaption> |
||
| 750 | </figure> |
||
| 751 | EOD; |
||
| 752 | |||
| 753 | return $html; |
||
| 754 | } |
||
| 755 | |||
| 756 | */ |
||
| 757 | |||
| 758 | |||
| 759 | |||
| 760 | /** |
||
| 761 | * Shorttags to to quicker format text as HTML. |
||
| 762 | * |
||
| 763 | * @param string text text to be converted. |
||
| 764 | * @return string the formatted text. |
||
| 765 | */ |
||
| 766 | /*public static function ShortTags($text) { |
||
| 767 | $callback = function($matches) { |
||
| 768 | switch($matches[1]) { |
||
| 769 | case 'IMG': |
||
| 770 | $caption = t('Image: '); |
||
| 771 | $pos = strpos($matches[2], '?'); |
||
| 772 | $href = $pos ? substr($matches[2], 0, $pos) : $matches[2]; |
||
| 773 | $src = htmlspecialchars($matches[2]); |
||
| 774 | return <<<EOD |
||
| 775 | <figure> |
||
| 776 | <a href='{$href}'><img src='{$src}' alt='{$matches[3]}' /></a> |
||
| 777 | <figcaption markdown=1>{$caption}{$matches[3]}</figcaption> |
||
| 778 | </figure> |
||
| 779 | EOD; |
||
| 780 | |||
| 781 | case 'IMG2': |
||
| 782 | $caption = null; //t('Image: '); |
||
| 783 | $pos = strpos($matches[2], '?'); |
||
| 784 | $href = $pos ? substr($matches[2], 0, $pos) : $matches[2]; |
||
| 785 | $src = htmlspecialchars($matches[2]); |
||
| 786 | return <<<EOD |
||
| 787 | <figure class="{$matches[4]}"> |
||
| 788 | <a href='{$href}'><img src='{$src}' alt='{$matches[3]}' /></a> |
||
| 789 | <figcaption markdown=1>{$caption}{$matches[3]}</figcaption> |
||
| 790 | </figure> |
||
| 791 | EOD; |
||
| 792 | case 'BOOK': |
||
| 793 | $isbn = $matches[2]; |
||
| 794 | $stores = array( |
||
| 795 | 'BTH' => "http://bth.summon.serialssolutions.com/?#!/search?ho=t&q={$isbn}", |
||
| 796 | 'Libris' => "http://libris.kb.se/hitlist?q={$isbn}", |
||
| 797 | 'Google Books' => "http://books.google.com/books?q={$isbn}", |
||
| 798 | 'Bokus' => "http://www.bokus.com/bok/{$isbn}", |
||
| 799 | 'Adlibris' => "http://www.adlibris.com/se/product.aspx?isbn={$isbn}", |
||
| 800 | 'Amazon' => "http://www.amazon.com/s/ref=nb_ss?url=field-keywords={$isbn}", |
||
| 801 | 'Barnes&Noble' => "http://search.barnesandnoble.com/booksearch/ISBNInquiry.asp?r=1&IF=N&EAN={$isbn}", |
||
| 802 | ); |
||
| 803 | $html = null; |
||
| 804 | foreach($stores as $key => $val) { |
||
| 805 | $html .= "<a href='$val'>$key</a> • "; |
||
| 806 | } |
||
| 807 | return substr($html, 0, -8); |
||
| 808 | break; |
||
| 809 | |||
| 810 | case 'YOUTUBE': |
||
| 811 | $caption = t('Figure: '); |
||
| 812 | $height = ceil($matches[3] / (16/9)); |
||
| 813 | return <<<EOD |
||
| 814 | <figure> |
||
| 815 | <iframe width='{$matches[3]}' height='{$height}' src="http://www.youtube.com/embed/{$matches[2]}" frameborder="0" |
||
| 816 | allowfullscreen></iframe> |
||
| 817 | <figcaption>{$caption}{$matches[4]}</figcaption> |
||
| 818 | </figure> |
||
| 819 | EOD; |
||
| 820 | break; |
||
| 821 | |||
| 822 | case 'syntax=': return CTextFilter::SyntaxHighlightGeSHi($matches[3], $matches[2]); break; |
||
| 823 | case '```': return CTextFilter::SyntaxHighlightGeSHi($matches[3], $matches[2]); break; |
||
| 824 | //case 'syntax=': return "<pre>" . highlight_string($matches[3], true) . "</pre>"; break; |
||
| 825 | //case 'INCL': include($matches[2]); break; |
||
| 826 | case 'INFO': return "<div class='info' markdown=1>"; break; |
||
| 827 | case '/INFO': return "</div>"; break; |
||
| 828 | case 'BASEURL': return CLydia::Instance()->request->base_url; break; |
||
| 829 | case 'FIGURE': return CTextFilter::ShortCodeFigure($matches[2]); break; |
||
| 830 | case 'FIGURE-SVG': return CTextFilter::ShortCodeSVGFigure($matches[2]); break; |
||
| 831 | case 'ASCIINEMA': return CTextFilter::ShortCodeAsciinema($matches[2]); break; |
||
| 832 | default: return "{$matches[1]} IS UNKNOWN SHORTTAG."; break; |
||
| 833 | } |
||
| 834 | }; |
||
| 835 | $patterns = array( |
||
| 836 | '#\[(BASEURL)\]#', |
||
| 837 | //'/\[(AUTHOR) name=(.+) email=(.+) url=(.+)\]/', |
||
| 838 | '/\[(FIGURE)[\s+](.+)\]/', |
||
| 839 | '/\[(FIGURE-SVG)[\s+](.+)\]/', |
||
| 840 | '/\[(ASCIINEMA)[\s+](.+)\]/', |
||
| 841 | '/\[(IMG) src=(.+) alt=(.+)\]/', |
||
| 842 | '/\[(IMG2) src=(.+) alt="(.+)" class="(.+)"\]/', |
||
| 843 | '/\[(BOOK) isbn=(.+)\]/', |
||
| 844 | '/\[(YOUTUBE) src=(.+) width=(.+) caption=(.+)\]/', |
||
| 845 | '/~~~(syntax=)(php|html|html5|css|sql|javascript|bash)\n([^~]+)\n~~~/s', |
||
| 846 | '/(```)(php|html|html5|css|sql|javascript|bash|text|txt|python)\n([^`]+)\n```/s', |
||
| 847 | //'/\[(INCL)/s*([^\]+)/', |
||
| 848 | '#\[(INFO)\]#', '#\[(/INFO)\]#', |
||
| 849 | ); |
||
| 850 | |||
| 851 | $ret = preg_replace_callback($patterns, $callback, $text); |
||
| 852 | return $ret; |
||
| 853 | } |
||
| 854 | */ |
||
| 855 | |||
| 856 | |||
| 857 | |||
| 858 | /** |
||
| 859 | * Support SmartyPants for better typography. |
||
| 860 | * |
||
| 861 | * @param string text text to be converted. |
||
| 862 | * @return string the formatted text. |
||
| 863 | */ |
||
| 864 | /* public static function SmartyPants($text) { |
||
| 865 | require_once(__DIR__.'/php_smartypants_1.5.1e/smartypants.php'); |
||
| 866 | return SmartyPants($text); |
||
| 867 | } |
||
| 868 | */ |
||
| 869 | |||
| 870 | |||
| 871 | /** |
||
| 872 | * Support enhanced SmartyPants/Typographer for better typography. |
||
| 873 | * |
||
| 874 | * @param string text text to be converted. |
||
| 875 | * @return string the formatted text. |
||
| 876 | */ |
||
| 877 | /* public static function Typographer($text) { |
||
| 878 | require_once(__DIR__.'/php_smartypants_typographer_1.0/smartypants.php'); |
||
| 879 | $ret = SmartyPants($text); |
||
| 880 | return $ret; |
||
| 881 | } |
||
| 882 | */ |
||
| 883 | } |
||
| 884 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.