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 ListDiffNew 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 ListDiffNew, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ListDiffNew extends AbstractDiff |
||
9 | { |
||
10 | protected static $listTypes = array('ul', 'ol', 'dl'); |
||
11 | |||
12 | 4 | public function build() |
|
21 | |||
22 | 4 | protected function diffLists(DiffList $oldList, DiffList $newList) |
|
23 | { |
||
24 | 4 | $oldMatchData = array(); |
|
25 | 4 | $newMatchData = array(); |
|
26 | 4 | $oldListIndices = array(); |
|
27 | 4 | $newListIndices = array(); |
|
28 | 4 | $oldListItems = array(); |
|
29 | 4 | $newListItems = array(); |
|
30 | |||
31 | 4 | foreach ($oldList->getListItems() as $oldIndex => $oldListItem) { |
|
32 | 4 | if ($oldListItem instanceof DiffListItem) { |
|
33 | 4 | $oldListItems[$oldIndex] = $oldListItem; |
|
34 | |||
35 | 4 | $oldListIndices[] = $oldIndex; |
|
36 | 4 | $oldMatchData[$oldIndex] = array(); |
|
37 | |||
38 | // Get match percentages |
||
39 | 4 | foreach ($newList->getListItems() as $newIndex => $newListItem) { |
|
40 | 4 | if ($newListItem instanceof DiffListItem) { |
|
41 | 4 | if (!in_array($newListItem, $newListItems)) { |
|
42 | 4 | $newListItems[$newIndex] = $newListItem; |
|
43 | 4 | } |
|
44 | 4 | if (!in_array($newIndex, $newListIndices)) { |
|
45 | 4 | $newListIndices[] = $newIndex; |
|
46 | 4 | } |
|
47 | 4 | if (!array_key_exists($newIndex, $newMatchData)) { |
|
48 | 4 | $newMatchData[$newIndex] = array(); |
|
49 | 4 | } |
|
50 | |||
51 | 4 | $oldText = implode('', $oldListItem->getText()); |
|
52 | 4 | $newText = implode('', $newListItem->getText()); |
|
53 | |||
54 | // similar_text |
||
55 | 4 | $percentage = null; |
|
56 | 4 | similar_text($oldText, $newText, $percentage); |
|
57 | |||
58 | 4 | $oldMatchData[$oldIndex][$newIndex] = $percentage; |
|
59 | 4 | $newMatchData[$newIndex][$oldIndex] = $percentage; |
|
60 | 4 | } |
|
61 | 4 | } |
|
62 | 4 | } |
|
63 | 4 | } |
|
64 | |||
65 | 4 | $currentIndexInOld = 0; |
|
66 | 4 | $currentIndexInNew = 0; |
|
67 | 4 | $oldCount = count($oldListIndices); |
|
68 | 4 | $newCount = count($newListIndices); |
|
69 | 4 | $difference = max($oldCount, $newCount) - min($oldCount, $newCount); |
|
70 | |||
71 | 4 | $diffOutput = ''; |
|
72 | |||
73 | 4 | foreach ($newList->getListItems() as $newIndex => $newListItem) { |
|
74 | 4 | if ($newListItem instanceof DiffListItem) { |
|
75 | 4 | $operation = null; |
|
|
|||
76 | |||
77 | 4 | $oldListIndex = array_key_exists($currentIndexInOld, $oldListIndices) ? $oldListIndices[$currentIndexInOld] : null; |
|
78 | 4 | $class = 'normal'; |
|
79 | |||
80 | 4 | if (null !== $oldListIndex && array_key_exists($oldListIndex, $oldMatchData)) { |
|
81 | // Check percentage matches of upcoming list items in old. |
||
82 | 4 | $matchPercentage = $oldMatchData[$oldListIndex][$newIndex]; |
|
83 | |||
84 | // does the old list item match better? |
||
85 | 4 | $otherMatchBetter = false; |
|
86 | 4 | View Code Duplication | foreach ($oldMatchData[$oldListIndex] as $index => $percentage) { |
87 | 4 | if ($index > $newIndex && $percentage > $matchPercentage) { |
|
88 | 2 | $otherMatchBetter = $index; |
|
89 | 2 | } |
|
90 | 4 | } |
|
91 | |||
92 | 4 | if (false !== $otherMatchBetter && $newCount > $oldCount && $difference > 0) { |
|
93 | 1 | $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins')); |
|
94 | 1 | $currentIndexInNew++; |
|
95 | 1 | $difference--; |
|
96 | |||
97 | 1 | continue; |
|
98 | } |
||
99 | |||
100 | 4 | $nextOldListIndex = array_key_exists($currentIndexInOld + 1, $oldListIndices) ? $oldListIndices[$currentIndexInOld + 1] : null; |
|
101 | |||
102 | 4 | $replacement = false; |
|
103 | |||
104 | 4 | if ($nextOldListIndex !== null && $oldMatchData[$nextOldListIndex][$newIndex] > $matchPercentage && $oldMatchData[$nextOldListIndex][$newIndex] > $this->matchThreshold) { |
|
105 | // Following list item in old is better match, use that. |
||
106 | $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del')); |
||
107 | |||
108 | $currentIndexInOld++; |
||
109 | $oldListIndex = $nextOldListIndex; |
||
110 | $matchPercentage = $oldMatchData[$oldListIndex]; |
||
111 | $replacement = true; |
||
112 | } |
||
113 | |||
114 | 4 | if ($matchPercentage > $this->matchThreshold || $currentIndexInNew === $currentIndexInOld) { |
|
115 | // Diff the two lists. |
||
116 | 4 | $htmlDiff = new HtmlDiff($oldListItems[$oldListIndex]->getInnerHtml(), $newListItem->getInnerHtml(), $this->encoding, $this->specialCaseTags, $this->groupDiffs); |
|
117 | 4 | $diffContent = $htmlDiff->build(); |
|
118 | |||
119 | 4 | $diffOutput .= sprintf('%s%s%s', $newListItem->getStartTagWithDiffClass($replacement ? 'replacement' : 'normal'), $diffContent, $newListItem->getEndTag()); |
|
120 | |||
121 | 4 | } else { |
|
122 | $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del')); |
||
123 | $diffOutput .= sprintf('%s', $newListItem->getHtml('replacement', 'ins')); |
||
124 | } |
||
125 | 4 | $currentIndexInOld++; |
|
126 | 4 | } else { |
|
127 | $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins')); |
||
128 | } |
||
129 | |||
130 | 4 | $currentIndexInNew++; |
|
131 | 4 | } |
|
132 | 4 | } |
|
133 | |||
134 | // Output any additional list items |
||
135 | 4 | while (array_key_exists($currentIndexInOld, $oldListIndices)) { |
|
136 | 2 | $oldListIndex = $oldListIndices[$currentIndexInOld]; |
|
137 | 2 | $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del')); |
|
138 | 2 | $currentIndexInOld++; |
|
139 | 2 | } |
|
140 | |||
141 | 4 | return sprintf('%s%s%s', $newList->getStartTagWithDiffClass(), $diffOutput, $newList->getEndTag()); |
|
142 | } |
||
143 | |||
144 | 4 | protected function buildDiffList($words) |
|
219 | |||
220 | 4 | protected function isOpeningListTag($word, $type = null) |
|
225 | |||
226 | 4 | protected function isClosingListTag($word, $type = null) |
|
232 | |||
233 | 4 | protected function isOpeningListItemTag($word, $type = null) |
|
239 | |||
240 | 4 | protected function isClosingListItemTag($word, $type = null) |
|
246 | } |
||
247 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.