Complex classes like AbstractDiff 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 AbstractDiff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | abstract class AbstractDiff |
||
6 | { |
||
7 | public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
|
|||
8 | public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
||
9 | public static $defaultGroupDiffs = true; |
||
10 | |||
11 | protected $content; |
||
12 | protected $oldText; |
||
13 | protected $newText; |
||
14 | protected $oldWords = array(); |
||
15 | protected $newWords = array(); |
||
16 | protected $encoding; |
||
17 | protected $specialCaseOpeningTags = array(); |
||
18 | protected $specialCaseClosingTags = array(); |
||
19 | protected $specialCaseTags; |
||
20 | protected $specialCaseChars; |
||
21 | protected $groupDiffs; |
||
22 | protected $matchThreshold = 80; |
||
23 | |||
24 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
||
42 | |||
43 | /** |
||
44 | * @return int |
||
45 | */ |
||
46 | public function getMatchThreshold() |
||
50 | |||
51 | /** |
||
52 | * @param int $matchThreshold |
||
53 | * |
||
54 | * @return AbstractDiff |
||
55 | */ |
||
56 | public function setMatchThreshold($matchThreshold) |
||
62 | |||
63 | |||
64 | |||
65 | public function setSpecialCaseChars(array $chars) |
||
69 | |||
70 | public function getSpecialCaseChars() |
||
74 | |||
75 | public function addSpecialCaseChar($char) |
||
81 | |||
82 | public function removeSpecialCaseChar($char) |
||
89 | |||
90 | public function setSpecialCaseTags(array $tags = array()) |
||
98 | |||
99 | public function addSpecialCaseTag($tag) |
||
115 | |||
116 | public function removeSpecialCaseTag($tag) |
||
132 | |||
133 | public function getSpecialCaseTags() |
||
137 | |||
138 | public function getOldHtml() |
||
142 | |||
143 | public function getNewHtml() |
||
147 | |||
148 | public function getDifference() |
||
152 | |||
153 | public function setGroupDiffs($boolean) |
||
157 | |||
158 | public function isGroupDiffs() |
||
162 | |||
163 | protected function getOpeningTag($tag) |
||
167 | |||
168 | protected function getClosingTag($tag) |
||
172 | |||
173 | protected function getStringBetween($str, $start, $end) |
||
187 | |||
188 | protected function purifyHtml($html, $tags = null) |
||
201 | |||
202 | protected function splitInputsToWords() |
||
207 | |||
208 | protected function isPartOfWord($text) |
||
212 | |||
213 | protected function convertHtmlToListOfWords($characterString) |
||
287 | |||
288 | protected function isStartOfTag($val) |
||
292 | |||
293 | protected function isEndOfTag($val) |
||
297 | |||
298 | protected function isWhiteSpace($value) |
||
302 | |||
303 | protected function explode($value) |
||
308 | } |
||
309 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.