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 | 14 | } |
|
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) |
|
121 | { |
||
122 | 15 | if (null !== $this->purifierConfig) { |
|
123 | 2 | $HTMLPurifierConfig = $this->purifierConfig; |
|
124 | 2 | } else { |
|
125 | 15 | $HTMLPurifierConfig = \HTMLPurifier_Config::createDefault(); |
|
126 | } |
||
127 | |||
128 | // Cache.SerializerPath defaults to Null and sets |
||
129 | // the location to inside the vendor HTMLPurifier library |
||
130 | // under the DefinitionCache/Serializer folder. |
||
131 | 15 | if (!is_null($defaultPurifierSerializerCache)) { |
|
132 | 2 | $HTMLPurifierConfig->set('Cache.SerializerPath', $defaultPurifierSerializerCache); |
|
133 | 2 | } |
|
134 | |||
135 | 15 | $this->purifier = new \HTMLPurifier($HTMLPurifierConfig); |
|
136 | 15 | } |
|
137 | |||
138 | /** |
||
139 | * Prepare (purify) the HTML |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | 15 | protected function prepare() |
|
144 | { |
||
145 | 15 | $this->initPurifier($this->config->getPurifierCacheLocation()); |
|
146 | |||
147 | 15 | $this->oldText = $this->purifyHtml($this->oldText); |
|
148 | 15 | $this->newText = $this->purifyHtml($this->newText); |
|
149 | 15 | } |
|
150 | |||
151 | /** |
||
152 | * @return DiffCache|null |
||
153 | */ |
||
154 | 1 | protected function getDiffCache() |
|
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | */ |
||
172 | 15 | protected function hasDiffCache() |
|
176 | |||
177 | /** |
||
178 | * @return HtmlDiffConfig |
||
179 | */ |
||
180 | 15 | public function getConfig() |
|
184 | |||
185 | /** |
||
186 | * @param HtmlDiffConfig $config |
||
187 | * |
||
188 | * @return AbstractDiff |
||
189 | */ |
||
190 | 15 | public function setConfig(HtmlDiffConfig $config) |
|
196 | |||
197 | /** |
||
198 | * @return int |
||
199 | * |
||
200 | * @deprecated since 0.1.0 |
||
201 | */ |
||
202 | public function getMatchThreshold() |
||
206 | |||
207 | /** |
||
208 | * @param int $matchThreshold |
||
209 | * |
||
210 | * @return AbstractDiff |
||
211 | * |
||
212 | * @deprecated since 0.1.0 |
||
213 | */ |
||
214 | public function setMatchThreshold($matchThreshold) |
||
220 | |||
221 | /** |
||
222 | * @param array $chars |
||
223 | * |
||
224 | * @deprecated since 0.1.0 |
||
225 | */ |
||
226 | public function setSpecialCaseChars(array $chars) |
||
230 | |||
231 | /** |
||
232 | * @return array|null |
||
233 | * |
||
234 | * @deprecated since 0.1.0 |
||
235 | */ |
||
236 | public function getSpecialCaseChars() |
||
240 | |||
241 | /** |
||
242 | * @param string $char |
||
243 | * |
||
244 | * @deprecated since 0.1.0 |
||
245 | */ |
||
246 | public function addSpecialCaseChar($char) |
||
250 | |||
251 | /** |
||
252 | * @param string $char |
||
253 | * |
||
254 | * @deprecated since 0.1.0 |
||
255 | */ |
||
256 | public function removeSpecialCaseChar($char) |
||
260 | |||
261 | /** |
||
262 | * @param array $tags |
||
263 | * |
||
264 | * @deprecated since 0.1.0 |
||
265 | */ |
||
266 | public function setSpecialCaseTags(array $tags = array()) |
||
270 | |||
271 | /** |
||
272 | * @param string $tag |
||
273 | * |
||
274 | * @deprecated since 0.1.0 |
||
275 | */ |
||
276 | public function addSpecialCaseTag($tag) |
||
280 | |||
281 | /** |
||
282 | * @param string $tag |
||
283 | * |
||
284 | * @deprecated since 0.1.0 |
||
285 | */ |
||
286 | public function removeSpecialCaseTag($tag) |
||
290 | |||
291 | /** |
||
292 | * @return array|null |
||
293 | * |
||
294 | * @deprecated since 0.1.0 |
||
295 | */ |
||
296 | public function getSpecialCaseTags() |
||
300 | |||
301 | /** |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getOldHtml() |
||
308 | |||
309 | /** |
||
310 | * @return string |
||
311 | */ |
||
312 | public function getNewHtml() |
||
316 | |||
317 | /** |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getDifference() |
||
324 | |||
325 | /** |
||
326 | * Clears the diff content. |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | public function clearContent() |
||
334 | |||
335 | /** |
||
336 | * @param bool $boolean |
||
337 | * |
||
338 | * @return $this |
||
339 | * |
||
340 | * @deprecated since 0.1.0 |
||
341 | */ |
||
342 | public function setGroupDiffs($boolean) |
||
348 | |||
349 | /** |
||
350 | * @return bool |
||
351 | * |
||
352 | * @deprecated since 0.1.0 |
||
353 | */ |
||
354 | public function isGroupDiffs() |
||
358 | |||
359 | /** |
||
360 | * @param \HTMLPurifier_Config $config |
||
361 | */ |
||
362 | 2 | public function setHTMLPurifierConfig(\HTMLPurifier_Config $config) |
|
363 | { |
||
364 | 2 | $this->purifierConfig = $config; |
|
365 | 2 | } |
|
366 | |||
367 | /** |
||
368 | * @param string $tag |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | protected function getOpeningTag($tag) |
||
376 | |||
377 | /** |
||
378 | * @param string $tag |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | protected function getClosingTag($tag) |
||
386 | |||
387 | /** |
||
388 | * @param string $str |
||
389 | * @param string $start |
||
390 | * @param string $end |
||
391 | * |
||
392 | * @return string |
||
393 | */ |
||
394 | protected function getStringBetween($str, $start, $end) |
||
408 | |||
409 | /** |
||
410 | * @param string $html |
||
411 | * |
||
412 | * @return string |
||
413 | */ |
||
414 | 15 | protected function purifyHtml($html) |
|
415 | { |
||
416 | 15 | if (class_exists('Tidy') && false) { |
|
417 | $config = array('output-xhtml' => true, 'indent' => false); |
||
418 | $tidy = new tidy(); |
||
419 | $tidy->parseString($html, $config, 'utf8'); |
||
420 | $html = (string) $tidy; |
||
421 | |||
422 | return $this->getStringBetween($html, '<body>'); |
||
423 | } |
||
424 | |||
425 | 15 | return $this->purifier->purify($html); |
|
426 | } |
||
427 | |||
428 | 15 | protected function splitInputsToWords() |
|
429 | { |
||
430 | 15 | $this->setOldWords($this->convertHtmlToListOfWords($this->explode($this->oldText))); |
|
431 | 15 | $this->setNewWords($this->convertHtmlToListOfWords($this->explode($this->newText))); |
|
432 | 15 | } |
|
433 | |||
434 | /** |
||
435 | * @param array $oldWords |
||
436 | */ |
||
437 | 15 | protected function setOldWords(array $oldWords) |
|
438 | { |
||
439 | 15 | $this->resetCache = true; |
|
440 | 15 | $this->oldWords = $oldWords; |
|
441 | 15 | } |
|
442 | |||
443 | /** |
||
444 | * @param array $newWords |
||
445 | */ |
||
446 | 15 | protected function setNewWords(array $newWords) |
|
447 | { |
||
448 | 15 | $this->resetCache = true; |
|
449 | 15 | $this->newWords = $newWords; |
|
450 | 15 | } |
|
451 | |||
452 | /** |
||
453 | * @param string $text |
||
454 | * |
||
455 | * @return bool |
||
456 | */ |
||
457 | 15 | protected function isPartOfWord($text) |
|
461 | |||
462 | /** |
||
463 | * @param array $characterString |
||
464 | * |
||
465 | * @return array |
||
466 | */ |
||
467 | 15 | protected function convertHtmlToListOfWords($characterString) |
|
543 | |||
544 | /** |
||
545 | * @param string $val |
||
546 | * |
||
547 | * @return bool |
||
548 | */ |
||
549 | 15 | protected function isStartOfTag($val) |
|
553 | |||
554 | /** |
||
555 | * @param string $val |
||
556 | * |
||
557 | * @return bool |
||
558 | */ |
||
559 | 15 | protected function isEndOfTag($val) |
|
563 | |||
564 | /** |
||
565 | * @param string $value |
||
566 | * |
||
567 | * @return bool |
||
568 | */ |
||
569 | protected function isWhiteSpace($value) |
||
573 | |||
574 | /** |
||
575 | * @param string $value |
||
576 | * |
||
577 | * @return array |
||
578 | */ |
||
579 | 15 | protected function explode($value) |
|
584 | } |
||
585 |
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.