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 Collection 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 Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Collection extends SplObjectStorage implements CollectionInterface |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * a unique string for identifying this collection |
||
24 | * |
||
25 | * @type string $collection_identifier |
||
26 | */ |
||
27 | protected $collection_identifier; |
||
28 | |||
29 | /** |
||
30 | * an interface (or class) name to be used for restricting the type of objects added to the storage |
||
31 | * this should be set from within the child class constructor |
||
32 | * |
||
33 | * @type string $interface |
||
34 | */ |
||
35 | protected $collection_interface; |
||
36 | |||
37 | |||
38 | /** |
||
39 | * Collection constructor |
||
40 | * |
||
41 | * @param string $collection_interface |
||
42 | * @throws InvalidInterfaceException |
||
43 | */ |
||
44 | public function __construct($collection_interface) |
||
49 | |||
50 | |||
51 | /** |
||
52 | * @return string |
||
53 | */ |
||
54 | public function collectionIdentifier() |
||
58 | |||
59 | |||
60 | /** |
||
61 | * creates a very readable unique 9 character identifier like: CF2-532-DAC |
||
62 | * and appends it to the non-qualified class name, ex: ThingCollection-CF2-532-DAC |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | protected function setCollectionIdentifier() |
||
79 | |||
80 | |||
81 | /** |
||
82 | * setCollectionInterface |
||
83 | * |
||
84 | * @access protected |
||
85 | * @param string $collection_interface |
||
86 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
87 | */ |
||
88 | View Code Duplication | protected function setCollectionInterface($collection_interface) |
|
95 | |||
96 | |||
97 | /** |
||
98 | * add |
||
99 | * attaches an object to the Collection |
||
100 | * and sets any supplied data associated with the current iterator entry |
||
101 | * by calling EE_Object_Collection::set_identifier() |
||
102 | * |
||
103 | * @access public |
||
104 | * @param $object |
||
105 | * @param mixed $identifier |
||
106 | * @return bool |
||
107 | * @throws InvalidEntityException |
||
108 | * @throws DuplicateCollectionIdentifierException |
||
109 | */ |
||
110 | public function add($object, $identifier = null) |
||
122 | |||
123 | |||
124 | /** |
||
125 | * setIdentifier |
||
126 | * Sets the data associated with an object in the Collection |
||
127 | * if no $identifier is supplied, then the spl_object_hash() is used |
||
128 | * |
||
129 | * @access public |
||
130 | * @param $object |
||
131 | * @param mixed $identifier |
||
132 | * @return bool |
||
133 | */ |
||
134 | View Code Duplication | public function setIdentifier($object, $identifier = null) |
|
150 | |||
151 | |||
152 | /** |
||
153 | * get |
||
154 | * finds and returns an object in the Collection based on the identifier that was set using addObject() |
||
155 | * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
||
156 | * |
||
157 | * @access public |
||
158 | * @param mixed $identifier |
||
159 | * @return mixed |
||
160 | */ |
||
161 | View Code Duplication | public function get($identifier) |
|
174 | |||
175 | |||
176 | /** |
||
177 | * has |
||
178 | * returns TRUE or FALSE |
||
179 | * depending on whether the object is within the Collection |
||
180 | * based on the supplied $identifier |
||
181 | * |
||
182 | * @access public |
||
183 | * @param mixed $identifier |
||
184 | * @return bool |
||
185 | */ |
||
186 | View Code Duplication | public function has($identifier) |
|
198 | |||
199 | |||
200 | /** |
||
201 | * hasObject |
||
202 | * returns TRUE or FALSE depending on whether the supplied object is within the Collection |
||
203 | * |
||
204 | * @access public |
||
205 | * @param $object |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function hasObject($object) |
||
212 | |||
213 | |||
214 | /** |
||
215 | * hasObjects |
||
216 | * returns true if there are objects within the Collection, and false if it is empty |
||
217 | * |
||
218 | * @access public |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function hasObjects() |
||
225 | |||
226 | |||
227 | /** |
||
228 | * isEmpty |
||
229 | * returns true if there are no objects within the Collection, and false if there are |
||
230 | * |
||
231 | * @access public |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function isEmpty() |
||
238 | |||
239 | |||
240 | /** |
||
241 | * remove |
||
242 | * detaches an object from the Collection |
||
243 | * |
||
244 | * @access public |
||
245 | * @param $object |
||
246 | * @return bool |
||
247 | */ |
||
248 | public function remove($object) |
||
253 | |||
254 | |||
255 | /** |
||
256 | * setCurrent |
||
257 | * advances pointer to the object whose identifier matches that which was provided |
||
258 | * |
||
259 | * @access public |
||
260 | * @param mixed $identifier |
||
261 | * @return boolean |
||
262 | */ |
||
263 | View Code Duplication | public function setCurrent($identifier) |
|
274 | |||
275 | |||
276 | /** |
||
277 | * setCurrentUsingObject |
||
278 | * advances pointer to the provided object |
||
279 | * |
||
280 | * @access public |
||
281 | * @param $object |
||
282 | * @return boolean |
||
283 | */ |
||
284 | public function setCurrentUsingObject($object) |
||
295 | |||
296 | |||
297 | /** |
||
298 | * Returns the object occupying the index before the current object, |
||
299 | * unless this is already the first object, in which case it just returns the first object |
||
300 | * |
||
301 | * @return mixed |
||
302 | */ |
||
303 | public function previous() |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Returns the index of a given object, or false if not found |
||
316 | * |
||
317 | * @see http://stackoverflow.com/a/8736013 |
||
318 | * @param $object |
||
319 | * @return boolean|int|string |
||
320 | */ |
||
321 | public function indexOf($object) |
||
333 | |||
334 | |||
335 | /** |
||
336 | * Returns the object at the given index |
||
337 | * |
||
338 | * @see http://stackoverflow.com/a/8736013 |
||
339 | * @param int $index |
||
340 | * @return mixed |
||
341 | */ |
||
342 | public function objectAtIndex($index) |
||
348 | |||
349 | |||
350 | /** |
||
351 | * Returns the sequence of objects as specified by the offset and length |
||
352 | * |
||
353 | * @see http://stackoverflow.com/a/8736013 |
||
354 | * @param int $offset |
||
355 | * @param int $length |
||
356 | * @return array |
||
357 | */ |
||
358 | public function slice($offset, $length) |
||
367 | |||
368 | |||
369 | /** |
||
370 | * Inserts an object at a certain point |
||
371 | * |
||
372 | * @see http://stackoverflow.com/a/8736013 |
||
373 | * @param mixed $object A single object |
||
374 | * @param int $index |
||
375 | * @param mixed $identifier |
||
376 | * @return bool |
||
377 | * @throws DuplicateCollectionIdentifierException |
||
378 | * @throws InvalidEntityException |
||
379 | */ |
||
380 | public function insertObjectAt($object, $index, $identifier = null) |
||
407 | |||
408 | |||
409 | /** |
||
410 | * Inserts an object (or an array of objects) at a certain point |
||
411 | * |
||
412 | * @see http://stackoverflow.com/a/8736013 |
||
413 | * @param mixed $objects A single object or an array of objects |
||
414 | * @param int $index |
||
415 | */ |
||
416 | public function insertAt($objects, $index) |
||
448 | |||
449 | |||
450 | /** |
||
451 | * Removes the object at the given index |
||
452 | * |
||
453 | * @see http://stackoverflow.com/a/8736013 |
||
454 | * @param int $index |
||
455 | */ |
||
456 | public function removeAt($index) |
||
460 | |||
461 | |||
462 | /** |
||
463 | * detaches ALL objects from the Collection |
||
464 | */ |
||
465 | public function detachAll() |
||
474 | |||
475 | |||
476 | /** |
||
477 | * unsets and detaches ALL objects from the Collection |
||
478 | */ |
||
479 | public function trashAndDetachAll() |
||
489 | } |
||
490 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.