Complex classes like SSHTMLBBCodeParser 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 SSHTMLBBCodeParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
56 | class SSHTMLBBCodeParser |
||
57 | { |
||
58 | /** |
||
59 | * An array of tags parsed by the engine, should be overwritten by filters |
||
60 | * |
||
61 | * @access private |
||
62 | * @var array |
||
63 | */ |
||
64 | var $_definedTags = array(); |
||
65 | |||
66 | /** |
||
67 | * A string containing the input |
||
68 | * |
||
69 | * @access private |
||
70 | * @var string |
||
71 | */ |
||
72 | var $_text = ''; |
||
73 | |||
74 | /** |
||
75 | * A string containing the preparsed input |
||
76 | * |
||
77 | * @access private |
||
78 | * @var string |
||
79 | */ |
||
80 | var $_preparsed = ''; |
||
81 | |||
82 | /** |
||
83 | * An array tags and texts build from the input text |
||
84 | * |
||
85 | * @access private |
||
86 | * @var array |
||
87 | */ |
||
88 | var $_tagArray = array(); |
||
89 | |||
90 | /** |
||
91 | * A string containing the parsed version of the text |
||
92 | * |
||
93 | * @access private |
||
94 | * @var string |
||
95 | */ |
||
96 | var $_parsed = ''; |
||
97 | |||
98 | /** |
||
99 | * An array of options, filled by an ini file or through the contructor |
||
100 | * |
||
101 | * @access private |
||
102 | * @var array |
||
103 | */ |
||
104 | var $_options = array( |
||
105 | 'quotestyle' => 'double', |
||
106 | 'quotewhat' => 'all', |
||
107 | 'open' => '[', |
||
108 | 'close' => ']', |
||
109 | 'xmlclose' => true, |
||
110 | 'filters' => 'Basic' |
||
111 | ); |
||
112 | |||
113 | /** |
||
114 | * An array of filters used for parsing |
||
115 | * |
||
116 | * @access private |
||
117 | * @var array |
||
118 | */ |
||
119 | var $_filters = array(); |
||
120 | |||
121 | /** |
||
122 | * Constructor, initialises the options and filters |
||
123 | * |
||
124 | * Sets the private variable _options with base options defined with |
||
125 | * &PEAR::getStaticProperty(), overwriting them with (if present) |
||
126 | * the argument to this method. |
||
127 | * Then it sets the extra options to properly escape the tag |
||
128 | * characters in preg_replace() etc. The set options are |
||
129 | * then stored back with &PEAR::getStaticProperty(), so that the filter |
||
130 | * classes can use them. |
||
131 | * All the filters in the options are initialised and their defined tags |
||
132 | * are copied into the private variable _definedTags. |
||
133 | * |
||
134 | * @param array options to use, can be left out |
||
135 | * @return none |
||
136 | * @access public |
||
137 | * @author Stijn de Reede <[email protected]> |
||
138 | */ |
||
139 | public function SSHTMLBBCodeParser($options = array()) |
||
179 | |||
180 | static function &getStaticProperty($class, $var) |
||
191 | |||
192 | /** |
||
193 | * Option setter |
||
194 | * |
||
195 | * @param string option name |
||
196 | * @param mixed option value |
||
197 | * @author Lorenzo Alberton <[email protected]> |
||
198 | */ |
||
199 | public function setOption($name, $value) |
||
203 | |||
204 | /** |
||
205 | * Add a new filter |
||
206 | * |
||
207 | * @param string filter |
||
208 | * @author Lorenzo Alberton <[email protected]> |
||
209 | */ |
||
210 | public function addFilter($filter) |
||
232 | |||
233 | /** |
||
234 | * Remove an existing filter |
||
235 | * |
||
236 | * @param string $filter |
||
237 | * @author Lorenzo Alberton <[email protected]> |
||
238 | */ |
||
239 | public function removeFilter($filter) |
||
255 | |||
256 | /** |
||
257 | * Add new filters |
||
258 | * |
||
259 | * @param mixed (array or string) |
||
260 | * @return boolean true if all ok, false if not. |
||
261 | * @author Lorenzo Alberton <[email protected]> |
||
262 | */ |
||
263 | public function addFilters($filters) |
||
284 | |||
285 | /** |
||
286 | * Executes statements before the actual array building starts |
||
287 | * |
||
288 | * This method should be overwritten in a filter if you want to do |
||
289 | * something before the parsing process starts. This can be useful to |
||
290 | * allow certain short alternative tags which then can be converted into |
||
291 | * proper tags with preg_replace() calls. |
||
292 | * The main class walks through all the filters and and calls this |
||
293 | * method. The filters should modify their private $_preparsed |
||
294 | * variable, with input from $_text. |
||
295 | * |
||
296 | * @return none |
||
297 | * @access private |
||
298 | * @see $_text |
||
299 | * @author Stijn de Reede <[email protected]> |
||
300 | */ |
||
301 | public function _preparse() |
||
318 | |||
319 | /** |
||
320 | * Builds the tag array from the input string $_text |
||
321 | * |
||
322 | * An array consisting of tag and text elements is contructed from the |
||
323 | * $_preparsed variable. The method uses _buildTag() to check if a tag is |
||
324 | * valid and to build the actual tag to be added to the tag array. |
||
325 | * |
||
326 | * @todo - rewrite whole method, as this one is old and probably slow |
||
327 | * - see if a recursive method would be better than an iterative one |
||
328 | * |
||
329 | * @return none |
||
330 | * @access private |
||
331 | * @see _buildTag() |
||
332 | * @see $_text |
||
333 | * @see $_tagArray |
||
334 | * @author Stijn de Reede <[email protected]> |
||
335 | */ |
||
336 | public function _buildTagArray() |
||
399 | |||
400 | /** |
||
401 | * Builds a tag from the input string |
||
402 | * |
||
403 | * This method builds a tag array based on the string it got as an |
||
404 | * argument. If the tag is invalid, <false> is returned. The tag |
||
405 | * attributes are extracted from the string and stored in the tag |
||
406 | * array as an associative array. |
||
407 | * |
||
408 | * @param string string to build tag from |
||
409 | * @return array tag in array format |
||
410 | * @access private |
||
411 | * @see _buildTagArray() |
||
412 | * @author Stijn de Reede <[email protected]> |
||
413 | */ |
||
414 | public function _buildTag($str) |
||
469 | |||
470 | /** |
||
471 | * Validates the tag array, regarding the allowed tags |
||
472 | * |
||
473 | * While looping through the tag array, two following text tags are |
||
474 | * joined, and it is checked that the tag is allowed inside the |
||
475 | * last opened tag. |
||
476 | * By remembering what tags have been opened it is checked that |
||
477 | * there is correct (xml compliant) nesting. |
||
478 | * In the end all still opened tags are closed. |
||
479 | * |
||
480 | * @return none |
||
481 | * @access private |
||
482 | * @see _isAllowed() |
||
483 | * @see $_tagArray |
||
484 | * @author Stijn de Reede <[email protected]>, Seth Price <[email protected]> |
||
485 | */ |
||
486 | public function _validateTagArray() |
||
591 | |||
592 | /** |
||
593 | * Checks to see if a parent is needed |
||
594 | * |
||
595 | * Checks to see if the current $in tag has an appropriate parent. If it |
||
596 | * does, then it returns false. If a parent is needed, then it returns the |
||
597 | * first tag in the list to add to the stack. |
||
598 | * |
||
599 | * @param array tag that is on the outside |
||
600 | * @param array tag that is on the inside |
||
601 | * @return boolean false if not needed, tag if needed, true if out |
||
602 | * of our minds |
||
603 | * @access private |
||
604 | * @see _validateTagArray() |
||
605 | * @author Seth Price <[email protected]> |
||
606 | */ |
||
607 | public function _parentNeeded($out, $in) |
||
631 | |||
632 | /** |
||
633 | * Checks to see if a child is needed |
||
634 | * |
||
635 | * Checks to see if the current $out tag has an appropriate child. If it |
||
636 | * does, then it returns false. If a child is needed, then it returns the |
||
637 | * first tag in the list to add to the stack. |
||
638 | * |
||
639 | * @param array tag that is on the outside |
||
640 | * @param array tag that is on the inside |
||
641 | * @return boolean false if not needed, tag if needed, true if out |
||
642 | * of our minds |
||
643 | * @access private |
||
644 | * @see _validateTagArray() |
||
645 | * @author Seth Price <[email protected]> |
||
646 | */ |
||
647 | public function _childNeeded($out, $in) |
||
671 | |||
672 | /** |
||
673 | * Checks to see if a tag is allowed inside another tag |
||
674 | * |
||
675 | * The allowed tags are extracted from the private _definedTags array. |
||
676 | * |
||
677 | * @param array tag that is on the outside |
||
678 | * @param array tag that is on the inside |
||
679 | * @return boolean return true if the tag is allowed, false |
||
680 | * otherwise |
||
681 | * @access private |
||
682 | * @see _validateTagArray() |
||
683 | * @author Stijn de Reede <[email protected]> |
||
684 | */ |
||
685 | public function _isAllowed($out, $in) |
||
704 | |||
705 | /** |
||
706 | * Builds a parsed string based on the tag array |
||
707 | * |
||
708 | * The correct html and attribute values are extracted from the private |
||
709 | * _definedTags array. |
||
710 | * |
||
711 | * @return none |
||
712 | * @access private |
||
713 | * @see $_tagArray |
||
714 | * @see $_parsed |
||
715 | * @author Stijn de Reede <[email protected]> |
||
716 | */ |
||
717 | public function _buildParsedString() |
||
763 | |||
764 | /** |
||
765 | * Sets text in the object to be parsed |
||
766 | * |
||
767 | * @param string the text to set in the object |
||
768 | * @return none |
||
769 | * @access public |
||
770 | * @see getText() |
||
771 | * @see $_text |
||
772 | * @author Stijn de Reede <[email protected]> |
||
773 | */ |
||
774 | public function setText($str) |
||
778 | |||
779 | /** |
||
780 | * Gets the unparsed text from the object |
||
781 | * |
||
782 | * @return string the text set in the object |
||
783 | * @access public |
||
784 | * @see setText() |
||
785 | * @see $_text |
||
786 | * @author Stijn de Reede <[email protected]> |
||
787 | */ |
||
788 | public function getText() |
||
792 | |||
793 | /** |
||
794 | * Gets the preparsed text from the object |
||
795 | * |
||
796 | * @return string the text set in the object |
||
797 | * @access public |
||
798 | * @see _preparse() |
||
799 | * @see $_preparsed |
||
800 | * @author Stijn de Reede <[email protected]> |
||
801 | */ |
||
802 | public function getPreparsed() |
||
806 | |||
807 | /** |
||
808 | * Gets the parsed text from the object |
||
809 | * |
||
810 | * @return string the parsed text set in the object |
||
811 | * @access public |
||
812 | * @see parse() |
||
813 | * @see $_parsed |
||
814 | * @author Stijn de Reede <[email protected]> |
||
815 | */ |
||
816 | public function getParsed() |
||
820 | |||
821 | /** |
||
822 | * Parses the text set in the object |
||
823 | * |
||
824 | * @return none |
||
825 | * @access public |
||
826 | * @see _preparse() |
||
827 | * @see _buildTagArray() |
||
828 | * @see _validateTagArray() |
||
829 | * @see _buildParsedString() |
||
830 | * @author Stijn de Reede <[email protected]> |
||
831 | */ |
||
832 | public function parse() |
||
839 | |||
840 | /** |
||
841 | * Quick method to do setText(), parse() and getParsed at once |
||
842 | * |
||
843 | * @return none |
||
844 | * @access public |
||
845 | * @see parse() |
||
846 | * @see $_text |
||
847 | * @author Stijn de Reede <[email protected]> |
||
848 | */ |
||
849 | public function qparse($str) |
||
855 | |||
856 | /** |
||
857 | * Quick static method to do setText(), parse() and getParsed at once |
||
858 | * |
||
859 | * @return none |
||
860 | * @access public |
||
861 | * @see parse() |
||
862 | * @see $_text |
||
863 | * @author Stijn de Reede <[email protected]> |
||
864 | */ |
||
865 | public function staticQparse($str) |
||
872 | } |
||
873 |