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 MyTextSanitizer 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 MyTextSanitizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
133 | class MyTextSanitizer |
||
134 | { |
||
135 | /** |
||
136 | * |
||
137 | * @var array |
||
138 | */ |
||
139 | public $smileys = array(); |
||
140 | |||
141 | /** |
||
142 | */ |
||
143 | public $censorConf; |
||
144 | |||
145 | /** |
||
146 | * |
||
147 | * @var holding reference to text |
||
148 | */ |
||
149 | public $text = ''; |
||
150 | public $patterns = array(); |
||
151 | public $replacements = array(); |
||
152 | |||
153 | //mb------------------------------ |
||
154 | public $callbackPatterns = array(); |
||
155 | public $callbacks = array(); |
||
156 | //mb------------------------------ |
||
157 | |||
158 | public $path_basic; |
||
159 | public $path_plugin; |
||
160 | |||
161 | public $config; |
||
162 | |||
163 | /** |
||
164 | * Constructor of this class |
||
165 | * |
||
166 | * Gets allowed html tags from admin config settings |
||
167 | * <br> should not be allowed since nl2br will be used |
||
168 | * when storing data. |
||
169 | * |
||
170 | * @access private |
||
171 | */ |
||
172 | |||
173 | public function __construct() |
||
179 | |||
180 | /** |
||
181 | * Enter description here... |
||
182 | * |
||
183 | * @param string $name |
||
184 | * @return array |
||
185 | */ |
||
186 | public function loadConfig($name = null) |
||
199 | |||
200 | /** |
||
201 | * Enter description here... |
||
202 | * |
||
203 | * @param array $config_default |
||
204 | * @param array $config_custom |
||
205 | * @return unknown |
||
206 | */ |
||
207 | public function mergeConfig($config_default, $config_custom) |
||
221 | |||
222 | /** |
||
223 | * Access the only instance of this class |
||
224 | * |
||
225 | * @return object |
||
226 | * @static |
||
227 | * @staticvar object |
||
228 | */ |
||
229 | public static function getInstance() |
||
238 | |||
239 | /** |
||
240 | * Get the smileys |
||
241 | * |
||
242 | * @param bool $isAll TRUE for all smileys, FALSE for smileys with display = 1 |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | public function getSmileys($isAll = true) |
||
270 | |||
271 | /** |
||
272 | * Replace emoticons in the message with smiley images |
||
273 | * |
||
274 | * @param string $message |
||
275 | * @return string |
||
276 | */ |
||
277 | public function smiley($message) |
||
286 | |||
287 | /** |
||
288 | * @param $match |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | public function makeClickableCallback01($match) |
||
296 | |||
297 | /** |
||
298 | * @param $match |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | public function makeClickableCallback02($match) |
||
306 | |||
307 | /** |
||
308 | * @param $match |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | public function makeClickableCallback03($match) |
||
316 | |||
317 | /** |
||
318 | * @param $match |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | public function makeClickableCallback04($match) |
||
326 | |||
327 | /** |
||
328 | * Make links in the text clickable |
||
329 | * |
||
330 | * @param string $text |
||
331 | * @return string |
||
332 | */ |
||
333 | public function makeClickable(&$text) |
||
374 | |||
375 | /** |
||
376 | * MyTextSanitizer::truncate() |
||
377 | * |
||
378 | * @param mixed $text |
||
379 | * @return mixed|string |
||
380 | */ |
||
381 | public function truncate($text) |
||
392 | |||
393 | /** |
||
394 | * Replace XoopsCodes with their equivalent HTML formatting |
||
395 | * |
||
396 | * @param string $text |
||
397 | * @param bool|int $allowimage Allow images in the text? |
||
398 | * On FALSE, uses links to images. |
||
399 | * @return string |
||
400 | */ |
||
401 | public function &xoopsCodeDecode(&$text, $allowimage = 1) |
||
456 | |||
457 | /** |
||
458 | * Convert quote tags |
||
459 | * |
||
460 | * @param string $text |
||
461 | * @return string |
||
462 | */ |
||
463 | public function quoteConv($text) |
||
478 | |||
479 | /** |
||
480 | * A quick solution for filtering XSS scripts |
||
481 | * |
||
482 | * @TODO : To be improved |
||
483 | * @param $text |
||
484 | * @return mixed |
||
485 | */ |
||
486 | public function filterXss($text) |
||
502 | |||
503 | /** |
||
504 | * Convert linebreaks to <br /> tags |
||
505 | * |
||
506 | * @param string $text |
||
507 | * @return string |
||
508 | */ |
||
509 | public function nl2Br($text) |
||
513 | |||
514 | /** |
||
515 | * Add slashes to the text if magic_quotes_gpc is turned off. |
||
516 | * |
||
517 | * @param string $text |
||
518 | * @return string |
||
519 | */ |
||
520 | public function addSlashes($text) |
||
528 | |||
529 | /** |
||
530 | * if magic_quotes_gpc is on, stirip back slashes |
||
531 | * |
||
532 | * @param string $text |
||
533 | * @return string |
||
534 | */ |
||
535 | public function stripSlashesGPC($text) |
||
543 | |||
544 | /** |
||
545 | * Convert special characters to HTML entities |
||
546 | * |
||
547 | * @param string $text string being converted |
||
548 | * @param int $quote_style |
||
549 | * @param string $charset character set used in conversion |
||
550 | * @param bool $double_encode |
||
551 | * @return string |
||
552 | */ |
||
553 | public function htmlSpecialChars($text, $quote_style = ENT_QUOTES, $charset = null, $double_encode = true) |
||
563 | |||
564 | /** |
||
565 | * Reverses {@link htmlSpecialChars()} |
||
566 | * |
||
567 | * @param string $text |
||
568 | * @return string |
||
569 | */ |
||
570 | public function undoHtmlSpecialChars($text) |
||
574 | |||
575 | /** |
||
576 | * Filters textarea form data in DB for display |
||
577 | * |
||
578 | * @param string $text |
||
579 | * @param bool|int $html allow html? |
||
580 | * @param bool|int $smiley allow smileys? |
||
581 | * @param bool|int $xcode allow xoopscode? |
||
582 | * @param bool|int $image allow inline images? |
||
583 | * @param bool|int $br convert linebreaks? |
||
584 | * @return string |
||
585 | */ |
||
586 | public function &displayTarea($text, $html = 0, $smiley = 1, $xcode = 1, $image = 1, $br = 1) |
||
622 | |||
623 | /** |
||
624 | * Filters textarea form data submitted for preview |
||
625 | * |
||
626 | * @param string $text |
||
627 | * @param bool|int $html allow html? |
||
628 | * @param bool|int $smiley allow smileys? |
||
629 | * @param bool|int $xcode allow xoopscode? |
||
630 | * @param bool|int $image allow inline images? |
||
631 | * @param bool|int $br convert linebreaks? |
||
632 | * @return string |
||
633 | */ |
||
634 | public function &previewTarea($text, $html = 0, $smiley = 1, $xcode = 1, $image = 1, $br = 1) |
||
641 | |||
642 | /** |
||
643 | * Replaces banned words in a string with their replacements |
||
644 | * |
||
645 | * @param string $text |
||
646 | * @return string |
||
647 | * @deprecated |
||
648 | */ |
||
649 | public function &censorString(&$text) |
||
658 | |||
659 | /** |
||
660 | * MyTextSanitizer::codePreConv() |
||
661 | * |
||
662 | * @param mixed $text |
||
663 | * @param mixed $xcode |
||
664 | * @return mixed |
||
665 | */ |
||
666 | public function codePreConv($text, $xcode = 1) |
||
684 | |||
685 | /** |
||
686 | * @param $match |
||
687 | * |
||
688 | * @return string |
||
689 | */ |
||
690 | public function codeConvCallback($match) |
||
694 | |||
695 | /** |
||
696 | * MyTextSanitizer::codeConv() |
||
697 | * |
||
698 | * @param mixed $text |
||
699 | * @param mixed $xcode |
||
700 | * @return mixed |
||
701 | */ |
||
702 | public function codeConv($text, $xcode = 1) |
||
712 | |||
713 | /** |
||
714 | * MyTextSanitizer::executeExtensions() |
||
715 | * |
||
716 | * @return bool |
||
717 | */ |
||
718 | public function executeExtensions() |
||
729 | |||
730 | /** |
||
731 | * MyTextSanitizer::loadExtension() |
||
732 | * |
||
733 | * @param mixed $name |
||
734 | * @return bool|null |
||
735 | */ |
||
736 | public function loadExtension($name) |
||
756 | |||
757 | /** |
||
758 | * MyTextSanitizer::executeExtension() |
||
759 | * |
||
760 | * @param mixed $name |
||
761 | * @return mixed |
||
762 | */ |
||
763 | public function executeExtension($name) |
||
770 | |||
771 | /** |
||
772 | * Filter out possible malicious text |
||
773 | * kses project at SF could be a good solution to check |
||
774 | * |
||
775 | * @param string $text text to filter |
||
776 | * @param bool $force force filtering |
||
777 | * @return string filtered text |
||
778 | */ |
||
779 | public function textFilter($text, $force = false) |
||
788 | |||
789 | // #################### Deprecated Methods ###################### |
||
790 | /** |
||
791 | * *#@+ |
||
792 | * |
||
793 | * @deprecated |
||
794 | */ |
||
795 | |||
796 | /** |
||
797 | * MyTextSanitizer::codeSanitizer() |
||
798 | * |
||
799 | * @param mixed $str |
||
800 | * @param mixed $image |
||
801 | * @return mixed|string |
||
802 | */ |
||
803 | public function codeSanitizer($str, $image = 1) |
||
811 | |||
812 | /** |
||
813 | * MyTextSanitizer::sanitizeForDisplay() |
||
814 | * |
||
815 | * @param mixed $text |
||
816 | * @param integer $allowhtml |
||
817 | * @param integer $smiley |
||
818 | * @param mixed $bbcode |
||
819 | * @return mixed|string |
||
820 | */ |
||
821 | View Code Duplication | public function sanitizeForDisplay($text, $allowhtml = 0, $smiley = 1, $bbcode = 1) |
|
842 | |||
843 | /** |
||
844 | * MyTextSanitizer::sanitizeForPreview() |
||
845 | * |
||
846 | * @param mixed $text |
||
847 | * @param integer $allowhtml |
||
848 | * @param integer $smiley |
||
849 | * @param mixed $bbcode |
||
850 | * @return mixed|string |
||
851 | */ |
||
852 | View Code Duplication | public function sanitizeForPreview($text, $allowhtml = 0, $smiley = 1, $bbcode = 1) |
|
874 | |||
875 | /** |
||
876 | * MyTextSanitizer::makeTboxData4Save() |
||
877 | * |
||
878 | * @param mixed $text |
||
879 | * @return string |
||
880 | */ |
||
881 | public function makeTboxData4Save($text) |
||
888 | |||
889 | /** |
||
890 | * MyTextSanitizer::makeTboxData4Show() |
||
891 | * |
||
892 | * @param mixed $text |
||
893 | * @param mixed $smiley |
||
894 | * @return mixed|string |
||
895 | */ |
||
896 | View Code Duplication | public function makeTboxData4Show($text, $smiley = 0) |
|
903 | |||
904 | /** |
||
905 | * MyTextSanitizer::makeTboxData4Edit() |
||
906 | * |
||
907 | * @param mixed $text |
||
908 | * @return string |
||
909 | */ |
||
910 | public function makeTboxData4Edit($text) |
||
916 | |||
917 | /** |
||
918 | * MyTextSanitizer::makeTboxData4Preview() |
||
919 | * |
||
920 | * @param mixed $text |
||
921 | * @param mixed $smiley |
||
922 | * @return mixed|string |
||
923 | */ |
||
924 | View Code Duplication | public function makeTboxData4Preview($text, $smiley = 0) |
|
932 | |||
933 | /** |
||
934 | * MyTextSanitizer::makeTboxData4PreviewInForm() |
||
935 | * |
||
936 | * @param mixed $text |
||
937 | * @return string |
||
938 | */ |
||
939 | View Code Duplication | public function makeTboxData4PreviewInForm($text) |
|
946 | |||
947 | /** |
||
948 | * MyTextSanitizer::makeTareaData4Save() |
||
949 | * |
||
950 | * @param mixed $text |
||
951 | * @return string |
||
952 | */ |
||
953 | public function makeTareaData4Save($text) |
||
959 | |||
960 | /** |
||
961 | * MyTextSanitizer::makeTareaData4Show() |
||
962 | * |
||
963 | * @param mixed $text |
||
964 | * @param integer $html |
||
965 | * @param integer $smiley |
||
966 | * @param mixed $xcode |
||
967 | * @return mixed|string |
||
968 | */ |
||
969 | View Code Duplication | public function &makeTareaData4Show(&$text, $html = 1, $smiley = 1, $xcode = 1) |
|
976 | |||
977 | /** |
||
978 | * MyTextSanitizer::makeTareaData4Edit() |
||
979 | * |
||
980 | * @param mixed $text |
||
981 | * @return string |
||
982 | */ |
||
983 | public function makeTareaData4Edit($text) |
||
989 | |||
990 | /** |
||
991 | * MyTextSanitizer::makeTareaData4Preview() |
||
992 | * |
||
993 | * @param mixed $text |
||
994 | * @param integer $html |
||
995 | * @param integer $smiley |
||
996 | * @param mixed $xcode |
||
997 | * @return mixed|string |
||
998 | */ |
||
999 | View Code Duplication | public function &makeTareaData4Preview(&$text, $html = 1, $smiley = 1, $xcode = 1) |
|
1006 | |||
1007 | /** |
||
1008 | * MyTextSanitizer::makeTareaData4PreviewInForm() |
||
1009 | * |
||
1010 | * @param mixed $text |
||
1011 | * @return string |
||
1012 | */ |
||
1013 | View Code Duplication | public function makeTareaData4PreviewInForm($text) |
|
1021 | |||
1022 | /** |
||
1023 | * MyTextSanitizer::makeTareaData4InsideQuotes() |
||
1024 | * |
||
1025 | * @param mixed $text |
||
1026 | * @return string |
||
1027 | */ |
||
1028 | public function makeTareaData4InsideQuotes($text) |
||
1034 | |||
1035 | /** |
||
1036 | * MyTextSanitizer::oopsStripSlashesGPC() |
||
1037 | * |
||
1038 | * @param mixed $text |
||
1039 | * @return string |
||
1040 | */ |
||
1041 | public function oopsStripSlashesGPC($text) |
||
1047 | |||
1048 | /** |
||
1049 | * MyTextSanitizer::oopsStripSlashesRT() |
||
1050 | * |
||
1051 | * @param mixed $text |
||
1052 | * @return mixed|string |
||
1053 | */ |
||
1054 | public function oopsStripSlashesRT($text) |
||
1063 | |||
1064 | /** |
||
1065 | * MyTextSanitizer::oopsAddSlashes() |
||
1066 | * |
||
1067 | * @param mixed $text |
||
1068 | * @return string |
||
1069 | */ |
||
1070 | public function oopsAddSlashes($text) |
||
1076 | |||
1077 | /** |
||
1078 | * MyTextSanitizer::oopsHtmlSpecialChars() |
||
1079 | * |
||
1080 | * @param mixed $text |
||
1081 | * @return string |
||
1082 | */ |
||
1083 | public function oopsHtmlSpecialChars($text) |
||
1089 | |||
1090 | /** |
||
1091 | * MyTextSanitizer::oopsNl2Br() |
||
1092 | * |
||
1093 | * @param mixed $text |
||
1094 | * @return string |
||
1095 | */ |
||
1096 | public function oopsNl2Br($text) |
||
1102 | } |
||
1103 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.