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 | /** |
||
18 | * @var array |
||
19 | * |
||
20 | * @deprecated since 0.1.0 |
||
21 | */ |
||
22 | public static $defaultSpecialCaseChars = array('.', ',', '(', ')', '\''); |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | * |
||
27 | * @deprecated since 0.1.0 |
||
28 | */ |
||
29 | public static $defaultGroupDiffs = true; |
||
30 | |||
31 | /** |
||
32 | * @var HtmlDiffConfig |
||
33 | */ |
||
34 | protected $config; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $content; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $oldText; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $newText; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $oldWords = array(); |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $newWords = array(); |
||
60 | |||
61 | /** |
||
62 | * @var DiffCache[] |
||
63 | */ |
||
64 | protected $diffCaches = array(); |
||
65 | |||
66 | /** |
||
67 | * @var \HTMLPurifier |
||
68 | */ |
||
69 | protected $purifier; |
||
70 | |||
71 | /** |
||
72 | * @var \HTMLPurifier_Config|null |
||
73 | */ |
||
74 | protected $purifierConfig = null; |
||
75 | |||
76 | /** |
||
77 | * AbstractDiff constructor. |
||
78 | * |
||
79 | * @param string $oldText |
||
80 | * @param string $newText |
||
81 | * @param string $encoding |
||
82 | * @param null|array $specialCaseTags |
||
83 | * @param null|bool $groupDiffs |
||
84 | */ |
||
85 | 14 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
|
103 | |||
104 | /** |
||
105 | * @return bool|string |
||
106 | */ |
||
107 | abstract public function build(); |
||
108 | |||
109 | /** |
||
110 | * Initializes HTMLPurifier with cache location. |
||
111 | * |
||
112 | * @param null|string $defaultPurifierSerializerCache |
||
113 | */ |
||
114 | 14 | public function initPurifier($defaultPurifierSerializerCache = null) |
|
133 | |||
134 | /** |
||
135 | * Prepare (purify) the HTML |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | 14 | protected function prepare() |
|
146 | |||
147 | /** |
||
148 | * @return DiffCache|null |
||
149 | */ |
||
150 | 1 | protected function getDiffCache() |
|
164 | |||
165 | /** |
||
166 | * @return bool |
||
167 | */ |
||
168 | 14 | protected function hasDiffCache() |
|
172 | |||
173 | /** |
||
174 | * @return HtmlDiffConfig |
||
175 | */ |
||
176 | 14 | public function getConfig() |
|
180 | |||
181 | /** |
||
182 | * @param HtmlDiffConfig $config |
||
183 | * |
||
184 | * @return AbstractDiff |
||
185 | */ |
||
186 | 14 | public function setConfig(HtmlDiffConfig $config) |
|
192 | |||
193 | /** |
||
194 | * @return int |
||
195 | * |
||
196 | * @deprecated since 0.1.0 |
||
197 | */ |
||
198 | public function getMatchThreshold() |
||
202 | |||
203 | /** |
||
204 | * @param int $matchThreshold |
||
205 | * |
||
206 | * @return AbstractDiff |
||
207 | * |
||
208 | * @deprecated since 0.1.0 |
||
209 | */ |
||
210 | public function setMatchThreshold($matchThreshold) |
||
216 | |||
217 | /** |
||
218 | * @param array $chars |
||
219 | * |
||
220 | * @deprecated since 0.1.0 |
||
221 | */ |
||
222 | public function setSpecialCaseChars(array $chars) |
||
226 | |||
227 | /** |
||
228 | * @return array|null |
||
229 | * |
||
230 | * @deprecated since 0.1.0 |
||
231 | */ |
||
232 | public function getSpecialCaseChars() |
||
236 | |||
237 | /** |
||
238 | * @param string $char |
||
239 | * |
||
240 | * @deprecated since 0.1.0 |
||
241 | */ |
||
242 | public function addSpecialCaseChar($char) |
||
246 | |||
247 | /** |
||
248 | * @param string $char |
||
249 | * |
||
250 | * @deprecated since 0.1.0 |
||
251 | */ |
||
252 | public function removeSpecialCaseChar($char) |
||
256 | |||
257 | /** |
||
258 | * @param array $tags |
||
259 | * |
||
260 | * @deprecated since 0.1.0 |
||
261 | */ |
||
262 | public function setSpecialCaseTags(array $tags = array()) |
||
266 | |||
267 | /** |
||
268 | * @param string $tag |
||
269 | * |
||
270 | * @deprecated since 0.1.0 |
||
271 | */ |
||
272 | public function addSpecialCaseTag($tag) |
||
276 | |||
277 | /** |
||
278 | * @param string $tag |
||
279 | * |
||
280 | * @deprecated since 0.1.0 |
||
281 | */ |
||
282 | public function removeSpecialCaseTag($tag) |
||
286 | |||
287 | /** |
||
288 | * @return array|null |
||
289 | * |
||
290 | * @deprecated since 0.1.0 |
||
291 | */ |
||
292 | public function getSpecialCaseTags() |
||
296 | |||
297 | /** |
||
298 | * @return string |
||
299 | */ |
||
300 | public function getOldHtml() |
||
304 | |||
305 | /** |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getNewHtml() |
||
312 | |||
313 | /** |
||
314 | * @return string |
||
315 | */ |
||
316 | public function getDifference() |
||
320 | |||
321 | /** |
||
322 | * Clears the diff content. |
||
323 | * |
||
324 | * @return void |
||
325 | */ |
||
326 | public function clearContent() |
||
330 | |||
331 | /** |
||
332 | * @param bool $boolean |
||
333 | * |
||
334 | * @return $this |
||
335 | * |
||
336 | * @deprecated since 0.1.0 |
||
337 | */ |
||
338 | public function setGroupDiffs($boolean) |
||
344 | |||
345 | /** |
||
346 | * @return bool |
||
347 | * |
||
348 | * @deprecated since 0.1.0 |
||
349 | */ |
||
350 | public function isGroupDiffs() |
||
354 | |||
355 | /** |
||
356 | * @param \HTMLPurifier_Config $config |
||
357 | */ |
||
358 | 2 | public function setHTMLPurifierConfig(\HTMLPurifier_Config $config) |
|
362 | |||
363 | /** |
||
364 | * @param string $tag |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | protected function getOpeningTag($tag) |
||
372 | |||
373 | /** |
||
374 | * @param string $tag |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | protected function getClosingTag($tag) |
||
382 | |||
383 | /** |
||
384 | * @param string $str |
||
385 | * @param string $start |
||
386 | * @param string $end |
||
387 | * |
||
388 | * @return string |
||
389 | */ |
||
390 | protected function getStringBetween($str, $start, $end) |
||
404 | |||
405 | /** |
||
406 | * @param string $html |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | 14 | protected function purifyHtml($html) |
|
423 | |||
424 | 14 | protected function splitInputsToWords() |
|
429 | |||
430 | /** |
||
431 | * @param string $text |
||
432 | * |
||
433 | * @return bool |
||
434 | */ |
||
435 | 14 | protected function isPartOfWord($text) |
|
439 | |||
440 | /** |
||
441 | * @param array $characterString |
||
442 | * |
||
443 | * @return array |
||
444 | */ |
||
445 | 14 | protected function convertHtmlToListOfWords($characterString) |
|
521 | |||
522 | /** |
||
523 | * @param string $val |
||
524 | * |
||
525 | * @return bool |
||
526 | */ |
||
527 | 14 | protected function isStartOfTag($val) |
|
531 | |||
532 | /** |
||
533 | * @param string $val |
||
534 | * |
||
535 | * @return bool |
||
536 | */ |
||
537 | 14 | protected function isEndOfTag($val) |
|
541 | |||
542 | /** |
||
543 | * @param string $value |
||
544 | * |
||
545 | * @return bool |
||
546 | */ |
||
547 | protected function isWhiteSpace($value) |
||
551 | |||
552 | /** |
||
553 | * @param string $value |
||
554 | * |
||
555 | * @return array |
||
556 | */ |
||
557 | 14 | protected function explode($value) |
|
562 | } |
||
563 |
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.