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 | /** |
||
13 | * @param string $oldText |
||
14 | * @param string $newText |
||
15 | * @param HtmlDiffConfig|null $config |
||
16 | * |
||
17 | * @return self |
||
18 | */ |
||
19 | 7 | View Code Duplication | public static function create($oldText, $newText, HtmlDiffConfig $config = null) |
29 | |||
30 | 7 | View Code Duplication | public function build() |
51 | |||
52 | 7 | protected function diffLists(DiffList $oldList, DiffList $newList) |
|
53 | { |
||
54 | 7 | $oldMatchData = array(); |
|
55 | 7 | $newMatchData = array(); |
|
56 | 7 | $oldListIndices = array(); |
|
57 | 7 | $newListIndices = array(); |
|
58 | 7 | $oldListItems = array(); |
|
59 | 7 | $newListItems = array(); |
|
60 | |||
61 | 7 | foreach ($oldList->getListItems() as $oldIndex => $oldListItem) { |
|
62 | 7 | if ($oldListItem instanceof DiffListItem) { |
|
63 | 7 | $oldListItems[$oldIndex] = $oldListItem; |
|
64 | |||
65 | 7 | $oldListIndices[] = $oldIndex; |
|
66 | 7 | $oldMatchData[$oldIndex] = array(); |
|
67 | |||
68 | // Get match percentages |
||
69 | 7 | foreach ($newList->getListItems() as $newIndex => $newListItem) { |
|
70 | 7 | if ($newListItem instanceof DiffListItem) { |
|
71 | 7 | if (!in_array($newListItem, $newListItems)) { |
|
72 | 7 | $newListItems[$newIndex] = $newListItem; |
|
73 | 7 | } |
|
74 | 7 | if (!in_array($newIndex, $newListIndices)) { |
|
75 | 7 | $newListIndices[] = $newIndex; |
|
76 | 7 | } |
|
77 | 7 | if (!array_key_exists($newIndex, $newMatchData)) { |
|
78 | 7 | $newMatchData[$newIndex] = array(); |
|
79 | 7 | } |
|
80 | |||
81 | 7 | $oldText = implode('', $oldListItem->getText()); |
|
82 | 7 | $newText = implode('', $newListItem->getText()); |
|
83 | |||
84 | // similar_text |
||
85 | 7 | $percentage = null; |
|
86 | 7 | similar_text($oldText, $newText, $percentage); |
|
87 | |||
88 | 7 | $oldMatchData[$oldIndex][$newIndex] = $percentage; |
|
89 | 7 | $newMatchData[$newIndex][$oldIndex] = $percentage; |
|
90 | 7 | } |
|
91 | 7 | } |
|
92 | 7 | } |
|
93 | 7 | } |
|
94 | |||
95 | 7 | $currentIndexInOld = 0; |
|
96 | 7 | $currentIndexInNew = 0; |
|
97 | 7 | $oldCount = count($oldListIndices); |
|
98 | 7 | $newCount = count($newListIndices); |
|
99 | 7 | $difference = max($oldCount, $newCount) - min($oldCount, $newCount); |
|
100 | |||
101 | 7 | $diffOutput = ''; |
|
102 | |||
103 | 7 | foreach ($newList->getListItems() as $newIndex => $newListItem) { |
|
104 | 7 | if ($newListItem instanceof DiffListItem) { |
|
105 | 7 | $operation = null; |
|
106 | |||
107 | 7 | $oldListIndex = array_key_exists($currentIndexInOld, $oldListIndices) ? $oldListIndices[$currentIndexInOld] : null; |
|
108 | 7 | $class = 'normal'; |
|
109 | |||
110 | 7 | if (null !== $oldListIndex && array_key_exists($oldListIndex, $oldMatchData)) { |
|
111 | // Check percentage matches of upcoming list items in old. |
||
112 | 7 | $matchPercentage = $oldMatchData[$oldListIndex][$newIndex]; |
|
113 | |||
114 | // does the old list item match better? |
||
115 | 7 | $otherMatchBetter = false; |
|
116 | 7 | foreach ($oldMatchData[$oldListIndex] as $index => $percentage) { |
|
117 | 7 | if ($index > $newIndex && $percentage > $matchPercentage) { |
|
118 | 4 | $otherMatchBetter = $index; |
|
119 | 4 | } |
|
120 | 7 | } |
|
121 | |||
122 | 7 | if (false !== $otherMatchBetter && $newCount > $oldCount && $difference > 0) { |
|
123 | 2 | $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins')); |
|
124 | 2 | $currentIndexInNew++; |
|
125 | 2 | $difference--; |
|
126 | |||
127 | 2 | continue; |
|
128 | } |
||
129 | |||
130 | 7 | $nextOldListIndex = array_key_exists($currentIndexInOld + 1, $oldListIndices) ? $oldListIndices[$currentIndexInOld + 1] : null; |
|
131 | |||
132 | 7 | $replacement = false; |
|
133 | |||
134 | 7 | if ($nextOldListIndex !== null && $oldMatchData[$nextOldListIndex][$newIndex] > $matchPercentage && $oldMatchData[$nextOldListIndex][$newIndex] > $this->config->getMatchThreshold()) { |
|
135 | // Following list item in old is better match, use that. |
||
136 | 1 | $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del')); |
|
137 | |||
138 | 1 | $currentIndexInOld++; |
|
139 | 1 | $oldListIndex = $nextOldListIndex; |
|
140 | 1 | $matchPercentage = $oldMatchData[$oldListIndex]; |
|
141 | 1 | $replacement = true; |
|
142 | 1 | } |
|
143 | |||
144 | 7 | if ($matchPercentage > $this->config->getMatchThreshold() || $currentIndexInNew === $currentIndexInOld) { |
|
145 | // Diff the two lists. |
||
146 | 7 | $htmlDiff = HtmlDiff::create( |
|
147 | 7 | $oldListItems[$oldListIndex]->getInnerHtml(), |
|
148 | 7 | $newListItem->getInnerHtml(), |
|
149 | 7 | $this->config |
|
150 | 7 | ); |
|
151 | 7 | $diffContent = $htmlDiff->build(); |
|
152 | |||
153 | 7 | $diffOutput .= sprintf('%s%s%s', $newListItem->getStartTagWithDiffClass($replacement ? 'replacement' : 'normal'), $diffContent, $newListItem->getEndTag()); |
|
154 | |||
155 | 7 | } else { |
|
156 | $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del')); |
||
157 | $diffOutput .= sprintf('%s', $newListItem->getHtml('replacement', 'ins')); |
||
158 | } |
||
159 | 7 | $currentIndexInOld++; |
|
160 | 7 | } else { |
|
161 | $diffOutput .= sprintf('%s', $newListItem->getHtml('normal new', 'ins')); |
||
162 | } |
||
163 | |||
164 | 7 | $currentIndexInNew++; |
|
165 | 7 | } |
|
166 | 7 | } |
|
167 | |||
168 | // Output any additional list items |
||
169 | 7 | while (array_key_exists($currentIndexInOld, $oldListIndices)) { |
|
170 | 2 | $oldListIndex = $oldListIndices[$currentIndexInOld]; |
|
171 | 2 | $diffOutput .= sprintf('%s', $oldListItems[$oldListIndex]->getHtml('removed', 'del')); |
|
172 | 2 | $currentIndexInOld++; |
|
173 | 2 | } |
|
174 | |||
175 | 7 | return sprintf('%s%s%s', $newList->getStartTagWithDiffClass(), $diffOutput, $newList->getEndTag()); |
|
176 | } |
||
177 | |||
178 | 7 | protected function buildDiffList($words) |
|
253 | |||
254 | 7 | protected function isOpeningListTag($word, $type = null) |
|
259 | |||
260 | 7 | protected function isClosingListTag($word, $type = null) |
|
266 | |||
267 | 7 | protected function isOpeningListItemTag($word, $type = null) |
|
273 | |||
274 | 7 | protected function isClosingListItemTag($word, $type = null) |
|
280 | } |
||
281 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.