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 | * @see array_slice_cached(); |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected $resetCache = false; |
||
81 | |||
82 | /** |
||
83 | * AbstractDiff constructor. |
||
84 | * |
||
85 | * @param string $oldText |
||
86 | * @param string $newText |
||
87 | * @param string $encoding |
||
88 | * @param null|array $specialCaseTags |
||
89 | * @param null|bool $groupDiffs |
||
90 | */ |
||
91 | 15 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
|
92 | { |
||
93 | 15 | mb_substitute_character(0x20); |
|
94 | |||
95 | 15 | $this->setConfig(HtmlDiffConfig::create()->setEncoding($encoding)); |
|
96 | |||
97 | 15 | if ($specialCaseTags !== null) { |
|
98 | 14 | $this->config->setSpecialCaseTags($specialCaseTags); |
|
99 | } |
||
100 | |||
101 | 15 | if ($groupDiffs !== null) { |
|
102 | $this->config->setGroupDiffs($groupDiffs); |
||
103 | } |
||
104 | |||
105 | 15 | $this->oldText = $oldText; |
|
106 | 15 | $this->newText = $newText; |
|
107 | 15 | $this->content = ''; |
|
108 | 15 | } |
|
109 | |||
110 | /** |
||
111 | * @return bool|string |
||
112 | */ |
||
113 | abstract public function build(); |
||
114 | |||
115 | /** |
||
116 | * Initializes HTMLPurifier with cache location. |
||
117 | * |
||
118 | * @param null|string $defaultPurifierSerializerCache |
||
119 | */ |
||
120 | 15 | public function initPurifier($defaultPurifierSerializerCache = null) |
|
142 | |||
143 | /** |
||
144 | * Prepare (purify) the HTML |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | 15 | protected function prepare() |
|
155 | |||
156 | /** |
||
157 | * @return DiffCache|null |
||
158 | */ |
||
159 | protected function getDiffCache() |
||
173 | |||
174 | /** |
||
175 | * @return bool |
||
176 | */ |
||
177 | 15 | protected function hasDiffCache() |
|
181 | |||
182 | /** |
||
183 | * @return HtmlDiffConfig |
||
184 | */ |
||
185 | 15 | public function getConfig() |
|
189 | |||
190 | /** |
||
191 | * @param HtmlDiffConfig $config |
||
192 | * |
||
193 | * @return AbstractDiff |
||
194 | */ |
||
195 | 15 | public function setConfig(HtmlDiffConfig $config) |
|
201 | |||
202 | /** |
||
203 | * @return int |
||
204 | * |
||
205 | * @deprecated since 0.1.0 |
||
206 | */ |
||
207 | public function getMatchThreshold() |
||
211 | |||
212 | /** |
||
213 | * @param int $matchThreshold |
||
214 | * |
||
215 | * @return AbstractDiff |
||
216 | * |
||
217 | * @deprecated since 0.1.0 |
||
218 | */ |
||
219 | public function setMatchThreshold($matchThreshold) |
||
225 | |||
226 | /** |
||
227 | * @param array $chars |
||
228 | * |
||
229 | * @deprecated since 0.1.0 |
||
230 | */ |
||
231 | public function setSpecialCaseChars(array $chars) |
||
235 | |||
236 | /** |
||
237 | * @return array|null |
||
238 | * |
||
239 | * @deprecated since 0.1.0 |
||
240 | */ |
||
241 | public function getSpecialCaseChars() |
||
245 | |||
246 | /** |
||
247 | * @param string $char |
||
248 | * |
||
249 | * @deprecated since 0.1.0 |
||
250 | */ |
||
251 | public function addSpecialCaseChar($char) |
||
255 | |||
256 | /** |
||
257 | * @param string $char |
||
258 | * |
||
259 | * @deprecated since 0.1.0 |
||
260 | */ |
||
261 | public function removeSpecialCaseChar($char) |
||
265 | |||
266 | /** |
||
267 | * @param array $tags |
||
268 | * |
||
269 | * @deprecated since 0.1.0 |
||
270 | */ |
||
271 | public function setSpecialCaseTags(array $tags = array()) |
||
275 | |||
276 | /** |
||
277 | * @param string $tag |
||
278 | * |
||
279 | * @deprecated since 0.1.0 |
||
280 | */ |
||
281 | public function addSpecialCaseTag($tag) |
||
285 | |||
286 | /** |
||
287 | * @param string $tag |
||
288 | * |
||
289 | * @deprecated since 0.1.0 |
||
290 | */ |
||
291 | public function removeSpecialCaseTag($tag) |
||
295 | |||
296 | /** |
||
297 | * @return array|null |
||
298 | * |
||
299 | * @deprecated since 0.1.0 |
||
300 | */ |
||
301 | public function getSpecialCaseTags() |
||
305 | |||
306 | /** |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getOldHtml() |
||
313 | |||
314 | /** |
||
315 | * @return string |
||
316 | */ |
||
317 | public function getNewHtml() |
||
321 | |||
322 | /** |
||
323 | * @return string |
||
324 | */ |
||
325 | public function getDifference() |
||
329 | |||
330 | /** |
||
331 | * Clears the diff content. |
||
332 | * |
||
333 | * @return void |
||
334 | */ |
||
335 | public function clearContent() |
||
339 | |||
340 | /** |
||
341 | * @param bool $boolean |
||
342 | * |
||
343 | * @return $this |
||
344 | * |
||
345 | * @deprecated since 0.1.0 |
||
346 | */ |
||
347 | public function setGroupDiffs($boolean) |
||
353 | |||
354 | /** |
||
355 | * @return bool |
||
356 | * |
||
357 | * @deprecated since 0.1.0 |
||
358 | */ |
||
359 | 15 | public function isGroupDiffs() |
|
363 | |||
364 | /** |
||
365 | * @param \HTMLPurifier_Config $config |
||
366 | */ |
||
367 | 2 | public function setHTMLPurifierConfig(\HTMLPurifier_Config $config) |
|
371 | |||
372 | /** |
||
373 | * @param string $tag |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | protected function getOpeningTag($tag) |
||
381 | |||
382 | /** |
||
383 | * @param string $tag |
||
384 | * |
||
385 | * @return string |
||
386 | */ |
||
387 | protected function getClosingTag($tag) |
||
391 | |||
392 | /** |
||
393 | * @param string $str |
||
394 | * @param string $start |
||
395 | * @param string $end |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | protected function getStringBetween($str, $start, $end) |
||
413 | |||
414 | /** |
||
415 | * @param string $html |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | 15 | protected function purifyHtml($html) |
|
420 | { |
||
421 | 15 | if (class_exists('Tidy') && false) { |
|
422 | $config = array('output-xhtml' => true, 'indent' => false); |
||
423 | $tidy = new tidy(); |
||
424 | $tidy->parseString($html, $config, 'utf8'); |
||
425 | $html = (string) $tidy; |
||
426 | |||
427 | return $this->getStringBetween($html, '<body>'); |
||
428 | } |
||
429 | |||
430 | 15 | return $this->purifier->purify($html); |
|
431 | } |
||
432 | |||
433 | 15 | protected function splitInputsToWords() |
|
438 | |||
439 | /** |
||
440 | * @param array $oldWords |
||
441 | */ |
||
442 | 15 | protected function setOldWords(array $oldWords) |
|
447 | |||
448 | /** |
||
449 | * @param array $newWords |
||
450 | */ |
||
451 | 15 | protected function setNewWords(array $newWords) |
|
456 | |||
457 | /** |
||
458 | * @param string $text |
||
459 | * |
||
460 | * @return bool |
||
461 | */ |
||
462 | 15 | protected function isPartOfWord($text) |
|
466 | |||
467 | /** |
||
468 | * @param array $characterString |
||
469 | * |
||
470 | * @return array |
||
471 | */ |
||
472 | 15 | protected function convertHtmlToListOfWords($characterString) |
|
549 | |||
550 | /** |
||
551 | * @param string $val |
||
552 | * |
||
553 | * @return bool |
||
554 | */ |
||
555 | 15 | protected function isStartOfTag($val) |
|
559 | |||
560 | /** |
||
561 | * @param string $val |
||
562 | * |
||
563 | * @return bool |
||
564 | */ |
||
565 | 15 | protected function isEndOfTag($val) |
|
569 | |||
570 | /** |
||
571 | * @param string $value |
||
572 | * |
||
573 | * @return bool |
||
574 | */ |
||
575 | protected function isWhiteSpace($value) |
||
579 | |||
580 | /** |
||
581 | * @param string $value |
||
582 | * |
||
583 | * @return array |
||
584 | */ |
||
585 | 15 | protected function explode($value) |
|
590 | } |
||
591 |
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.