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 ArrayCollection 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 ArrayCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class ArrayCollection implements Collection |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * An array containing the entries of this collection. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | private $elements; |
||
21 | |||
22 | /** |
||
23 | * Initializes a new ArrayCollection. |
||
24 | * |
||
25 | * @param array $elements |
||
26 | */ |
||
27 | public function __construct(array $elements = array()) |
||
31 | |||
32 | /** |
||
33 | * {@inheritDoc} |
||
34 | */ |
||
35 | public function toArray() |
||
39 | |||
40 | /** |
||
41 | * {@inheritDoc} |
||
42 | */ |
||
43 | public function first() |
||
47 | |||
48 | /** |
||
49 | * {@inheritDoc} |
||
50 | */ |
||
51 | public function last() |
||
55 | |||
56 | /** |
||
57 | * {@inheritDoc} |
||
58 | */ |
||
59 | public function key() |
||
63 | |||
64 | /** |
||
65 | * {@inheritDoc} |
||
66 | */ |
||
67 | public function next() |
||
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | public function current() |
||
79 | |||
80 | /** |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | public function remove($key) |
||
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | */ |
||
98 | public function removeElement($element) |
||
110 | |||
111 | /** |
||
112 | * Required by interface ArrayAccess. |
||
113 | * |
||
114 | * {@inheritDoc} |
||
115 | */ |
||
116 | public function offsetExists($offset) |
||
120 | |||
121 | /** |
||
122 | * Required by interface ArrayAccess. |
||
123 | * |
||
124 | * {@inheritDoc} |
||
125 | */ |
||
126 | public function offsetGet($offset) |
||
130 | |||
131 | /** |
||
132 | * Required by interface ArrayAccess. |
||
133 | * |
||
134 | * {@inheritDoc} |
||
135 | */ |
||
136 | public function offsetSet($offset, $value) |
||
146 | |||
147 | /** |
||
148 | * Required by interface ArrayAccess. |
||
149 | * |
||
150 | * {@inheritDoc} |
||
151 | */ |
||
152 | public function offsetUnset($offset) |
||
156 | |||
157 | /** |
||
158 | * {@inheritDoc} |
||
159 | */ |
||
160 | public function containsKey($key) |
||
164 | |||
165 | /** |
||
166 | * {@inheritDoc} |
||
167 | */ |
||
168 | public function contains($element) |
||
172 | |||
173 | /** |
||
174 | * {@inheritDoc} |
||
175 | */ |
||
176 | View Code Duplication | public function exists(Closure $p) |
|
186 | |||
187 | /** |
||
188 | * {@inheritDoc} |
||
189 | */ |
||
190 | public function indexOf($element) |
||
194 | |||
195 | /** |
||
196 | * {@inheritDoc} |
||
197 | */ |
||
198 | public function get($key) |
||
202 | |||
203 | /** |
||
204 | * {@inheritDoc} |
||
205 | */ |
||
206 | public function getKeys() |
||
210 | |||
211 | /** |
||
212 | * {@inheritDoc} |
||
213 | */ |
||
214 | public function getValues() |
||
218 | |||
219 | /** |
||
220 | * {@inheritDoc} |
||
221 | */ |
||
222 | public function count() |
||
226 | |||
227 | /** |
||
228 | * {@inheritDoc} |
||
229 | */ |
||
230 | public function set($key, $value) |
||
234 | |||
235 | /** |
||
236 | * {@inheritDoc} |
||
237 | */ |
||
238 | public function add($value) |
||
244 | |||
245 | /** |
||
246 | * {@inheritDoc} |
||
247 | */ |
||
248 | public function isEmpty() |
||
252 | |||
253 | /** |
||
254 | * Required by interface IteratorAggregate. |
||
255 | * |
||
256 | * {@inheritDoc} |
||
257 | */ |
||
258 | public function getIterator() |
||
262 | |||
263 | /** |
||
264 | * {@inheritDoc} |
||
265 | */ |
||
266 | public function map(Closure $func) |
||
270 | |||
271 | /** |
||
272 | * {@inheritDoc} |
||
273 | */ |
||
274 | public function filter(Closure $p) |
||
278 | |||
279 | /** |
||
280 | * {@inheritDoc} |
||
281 | */ |
||
282 | View Code Duplication | public function forAll(Closure $p) |
|
292 | |||
293 | /** |
||
294 | * {@inheritDoc} |
||
295 | */ |
||
296 | public function partition(Closure $p) |
||
313 | |||
314 | /** |
||
315 | * Returns a string representation of this object. |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | public function __toString() |
||
323 | |||
324 | /** |
||
325 | * {@inheritDoc} |
||
326 | */ |
||
327 | public function clear() |
||
331 | |||
332 | /** |
||
333 | * {@inheritDoc} |
||
334 | */ |
||
335 | public function slice($offset, $length = null) |
||
339 | } |
||
340 |
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.