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 |
||
39 | abstract class AbstractCollection extends Object implements CollectionInterface, \IteratorAggregate |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * Holds the items of the ArrayList |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $items = array(); |
||
48 | |||
49 | /** |
||
50 | * Default constructor which makes sure that our properties are preset properly |
||
51 | */ |
||
52 | 19 | public function __construct() |
|
56 | |||
57 | /** |
||
58 | * This method returns a new Iter object |
||
59 | * needed by a foreach structure. |
||
60 | * |
||
61 | * @return \AppserverIo\Collections\Iter Holds the Iter object |
||
62 | * @see \AppserverIo\Collections\IteratorAggregate::getIterator() |
||
63 | */ |
||
64 | 3 | public function getIterator() |
|
68 | |||
69 | /** |
||
70 | * This method appends all elements of the |
||
71 | * passed array to the Collection. |
||
72 | * |
||
73 | * @param array $array Holds the array with the values to add |
||
74 | * |
||
75 | * @return \AppserverIo\Collections\CollectionInterface The instance |
||
76 | * @see \AppserverIo\Collections\Collection::addAll($array) |
||
77 | */ |
||
78 | 3 | public function addAll($array) |
|
86 | |||
87 | /** |
||
88 | * This method returns the element with the passed key |
||
89 | * from the Collection. |
||
90 | * |
||
91 | * @param mixed $key Holds the key of the element to return |
||
92 | * |
||
93 | * @return mixed The requested element |
||
94 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an integer |
||
95 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key OR value are NULL |
||
96 | * @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Collection |
||
97 | * @see \AppserverIo\Collections\CollectionInterface::get($key) |
||
98 | */ |
||
99 | 8 | View Code Duplication | public function get($key) |
137 | |||
138 | /** |
||
139 | * This method checks if an element with the passed |
||
140 | * key exists in the Collection. |
||
141 | * If yes the method |
||
142 | * returns TRUE, else FALSE. |
||
143 | * |
||
144 | * @param mixed $key Holds the key of the element to check for |
||
145 | * |
||
146 | * @return boolean Returns TRUE if the element exists in the Collection else FALSE |
||
147 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an integer |
||
148 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
||
149 | * @see \AppserverIo\Collections\CollectionInterface::exists($key) |
||
150 | */ |
||
151 | 1 | public function exists($key) |
|
181 | |||
182 | /** |
||
183 | * This method returns an array with the |
||
184 | * items of the Dictionary. The keys are |
||
185 | * lost in the array. |
||
186 | * |
||
187 | * @return array Holds an array with the items of the Dictionary |
||
188 | * @see \AppserverIo\Collections\CollectionInterface::toArray() |
||
189 | */ |
||
190 | 4 | public function toArray() |
|
194 | |||
195 | /** |
||
196 | * This method returns the number of entries of the Collection. |
||
197 | * |
||
198 | * @return integer The number of entries |
||
199 | * @see \AppserverIo\Collections\CollectionInterface::size() |
||
200 | */ |
||
201 | 11 | public function size() |
|
205 | |||
206 | /** |
||
207 | * This method initializes the Collection and removes |
||
208 | * all existing entries. |
||
209 | * |
||
210 | * @return void |
||
211 | * @see \AppserverIo\Collections\CollectionInterface::clear() |
||
212 | */ |
||
213 | 6 | public function clear() |
|
217 | |||
218 | /** |
||
219 | * This returns true if the Collection has no |
||
220 | * entries, otherwise false. |
||
221 | * |
||
222 | * @return boolean |
||
223 | * @see \AppserverIo\Collections\Collection::isEmpty() |
||
224 | */ |
||
225 | 3 | public function isEmpty() |
|
232 | |||
233 | /** |
||
234 | * This method removes the element with the passed key, that has to be an integer, from the IndexedCollection. |
||
235 | * |
||
236 | * @param mixed $key Holds the key of the element to remove |
||
237 | * |
||
238 | * @return void |
||
239 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an integer |
||
240 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
||
241 | * @throws \AppserverIo\Collections\IndexOutOfBoundsException |
||
242 | */ |
||
243 | 6 | View Code Duplication | public function remove($key) |
285 | |||
286 | /** |
||
287 | * This method merges the elements of the passed map |
||
288 | * with the elements of the actual map. |
||
289 | * |
||
290 | * If the keys are equal, the existing value is taken, else |
||
291 | * the new one is appended. |
||
292 | * |
||
293 | * @param \AppserverIo\Collections\CollectionInterface $col Holds the Collection with the values to merge |
||
294 | * |
||
295 | * @return void |
||
296 | * @deprecated Does not work correctly |
||
297 | */ |
||
298 | public function merge(CollectionInterface $col) |
||
303 | |||
304 | /** |
||
305 | * This method checks if an element with the passed |
||
306 | * value exists in the ArrayList. |
||
307 | * |
||
308 | * @param mixed $value Holds the value to check the elements of the array list for |
||
309 | * |
||
310 | * @return boolean Returns true if an element with the passed value exists in the array list |
||
311 | */ |
||
312 | public function contains($value) |
||
326 | } |
||
327 |
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.