Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like HtmlDiff 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 HtmlDiff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class HtmlDiff extends AbstractDiff |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $wordIndices; |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $oldTables; |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $newTables; |
||
25 | /** |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $insertSpaceInReplace = false; |
||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $newIsolatedDiffTags; |
||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $oldIsolatedDiffTags; |
||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $isolatedDiffTags = array ( |
||
41 | 'ol' => '[[REPLACE_ORDERED_LIST]]', |
||
42 | 'ul' => '[[REPLACE_UNORDERED_LIST]]', |
||
43 | 'sub' => '[[REPLACE_SUB_SCRIPT]]', |
||
44 | 'sup' => '[[REPLACE_SUPER_SCRIPT]]', |
||
45 | 'dl' => '[[REPLACE_DEFINITION_LIST]]', |
||
46 | 'table' => '[[REPLACE_TABLE]]', |
||
47 | 'strong' => '[[REPLACE_STRONG]]', |
||
48 | 'b' => '[[REPLACE_B]]', |
||
49 | 'em' => '[[REPLACE_EM]]', |
||
50 | 'i' => '[[REPLACE_I]]', |
||
51 | 'a' => '[[REPLACE_A]]', |
||
52 | ); |
||
53 | /** |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $useTableDiffing = true; |
||
57 | |||
58 | /** |
||
59 | * @param $bool |
||
60 | * |
||
61 | * @return $this |
||
62 | */ |
||
63 | public function setUseTableDiffing($bool) |
||
69 | |||
70 | /** |
||
71 | * @param boolean $boolean |
||
72 | * @return HtmlDiff |
||
73 | */ |
||
74 | public function setInsertSpaceInReplace($boolean) |
||
80 | |||
81 | /** |
||
82 | * @return boolean |
||
83 | */ |
||
84 | public function getInsertSpaceInReplace() |
||
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | 11 | public function build() |
|
105 | |||
106 | 11 | protected function indexNewWords() |
|
120 | |||
121 | 11 | protected function replaceIsolatedDiffTags() |
|
126 | |||
127 | /** |
||
128 | * @param array $words |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 11 | protected function createIsolatedDiffTagPlaceholders(&$words) |
|
166 | |||
167 | /** |
||
168 | * @param string $item |
||
169 | * @param null|string $currentIsolatedDiffTag |
||
170 | * |
||
171 | * @return false|string |
||
172 | */ |
||
173 | 11 | View Code Duplication | protected function isOpeningIsolatedDiffTag($item, $currentIsolatedDiffTag = null) |
186 | |||
187 | /** |
||
188 | * @param string $item |
||
189 | * @param null|string $currentIsolatedDiffTag |
||
190 | * |
||
191 | * @return false|string |
||
192 | */ |
||
193 | 11 | View Code Duplication | protected function isClosingIsolatedDiffTag($item, $currentIsolatedDiffTag = null) |
206 | |||
207 | /** |
||
208 | * @param Operation $operation |
||
209 | */ |
||
210 | 11 | protected function performOperation($operation) |
|
229 | |||
230 | /** |
||
231 | * @param Operation $operation |
||
232 | */ |
||
233 | 7 | protected function processReplaceOperation($operation) |
|
238 | |||
239 | /** |
||
240 | * @param Operation $operation |
||
241 | * @param string $cssClass |
||
242 | */ |
||
243 | 9 | View Code Duplication | protected function processInsertOperation($operation, $cssClass) |
259 | |||
260 | /** |
||
261 | * @param Operation $operation |
||
262 | * @param string $cssClass |
||
263 | */ |
||
264 | 9 | View Code Duplication | protected function processDeleteOperation($operation, $cssClass) |
280 | |||
281 | /** |
||
282 | * @param Operation $operation |
||
283 | * @param int $pos |
||
284 | * @param string $placeholder |
||
285 | * @param bool $stripWrappingTags |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | 7 | protected function diffIsolatedPlaceholder($operation, $pos, $placeholder, $stripWrappingTags = true) |
|
304 | |||
305 | /** |
||
306 | * @param string $oldText |
||
307 | * @param string $newText |
||
308 | * @param bool $stripWrappingTags |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | 5 | protected function diffElements($oldText, $newText, $stripWrappingTags = true) |
|
333 | |||
334 | /** |
||
335 | * @param string $oldText |
||
336 | * @param string $newText |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | 4 | View Code Duplication | protected function diffList($oldText, $newText) |
347 | |||
348 | /** |
||
349 | * @param string $oldText |
||
350 | * @param string $newText |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | View Code Duplication | protected function diffTables($oldText, $newText) |
|
361 | |||
362 | /** |
||
363 | * @param string $oldText |
||
364 | * @param string $newText |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | 1 | protected function diffLinks($oldText, $newText) |
|
383 | |||
384 | /** |
||
385 | * @param Operation $operation |
||
386 | */ |
||
387 | 11 | View Code Duplication | protected function processEqualOperation($operation) |
403 | |||
404 | /** |
||
405 | * @param string $text |
||
406 | * @param string $attribute |
||
407 | * |
||
408 | * @return null|string |
||
409 | */ |
||
410 | 1 | protected function getAttributeFromTag($text, $attribute) |
|
419 | |||
420 | /** |
||
421 | * @param string $text |
||
422 | * |
||
423 | * @return bool |
||
424 | */ |
||
425 | 7 | protected function isListPlaceholder($text) |
|
429 | |||
430 | /** |
||
431 | * @param string $text |
||
432 | * |
||
433 | * @return bool |
||
434 | */ |
||
435 | 5 | public function isLinkPlaceholder($text) |
|
439 | |||
440 | /** |
||
441 | * @param string $text |
||
442 | * @param array|string $types |
||
443 | * @param bool $strict |
||
444 | * |
||
445 | * @return bool |
||
446 | */ |
||
447 | 7 | protected function isPlaceholderType($text, $types, $strict = true) |
|
464 | |||
465 | /** |
||
466 | * @param string $text |
||
467 | * |
||
468 | * @return bool |
||
469 | */ |
||
470 | 5 | protected function isTablePlaceholder($text) |
|
476 | |||
477 | /** |
||
478 | * @param Operation $operation |
||
479 | * @param int $posInNew |
||
480 | * |
||
481 | * @return array |
||
482 | */ |
||
483 | 7 | protected function findIsolatedDiffTagsInOld($operation, $posInNew) |
|
489 | |||
490 | /** |
||
491 | * @param string $tag |
||
492 | * @param string $cssClass |
||
493 | * @param array $words |
||
494 | */ |
||
495 | 9 | protected function insertTag($tag, $cssClass, &$words) |
|
550 | |||
551 | /** |
||
552 | * @param string $word |
||
553 | * @param string $condition |
||
554 | * |
||
555 | * @return bool |
||
556 | */ |
||
557 | 9 | protected function checkCondition($word, $condition) |
|
561 | |||
562 | /** |
||
563 | * @param string $text |
||
564 | * @param string $tagName |
||
565 | * @param string $cssClass |
||
566 | * |
||
567 | * @return string |
||
568 | */ |
||
569 | 10 | protected function wrapText($text, $tagName, $cssClass) |
|
573 | |||
574 | /** |
||
575 | * @param array $words |
||
576 | * @param string $condition |
||
577 | * |
||
578 | * @return array |
||
579 | */ |
||
580 | 9 | protected function extractConsecutiveWords(&$words, $condition) |
|
614 | |||
615 | /** |
||
616 | * @param string $item |
||
617 | * |
||
618 | * @return bool |
||
619 | */ |
||
620 | 11 | protected function isTag($item) |
|
624 | |||
625 | /** |
||
626 | * @param string $item |
||
627 | * |
||
628 | * @return bool |
||
629 | */ |
||
630 | 11 | protected function isOpeningTag($item) |
|
634 | |||
635 | /** |
||
636 | * @param string $item |
||
637 | * |
||
638 | * @return bool |
||
639 | */ |
||
640 | 11 | protected function isClosingTag($item) |
|
644 | |||
645 | /** |
||
646 | * @return Operation[] |
||
647 | */ |
||
648 | 11 | protected function operations() |
|
681 | |||
682 | /** |
||
683 | * @return Match[] |
||
684 | */ |
||
685 | 11 | protected function matchingBlocks() |
|
692 | |||
693 | /** |
||
694 | * @param int $startInOld |
||
695 | * @param int $endInOld |
||
696 | * @param int $startInNew |
||
697 | * @param int $endInNew |
||
698 | * @param array $matchingBlocks |
||
699 | */ |
||
700 | 11 | protected function findMatchingBlocks($startInOld, $endInOld, $startInNew, $endInNew, &$matchingBlocks) |
|
713 | |||
714 | /** |
||
715 | * @param string $word |
||
716 | * |
||
717 | * @return string |
||
718 | */ |
||
719 | 8 | protected function stripTagAttributes($word) |
|
725 | |||
726 | /** |
||
727 | * @param int $startInOld |
||
728 | * @param int $endInOld |
||
729 | * @param int $startInNew |
||
730 | * @param int $endInNew |
||
731 | * |
||
732 | * @return Match|null |
||
733 | */ |
||
734 | 11 | protected function findMatch($startInOld, $endInOld, $startInNew, $endInNew) |
|
789 | } |
||
790 |
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.