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 |
||
8 | abstract class AbstractDiff |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | * |
||
13 | * @deprecated since 0.1.0 |
||
14 | */ |
||
15 | public static $defaultSpecialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p'); |
||
|
|||
16 | /** |
||
17 | * @var array |
||
18 | * |
||
19 | * @deprecated since 0.1.0 |
||
20 | */ |
||
21 | public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
||
22 | /** |
||
23 | * @var bool |
||
24 | * |
||
25 | * @deprecated since 0.1.0 |
||
26 | */ |
||
27 | public static $defaultGroupDiffs = true; |
||
28 | |||
29 | /** |
||
30 | * @var HtmlDiffConfig |
||
31 | */ |
||
32 | protected $config; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $content; |
||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $oldText; |
||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $newText; |
||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $oldWords = array(); |
||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $newWords = array(); |
||
54 | |||
55 | /** |
||
56 | * @var DiffCache[] |
||
57 | */ |
||
58 | private $diffCaches = array(); |
||
59 | |||
60 | /** |
||
61 | * AbstractDiff constructor. |
||
62 | * |
||
63 | * @param string $oldText |
||
64 | * @param string $newText |
||
65 | * @param string $encoding |
||
66 | * @param null|array $specialCaseTags |
||
67 | * @param null|bool $groupDiffs |
||
68 | */ |
||
69 | 11 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
|
87 | |||
88 | /** |
||
89 | * @return bool|string |
||
90 | */ |
||
91 | abstract public function build(); |
||
92 | |||
93 | /** |
||
94 | * @return DiffCache|null |
||
95 | */ |
||
96 | protected function getDiffCache() |
||
110 | |||
111 | /** |
||
112 | * @return bool |
||
113 | */ |
||
114 | 11 | protected function hasDiffCache() |
|
118 | |||
119 | /** |
||
120 | * @return HtmlDiffConfig |
||
121 | */ |
||
122 | 11 | public function getConfig() |
|
126 | |||
127 | /** |
||
128 | * @param HtmlDiffConfig $config |
||
129 | * |
||
130 | * @return AbstractDiff |
||
131 | */ |
||
132 | 8 | public function setConfig(HtmlDiffConfig $config) |
|
138 | |||
139 | /** |
||
140 | * @return int |
||
141 | * |
||
142 | * @deprecated since 0.1.0 |
||
143 | */ |
||
144 | public function getMatchThreshold() |
||
148 | |||
149 | /** |
||
150 | * @param int $matchThreshold |
||
151 | * |
||
152 | * @return AbstractDiff |
||
153 | * |
||
154 | * @deprecated since 0.1.0 |
||
155 | */ |
||
156 | public function setMatchThreshold($matchThreshold) |
||
162 | |||
163 | /** |
||
164 | * @param array $chars |
||
165 | * |
||
166 | * @deprecated since 0.1.0 |
||
167 | */ |
||
168 | public function setSpecialCaseChars(array $chars) |
||
172 | |||
173 | /** |
||
174 | * @return array|null |
||
175 | * |
||
176 | * @deprecated since 0.1.0 |
||
177 | */ |
||
178 | public function getSpecialCaseChars() |
||
182 | |||
183 | /** |
||
184 | * @param string $char |
||
185 | * |
||
186 | * @deprecated since 0.1.0 |
||
187 | */ |
||
188 | public function addSpecialCaseChar($char) |
||
192 | |||
193 | /** |
||
194 | * @param string $char |
||
195 | * |
||
196 | * @deprecated since 0.1.0 |
||
197 | */ |
||
198 | public function removeSpecialCaseChar($char) |
||
202 | |||
203 | /** |
||
204 | * @param array $tags |
||
205 | * |
||
206 | * @deprecated since 0.1.0 |
||
207 | */ |
||
208 | public function setSpecialCaseTags(array $tags = array()) |
||
212 | |||
213 | /** |
||
214 | * @param string $tag |
||
215 | * |
||
216 | * @deprecated since 0.1.0 |
||
217 | */ |
||
218 | public function addSpecialCaseTag($tag) |
||
222 | |||
223 | /** |
||
224 | * @param string $tag |
||
225 | * |
||
226 | * @deprecated since 0.1.0 |
||
227 | */ |
||
228 | public function removeSpecialCaseTag($tag) |
||
232 | |||
233 | /** |
||
234 | * @return array|null |
||
235 | * |
||
236 | * @deprecated since 0.1.0 |
||
237 | */ |
||
238 | public function getSpecialCaseTags() |
||
242 | |||
243 | /** |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getOldHtml() |
||
250 | |||
251 | /** |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getNewHtml() |
||
258 | |||
259 | /** |
||
260 | * @return string |
||
261 | */ |
||
262 | public function getDifference() |
||
266 | |||
267 | /** |
||
268 | * Clears the diff content. |
||
269 | * |
||
270 | * @return void |
||
271 | */ |
||
272 | public function clearContent() |
||
276 | |||
277 | /** |
||
278 | * @param bool $boolean |
||
279 | * |
||
280 | * @return $this |
||
281 | * |
||
282 | * @deprecated since 0.1.0 |
||
283 | */ |
||
284 | public function setGroupDiffs($boolean) |
||
290 | |||
291 | /** |
||
292 | * @return bool |
||
293 | * |
||
294 | * @deprecated since 0.1.0 |
||
295 | */ |
||
296 | public function isGroupDiffs() |
||
300 | |||
301 | /** |
||
302 | * @param string $tag |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | protected function getOpeningTag($tag) |
||
310 | |||
311 | /** |
||
312 | * @param string $tag |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | protected function getClosingTag($tag) |
||
320 | |||
321 | /** |
||
322 | * @param string $str |
||
323 | * @param string $start |
||
324 | * @param string $end |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | protected function getStringBetween($str, $start, $end) |
||
342 | |||
343 | /** |
||
344 | * @param string $html |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | 11 | protected function purifyHtml($html) |
|
361 | |||
362 | 11 | protected function splitInputsToWords() |
|
367 | |||
368 | /** |
||
369 | * @param string $text |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | 11 | protected function isPartOfWord($text) |
|
377 | |||
378 | /** |
||
379 | * @param array $characterString |
||
380 | * |
||
381 | * @return array |
||
382 | */ |
||
383 | 11 | protected function convertHtmlToListOfWords($characterString) |
|
458 | |||
459 | /** |
||
460 | * @param string $val |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | 11 | protected function isStartOfTag($val) |
|
468 | |||
469 | /** |
||
470 | * @param string $val |
||
471 | * |
||
472 | * @return bool |
||
473 | */ |
||
474 | 11 | protected function isEndOfTag($val) |
|
478 | |||
479 | /** |
||
480 | * @param string $value |
||
481 | * |
||
482 | * @return bool |
||
483 | */ |
||
484 | protected function isWhiteSpace($value) |
||
488 | |||
489 | /** |
||
490 | * @param string $value |
||
491 | * |
||
492 | * @return array |
||
493 | */ |
||
494 | 11 | protected function explode($value) |
|
499 | } |
||
500 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.