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 AbstractCollection 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 AbstractCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | abstract class AbstractCollection implements CollectionInterface |
||
38 | { |
||
39 | /** |
||
40 | * An array containing the entries of this collection. |
||
41 | * |
||
42 | * @var DeltaInterface[] |
||
43 | */ |
||
44 | private $elements; |
||
45 | |||
46 | /** |
||
47 | * Initializes a new AbstractCollection. |
||
48 | * |
||
49 | * @param DeltaInterface[] $elements |
||
50 | * |
||
51 | * @throws AlreadyExistsException |
||
52 | * @throws CollectionException |
||
53 | * @throws InvalidArgumentException |
||
54 | */ |
||
55 | 158 | public function __construct($elements = []) |
|
75 | |||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | 21 | final public function toArray() |
|
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | 26 | final public function first() |
|
91 | |||
92 | /** |
||
93 | * {@inheritDoc} |
||
94 | */ |
||
95 | 13 | final public function last() |
|
99 | |||
100 | /** |
||
101 | * {@inheritDoc} |
||
102 | */ |
||
103 | 1 | final public function key() |
|
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | */ |
||
111 | 1 | final public function next() |
|
115 | |||
116 | /** |
||
117 | * {@inheritDoc} |
||
118 | */ |
||
119 | 1 | final public function current() |
|
123 | |||
124 | /** |
||
125 | * {@inheritDoc} |
||
126 | */ |
||
127 | 4 | public function remove($key) |
|
139 | |||
140 | /** |
||
141 | * {@inheritDoc} |
||
142 | */ |
||
143 | 144 | final public function contains($key) |
|
148 | |||
149 | /** |
||
150 | * {@inheritDoc} |
||
151 | */ |
||
152 | 1 | final public function containsVersion(DeltaInterface $version) |
|
157 | |||
158 | /** |
||
159 | * {@inheritDoc} |
||
160 | */ |
||
161 | 2 | View Code Duplication | final public function exists(Closure $p) |
171 | |||
172 | /** |
||
173 | * {@inheritDoc} |
||
174 | */ |
||
175 | 59 | final public function get($key) |
|
180 | |||
181 | /** |
||
182 | * {@inheritDoc} |
||
183 | */ |
||
184 | 34 | final public function getKeys() |
|
188 | |||
189 | /** |
||
190 | * {@inheritDoc} |
||
191 | */ |
||
192 | 1 | final public function getValues() |
|
196 | |||
197 | /** |
||
198 | * {@inheritDoc} |
||
199 | */ |
||
200 | 53 | final public function count() |
|
204 | |||
205 | /** |
||
206 | * {@inheritDoc} |
||
207 | */ |
||
208 | 10 | public function replace(DeltaInterface $version) |
|
216 | |||
217 | /** |
||
218 | * {@inheritDoc} |
||
219 | */ |
||
220 | 143 | public function add(DeltaInterface $value) |
|
232 | |||
233 | /** |
||
234 | * {@inheritDoc} |
||
235 | */ |
||
236 | 3 | final public function isEmpty() |
|
240 | |||
241 | /** |
||
242 | * Required by interface IteratorAggregate. |
||
243 | * |
||
244 | * {@inheritDoc} |
||
245 | */ |
||
246 | 54 | final public function getIterator() |
|
250 | |||
251 | /** |
||
252 | * {@inheritDoc} |
||
253 | */ |
||
254 | 34 | final public function map(Closure $func) |
|
258 | |||
259 | /** |
||
260 | * {@inheritDoc} |
||
261 | */ |
||
262 | 1 | final public function filter(Closure $p) |
|
266 | |||
267 | /** |
||
268 | * {@inheritDoc} |
||
269 | */ |
||
270 | 4 | View Code Duplication | final public function forAll(Closure $p) |
280 | |||
281 | /** |
||
282 | * {@inheritDoc} |
||
283 | */ |
||
284 | 14 | final public function partition(Closure $p) |
|
298 | |||
299 | /** |
||
300 | * Returns a string representation of this object. |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | 1 | public function __toString() |
|
308 | |||
309 | /** |
||
310 | * {@inheritDoc} |
||
311 | */ |
||
312 | 8 | public function clear() |
|
316 | |||
317 | /** |
||
318 | * @inheritDoc |
||
319 | */ |
||
320 | 2 | final public function slice($offset, $length = null) |
|
324 | |||
325 | /** |
||
326 | * @inheritdoc |
||
327 | */ |
||
328 | 10 | final public function merge(CollectionInterface $collection) |
|
336 | |||
337 | /** |
||
338 | * @inheritdoc |
||
339 | */ |
||
340 | 33 | final public function getPosition($key) { |
|
343 | |||
344 | /** |
||
345 | * @inheritdoc |
||
346 | */ |
||
347 | 34 | final public function getByPosition($position) |
|
356 | } |
||
357 |
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.