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 |
||
20 | class Collection extends \SplObjectStorage implements CollectionInterface { |
||
21 | |||
22 | |||
23 | /** |
||
24 | * an interface (or class) name to be used for restricting the type of objects added to the storage |
||
25 | * this should be set from within the child class constructor |
||
26 | * |
||
27 | * @type string $interface |
||
28 | */ |
||
29 | protected $collection_interface; |
||
30 | |||
31 | |||
32 | |||
33 | /** |
||
34 | * Collection constructor |
||
35 | * |
||
36 | * @param string $collection_interface |
||
37 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
38 | */ |
||
39 | public function __construct( $collection_interface ) { |
||
42 | |||
43 | |||
44 | |||
45 | /** |
||
46 | * setCollectionInterface |
||
47 | * |
||
48 | * @access protected |
||
49 | * @param string $collection_interface |
||
50 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
51 | */ |
||
52 | protected function setCollectionInterface( $collection_interface ) { |
||
58 | |||
59 | |||
60 | |||
61 | /** |
||
62 | * add |
||
63 | * attaches an object to the Collection |
||
64 | * and sets any supplied data associated with the current iterator entry |
||
65 | * by calling EE_Object_Collection::set_identifier() |
||
66 | * |
||
67 | * @access public |
||
68 | * @param $object |
||
69 | * @param mixed $identifier |
||
70 | * @return bool |
||
71 | * @throws \EventEspresso\core\exceptions\InvalidEntityException |
||
72 | */ |
||
73 | public function add( $object, $identifier = null ) { |
||
81 | |||
82 | |||
83 | |||
84 | /** |
||
85 | * setIdentifier |
||
86 | |||
87 | * Sets the data associated with an object in the Collection |
||
88 | * if no $identifier is supplied, then the spl_object_hash() is used |
||
89 | * |
||
90 | * @access public |
||
91 | * @param $object |
||
92 | * @param mixed $identifier |
||
93 | * @return bool |
||
94 | */ |
||
95 | View Code Duplication | public function setIdentifier( $object, $identifier = null ) { |
|
108 | |||
109 | |||
110 | |||
111 | /** |
||
112 | * get |
||
113 | * finds and returns an object in the Collection based on the identifier that was set using addObject() |
||
114 | * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
||
115 | * |
||
116 | * @access public |
||
117 | * @param mixed $identifier |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function get( $identifier ) { |
||
121 | $this->rewind(); |
||
122 | while ( $this->valid() ) { |
||
123 | if ( $identifier === $this->getInfo() ) { |
||
124 | $object = $this->current(); |
||
125 | $this->rewind(); |
||
126 | return $object; |
||
127 | } |
||
128 | $this->next(); |
||
129 | } |
||
130 | return null; |
||
131 | } |
||
132 | |||
133 | |||
134 | |||
135 | /** |
||
136 | * has |
||
137 | * returns TRUE or FALSE |
||
138 | * depending on whether the object is within the Collection |
||
139 | * based on the supplied $identifier |
||
140 | * |
||
141 | * @access public |
||
142 | * @param mixed $identifier |
||
143 | * @return bool |
||
144 | */ |
||
145 | View Code Duplication | public function has( $identifier ) { |
|
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * hasObject |
||
161 | * returns TRUE or FALSE depending on whether the supplied object is within the Collection |
||
162 | * |
||
163 | * @access public |
||
164 | * @param $object |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function hasObject( $object ) { |
||
170 | |||
171 | |||
172 | |||
173 | /** |
||
174 | * remove |
||
175 | * detaches an object from the Collection |
||
176 | * |
||
177 | * @access public |
||
178 | * @param $object |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function remove( $object ) { |
||
185 | |||
186 | |||
187 | |||
188 | /** |
||
189 | * setCurrent |
||
190 | * advances pointer to the object whose identifier matches that which was provided |
||
191 | * |
||
192 | * @access public |
||
193 | * @param mixed $identifier |
||
194 | * @return boolean |
||
195 | */ |
||
196 | View Code Duplication | public function setCurrent( $identifier ) { |
|
206 | |||
207 | |||
208 | |||
209 | /** |
||
210 | * setCurrentUsingObject |
||
211 | * advances pointer to the provided object |
||
212 | * |
||
213 | * @access public |
||
214 | * @param $object |
||
215 | * @return boolean |
||
216 | */ |
||
217 | public function setCurrentUsingObject( $object ) { |
||
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * Returns the object occupying the index before the current object, |
||
232 | * unless this is already the first object, in which case it just returns the first object |
||
233 | * |
||
234 | * @return mixed |
||
235 | */ |
||
236 | public function previous() { |
||
244 | |||
245 | |||
246 | |||
247 | /** |
||
248 | * Returns the index of a given object, or false if not found |
||
249 | * |
||
250 | * @see http://stackoverflow.com/a/8736013 |
||
251 | * @param $object |
||
252 | * @return boolean|int|string |
||
253 | */ |
||
254 | public function indexOf( $object ) { |
||
255 | if ( ! $this->contains( $object ) ) { |
||
256 | return false; |
||
257 | } |
||
258 | foreach ( $this as $index => $obj ) { |
||
259 | if ( $obj === $object ) { |
||
260 | return $index; |
||
261 | } |
||
262 | } |
||
263 | return false; |
||
264 | } |
||
265 | |||
266 | |||
267 | |||
268 | /** |
||
269 | * Returns the object at the given index |
||
270 | * |
||
271 | * @see http://stackoverflow.com/a/8736013 |
||
272 | * @param int $index |
||
273 | * @return mixed |
||
274 | */ |
||
275 | public function objectAtIndex( $index ) { |
||
280 | |||
281 | |||
282 | |||
283 | /** |
||
284 | * Returns the sequence of objects as specified by the offset and length |
||
285 | * |
||
286 | * @see http://stackoverflow.com/a/8736013 |
||
287 | * @param int $offset |
||
288 | * @param int $length |
||
289 | * @return array |
||
290 | */ |
||
291 | public function slice( $offset, $length ) { |
||
299 | |||
300 | |||
301 | |||
302 | /** |
||
303 | * Inserts an object (or an array of objects) at a certain point |
||
304 | * |
||
305 | * @see http://stackoverflow.com/a/8736013 |
||
306 | * @param mixed $objects A single object or an array of objects |
||
307 | * @param int $index |
||
308 | */ |
||
309 | public function insertAt( $objects, $index ) { |
||
340 | |||
341 | |||
342 | |||
343 | /** |
||
344 | * Removes the object at the given index |
||
345 | * |
||
346 | * @see http://stackoverflow.com/a/8736013 |
||
347 | * @param int $index |
||
348 | */ |
||
349 | public function removeAt( $index ) { |
||
352 | |||
353 | |||
354 | |||
355 | } |
||
356 | // End of file Collection.php |
||
357 | // Location: /Collection.php |
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.