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 | * Call each filter. |
||
43 | * |
||
44 | * @deprecated deprecated since version 1.2 in favour of parse(). |
||
45 | * |
||
46 | * @param string $text the text to filter. |
||
47 | * @param string|array $filters as comma separated list of filter, |
||
48 | * or filters sent in as array. |
||
49 | * |
||
50 | * @return string the formatted text. |
||
51 | */ |
||
52 | public function doFilter($text, $filters) |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * Return an array of all filters supported. |
||
88 | * |
||
89 | * @return array with strings of filters supported. |
||
90 | */ |
||
91 | 1 | public function getFilters() |
|
95 | |||
96 | |||
97 | |||
98 | /** |
||
99 | * Check if filter is supported. |
||
100 | * |
||
101 | * @param string $filter to use. |
||
102 | * |
||
103 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
104 | * |
||
105 | * @return boolean true if filter exists, false othwerwise. |
||
106 | */ |
||
107 | 2 | public function hasFilter($filter) |
|
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * Add array items to frontmatter. |
||
116 | * |
||
117 | * @param array|null $matter key value array with items to add |
||
118 | * or null if empty. |
||
119 | * |
||
120 | * @return $this |
||
121 | */ |
||
122 | 3 | private function addToFrontmatter($matter) |
|
135 | |||
136 | |||
137 | |||
138 | /** |
||
139 | * Call a specific filter and store its details. |
||
140 | * |
||
141 | * @param string $filter to use. |
||
142 | * |
||
143 | * @throws mos/TextFilter/Exception when filter does not exists. |
||
144 | * |
||
145 | * @return string the formatted text. |
||
146 | */ |
||
147 | 6 | private function parseFactory($filter) |
|
202 | |||
203 | |||
204 | |||
205 | /** |
||
206 | * Call each filter and return array with details of the formatted content. |
||
207 | * |
||
208 | * @param string $text the text to filter. |
||
209 | * @param array $filter array of filters to use. |
||
210 | * |
||
211 | * @throws mos/TextFilter/Exception when filterd does not exists. |
||
212 | * |
||
213 | * @return array with the formatted text and additional details. |
||
214 | */ |
||
215 | 8 | public function parse($text, $filter) |
|
229 | |||
230 | 8 | ||
231 | |||
232 | /** |
||
233 | * Add excerpt as short version of text if available. |
||
234 | * |
||
235 | * @param object &$current same structure as returned by parse(). |
||
236 | * |
||
237 | * @return void. |
||
|
|||
238 | */ |
||
239 | public function addExcerpt($current) |
||
245 | |||
246 | 3 | ||
247 | |||
248 | 3 | /** |
|
249 | * Extract front matter from text. |
||
250 | 3 | * |
|
251 | * @param string $text the text to be parsed. |
||
252 | * @param string $startToken the start token. |
||
253 | * @param string $stopToken the stop token. |
||
254 | * |
||
255 | * @return array with the formatted text and the front matter. |
||
256 | 3 | */ |
|
257 | 3 | private function extractFrontMatter($text, $startToken, $stopToken) |
|
284 | |||
285 | 3 | ||
286 | 2 | ||
287 | /** |
||
288 | 2 | * Extract JSON front matter from text. |
|
289 | * |
||
290 | * @param string $text the text to be parsed. |
||
291 | 2 | * |
|
292 | * @return array with the formatted text and the front matter. |
||
293 | */ |
||
294 | 3 | public function jsonFrontMatter($text) |
|
311 | |||
312 | |||
313 | |||
314 | /** |
||
315 | * Extract YAML front matter from text. |
||
316 | * |
||
317 | * @param string $text the text to be parsed. |
||
318 | * |
||
319 | * @return array with the formatted text and the front matter. |
||
320 | */ |
||
321 | public function yamlFrontMatter($text) |
||
339 | 1 | ||
340 | |||
341 | 1 | ||
342 | 1 | /** |
|
343 | 1 | * Get the title from the first H1. |
|
344 | * |
||
345 | 1 | * @param string $text the text to be parsed. |
|
346 | * |
||
347 | * @return string|null with the title, if its found. |
||
348 | */ |
||
349 | public function getTitleFromFirstH1($text) |
||
360 | |||
361 | |||
362 | 3 | ||
363 | 3 | /** |
|
364 | 3 | * Helper, BBCode formatting converting to HTML. |
|
365 | 3 | * |
|
366 | 3 | * @param string $text The text to be converted. |
|
367 | * |
||
368 | 3 | * @return string the formatted text. |
|
369 | * |
||
370 | * @link http://dbwebb.se/coachen/reguljara-uttryck-i-php-ger-bbcode-formattering |
||
371 | 3 | */ |
|
372 | 3 | public function bbcode2html($text) |
|
394 | |||
395 | 1 | ||
396 | 1 | ||
397 | /** |
||
398 | 1 | * Make clickable links from URLs in text. |
|
399 | 1 | * |
|
400 | * @param string $text the text that should be formatted. |
||
401 | 1 | * |
|
402 | * @return string with formatted anchors. |
||
403 | * |
||
404 | * @link http://dbwebb.se/coachen/lat-php-funktion-make-clickable-automatiskt-skapa-klickbara-lankar |
||
405 | */ |
||
406 | public function makeClickable($text) |
||
416 | 2 | ||
417 | 2 | ||
418 | 2 | ||
419 | 2 | /** |
|
420 | 2 | * Syntax highlighter using GeSHi http://qbnz.com/highlighter/. |
|
421 | * |
||
422 | * @param string $text text to be converted. |
||
423 | * @param string $language which language to use for highlighting syntax. |
||
424 | * |
||
425 | 2 | * @return string the formatted text. |
|
426 | */ |
||
427 | public function syntaxHighlightGeSHi($text, $language = "text") |
||
444 | |||
445 | 1 | ||
446 | |||
447 | /** |
||
448 | * Format text according to HTML Purifier. |
||
449 | * |
||
450 | * @param string $text that should be formatted. |
||
451 | * |
||
452 | * @return string as the formatted html-text. |
||
453 | */ |
||
454 | public function purify($text) |
||
464 | |||
465 | |||
466 | |||
467 | /** |
||
468 | * Format text according to Markdown syntax. |
||
469 | * |
||
470 | * @param string $text the text that should be formatted. |
||
471 | 1 | * |
|
472 | * @return string as the formatted html-text. |
||
473 | 1 | */ |
|
474 | public function markdown($text) |
||
478 | |||
479 | |||
480 | |||
481 | /** |
||
482 | * For convenience access to nl2br |
||
483 | * |
||
484 | * @param string $text text to be converted. |
||
485 | 2 | * |
|
486 | * @return string the formatted text. |
||
487 | */ |
||
488 | public function nl2br($text) |
||
492 | |||
493 | |||
494 | |||
495 | /** |
||
496 | * Shortcode to to quicker format text as HTML. |
||
497 | * |
||
498 | * @param string $text text to be converted. |
||
499 | * |
||
500 | * @return string the formatted text. |
||
501 | 2 | */ |
|
502 | 2 | public function shortCode($text) |
|
542 | 1 | ||
543 | |||
544 | |||
545 | 1 | /** |
|
546 | 1 | * Init shortcode handling by preparing the option list to an array, for those using arguments. |
|
547 | 1 | * |
|
548 | * @param string $options for the shortcode. |
||
549 | 1 | * |
|
550 | * @return array with all the options. |
||
551 | 1 | */ |
|
552 | public static function shortCodeInit($options) |
||
570 | 1 | ||
571 | 1 | ||
572 | 1 | ||
573 | 1 | /** |
|
574 | 1 | * Shortcode for <figure>. |
|
575 | 1 | * |
|
576 | 1 | * Usage example: [FIGURE src="img/home/me.jpg" caption="Me" alt="Bild på mig" nolink="nolink"] |
|
577 | 1 | * |
|
578 | 1 | * @param string $options for the shortcode. |
|
579 | 1 | * |
|
580 | 1 | * @return array with all the options. |
|
581 | 1 | */ |
|
582 | public static function shortCodeFigure($options) |
||
629 | |||
630 | |||
631 | |||
632 | /** |
||
633 | * Shortcode for [asciinema]. |
||
634 | * |
||
635 | * @param string $code the code to process. |
||
636 | * @param string $options for the shortcode. |
||
637 | * @return array with all the options. |
||
638 | */ |
||
639 | /* public static function ShortCodeAsciinema($options) { |
||
640 | extract(array_merge(array( |
||
641 | 'id' => null, |
||
642 | 'class' => null, |
||
643 | 'src' => null, |
||
644 | 'title' => null, |
||
645 | 'alt' => null, |
||
646 | 'caption' => null, |
||
647 | ), CTextFilter::ShortCodeInit($options)), EXTR_SKIP); |
||
648 | |||
649 | $id = $id ? " id='$id'" : null; |
||
650 | $class = $class ? " class='figure $class'" : " class='figure'"; |
||
651 | $title = $title ? " title='$title'" : null; |
||
652 | |||
653 | if(!$alt && $caption) { |
||
654 | $alt = $caption; |
||
655 | } |
||
656 | |||
657 | if(!$href) { |
||
658 | $pos = strpos($src, '?'); |
||
659 | $href = $pos ? substr($src, 0, $pos) : $src; |
||
660 | } |
||
661 | |||
662 | $html = <<<EOD |
||
663 | <script type="text/javascript" src="https://asciinema.org/a/{$src}.js" id="asciicast-{$src}" async></script> |
||
664 | EOD; |
||
665 | |||
666 | return $html; |
||
667 | } |
||
668 | */ |
||
669 | |||
670 | |||
671 | |||
672 | /** |
||
673 | * Shortcode for including a SVG-image inside a <figure>. |
||
674 | * |
||
675 | * @param string $code the code to process. |
||
676 | * @param string $options for the shortcode. |
||
677 | * @return array with all the options. |
||
678 | */ |
||
679 | /*public static function ShortCodeSVGFigure($options) { |
||
680 | extract(array_merge(array( |
||
681 | 'id' => null, |
||
682 | 'class' => null, |
||
683 | 'src' => null, |
||
684 | 'path' => null, |
||
685 | 'title' => null, |
||
686 | 'alt' => null, |
||
687 | 'caption' => null, |
||
688 | 'href' => null, |
||
689 | 'nolink' => false, |
||
690 | ), CTextFilter::ShortCodeInit($options)), EXTR_SKIP); |
||
691 | |||
692 | $id = $id ? " id='$id'" : null; |
||
693 | //$class = $class ? " class='$class'" : null; |
||
694 | $class = $class ? " class='figure $class'" : " class='figure'"; |
||
695 | $title = $title ? " title='$title'" : null; |
||
696 | |||
697 | if(!$alt && $caption) { |
||
698 | $alt = $caption; |
||
699 | } |
||
700 | |||
701 | if(!$href) { |
||
702 | $pos = strpos($src, '?'); |
||
703 | $href = $pos ? substr($src, 0, $pos) : $src; |
||
704 | } |
||
705 | |||
706 | if(!$nolink) { |
||
707 | $a_start = "<a href='{$href}'>"; |
||
708 | $a_end = "</a>"; |
||
709 | } |
||
710 | |||
711 | // Import the file containing the svg-image |
||
712 | $svg = null; |
||
713 | |||
714 | if($path[0] != '/') { |
||
715 | $path = self::$dir . '/' . $path; |
||
716 | } |
||
717 | |||
718 | if(is_file($path)) { |
||
719 | $svg = file_get_contents($path); |
||
720 | } |
||
721 | else { |
||
722 | $svg = "No such file: $path"; |
||
723 | } |
||
724 | $html = <<<EOD |
||
725 | <figure{$id}{$class}> |
||
726 | {$svg} |
||
727 | <figcaption markdown=1>{$caption}</figcaption> |
||
728 | </figure> |
||
729 | EOD; |
||
730 | |||
731 | return $html; |
||
732 | } |
||
733 | |||
734 | */ |
||
735 | |||
736 | |||
737 | |||
738 | /** |
||
739 | * Shorttags to to quicker format text as HTML. |
||
740 | * |
||
741 | * @param string text text to be converted. |
||
742 | * @return string the formatted text. |
||
743 | */ |
||
744 | /*public static function ShortTags($text) { |
||
745 | $callback = function($matches) { |
||
746 | switch($matches[1]) { |
||
747 | case 'IMG': |
||
748 | $caption = t('Image: '); |
||
749 | $pos = strpos($matches[2], '?'); |
||
750 | $href = $pos ? substr($matches[2], 0, $pos) : $matches[2]; |
||
751 | $src = htmlspecialchars($matches[2]); |
||
752 | return <<<EOD |
||
753 | <figure> |
||
754 | <a href='{$href}'><img src='{$src}' alt='{$matches[3]}' /></a> |
||
755 | <figcaption markdown=1>{$caption}{$matches[3]}</figcaption> |
||
756 | </figure> |
||
757 | EOD; |
||
758 | |||
759 | case 'IMG2': |
||
760 | $caption = null; //t('Image: '); |
||
761 | $pos = strpos($matches[2], '?'); |
||
762 | $href = $pos ? substr($matches[2], 0, $pos) : $matches[2]; |
||
763 | $src = htmlspecialchars($matches[2]); |
||
764 | return <<<EOD |
||
765 | <figure class="{$matches[4]}"> |
||
766 | <a href='{$href}'><img src='{$src}' alt='{$matches[3]}' /></a> |
||
767 | <figcaption markdown=1>{$caption}{$matches[3]}</figcaption> |
||
768 | </figure> |
||
769 | EOD; |
||
770 | case 'BOOK': |
||
771 | $isbn = $matches[2]; |
||
772 | $stores = array( |
||
773 | 'BTH' => "http://bth.summon.serialssolutions.com/?#!/search?ho=t&q={$isbn}", |
||
774 | 'Libris' => "http://libris.kb.se/hitlist?q={$isbn}", |
||
775 | 'Google Books' => "http://books.google.com/books?q={$isbn}", |
||
776 | 'Bokus' => "http://www.bokus.com/bok/{$isbn}", |
||
777 | 'Adlibris' => "http://www.adlibris.com/se/product.aspx?isbn={$isbn}", |
||
778 | 'Amazon' => "http://www.amazon.com/s/ref=nb_ss?url=field-keywords={$isbn}", |
||
779 | 'Barnes&Noble' => "http://search.barnesandnoble.com/booksearch/ISBNInquiry.asp?r=1&IF=N&EAN={$isbn}", |
||
780 | ); |
||
781 | $html = null; |
||
782 | foreach($stores as $key => $val) { |
||
783 | $html .= "<a href='$val'>$key</a> • "; |
||
784 | } |
||
785 | return substr($html, 0, -8); |
||
786 | break; |
||
787 | |||
788 | case 'YOUTUBE': |
||
789 | $caption = t('Figure: '); |
||
790 | $height = ceil($matches[3] / (16/9)); |
||
791 | return <<<EOD |
||
792 | <figure> |
||
793 | <iframe width='{$matches[3]}' height='{$height}' src="http://www.youtube.com/embed/{$matches[2]}" frameborder="0" |
||
794 | allowfullscreen></iframe> |
||
795 | <figcaption>{$caption}{$matches[4]}</figcaption> |
||
796 | </figure> |
||
797 | EOD; |
||
798 | break; |
||
799 | |||
800 | case 'syntax=': return CTextFilter::SyntaxHighlightGeSHi($matches[3], $matches[2]); break; |
||
801 | case '```': return CTextFilter::SyntaxHighlightGeSHi($matches[3], $matches[2]); break; |
||
802 | //case 'syntax=': return "<pre>" . highlight_string($matches[3], true) . "</pre>"; break; |
||
803 | //case 'INCL': include($matches[2]); break; |
||
804 | case 'INFO': return "<div class='info' markdown=1>"; break; |
||
805 | case '/INFO': return "</div>"; break; |
||
806 | case 'BASEURL': return CLydia::Instance()->request->base_url; break; |
||
807 | case 'FIGURE': return CTextFilter::ShortCodeFigure($matches[2]); break; |
||
808 | case 'FIGURE-SVG': return CTextFilter::ShortCodeSVGFigure($matches[2]); break; |
||
809 | case 'ASCIINEMA': return CTextFilter::ShortCodeAsciinema($matches[2]); break; |
||
810 | default: return "{$matches[1]} IS UNKNOWN SHORTTAG."; break; |
||
811 | } |
||
812 | }; |
||
813 | $patterns = array( |
||
814 | '#\[(BASEURL)\]#', |
||
815 | //'/\[(AUTHOR) name=(.+) email=(.+) url=(.+)\]/', |
||
816 | '/\[(FIGURE)[\s+](.+)\]/', |
||
817 | '/\[(FIGURE-SVG)[\s+](.+)\]/', |
||
818 | '/\[(ASCIINEMA)[\s+](.+)\]/', |
||
819 | '/\[(IMG) src=(.+) alt=(.+)\]/', |
||
820 | '/\[(IMG2) src=(.+) alt="(.+)" class="(.+)"\]/', |
||
821 | '/\[(BOOK) isbn=(.+)\]/', |
||
822 | '/\[(YOUTUBE) src=(.+) width=(.+) caption=(.+)\]/', |
||
823 | '/~~~(syntax=)(php|html|html5|css|sql|javascript|bash)\n([^~]+)\n~~~/s', |
||
824 | '/(```)(php|html|html5|css|sql|javascript|bash|text|txt|python)\n([^`]+)\n```/s', |
||
825 | //'/\[(INCL)/s*([^\]+)/', |
||
826 | '#\[(INFO)\]#', '#\[(/INFO)\]#', |
||
827 | ); |
||
828 | |||
829 | $ret = preg_replace_callback($patterns, $callback, $text); |
||
830 | return $ret; |
||
831 | } |
||
832 | */ |
||
833 | |||
834 | |||
835 | |||
836 | /** |
||
837 | * Support SmartyPants for better typography. |
||
838 | * |
||
839 | * @param string text text to be converted. |
||
840 | * @return string the formatted text. |
||
841 | */ |
||
842 | /* public static function SmartyPants($text) { |
||
843 | require_once(__DIR__.'/php_smartypants_1.5.1e/smartypants.php'); |
||
844 | return SmartyPants($text); |
||
845 | } |
||
846 | */ |
||
847 | |||
848 | |||
849 | /** |
||
850 | * Support enhanced SmartyPants/Typographer for better typography. |
||
851 | * |
||
852 | * @param string text text to be converted. |
||
853 | * @return string the formatted text. |
||
854 | */ |
||
855 | /* public static function Typographer($text) { |
||
856 | require_once(__DIR__.'/php_smartypants_typographer_1.0/smartypants.php'); |
||
857 | $ret = SmartyPants($text); |
||
858 | return $ret; |
||
859 | } |
||
860 | */ |
||
861 | } |
||
862 |
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.