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 |
||
16 | class Collection extends BaseCollection |
||
17 | { |
||
18 | /** |
||
19 | * Dynamically retrieve attributes on the model. |
||
20 | * |
||
21 | * @param string $key |
||
22 | * |
||
23 | * @return mixed |
||
24 | */ |
||
25 | 1 | public function __get($key) |
|
45 | |||
46 | /** |
||
47 | * Allow a method to be run on the entire collection. |
||
48 | * |
||
49 | * @param string $method |
||
50 | * @param array $args |
||
51 | * |
||
52 | * @return Collection |
||
53 | */ |
||
54 | 24 | public function __call($method, $args) |
|
76 | |||
77 | /** |
||
78 | * Insert into an object |
||
79 | * |
||
80 | * Should be able to do this with methods |
||
81 | * that already exist on collection. |
||
82 | * |
||
83 | * @param mixed $value |
||
84 | * @param int $afterKey |
||
85 | * |
||
86 | * @return Collection |
||
87 | */ |
||
88 | public function insertAfter($value, $afterKey) |
||
104 | |||
105 | /** |
||
106 | * Turn a collection into a drop down for an html select element. |
||
107 | * |
||
108 | * @param string $firstOptionText Text for the first object in the select array. |
||
109 | * @param string $id The column to use for the id column in the option element. |
||
110 | * @param string $name The column to use for the name column in the option element. |
||
111 | * |
||
112 | * @return array The new select element array. |
||
113 | */ |
||
114 | public function toSelectArray($firstOptionText = 'Select one', $id = 'id', $name = 'name') |
||
128 | |||
129 | /** |
||
130 | * Turn the magic getWhere into a real where query. |
||
131 | * |
||
132 | * @param $method |
||
133 | * @param $args |
||
134 | * |
||
135 | * @return Collection |
||
136 | */ |
||
137 | 24 | private function magicWhere($method, $args) |
|
169 | |||
170 | /** |
||
171 | * @param $whereStatement |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | 22 | private function determineMagicWhereDetails($whereStatement) |
|
206 | |||
207 | 22 | private function checkMagicWhereFinalOperator($operator, $finalOperator) |
|
215 | |||
216 | 22 | private function checkMagicWherePosition($operator, $position) |
|
224 | |||
225 | 22 | private function checkMagicWhereNot($operator, $not) |
|
233 | |||
234 | /** |
||
235 | * Search a collection for the value specified. |
||
236 | * |
||
237 | * @param string $column The column to search. |
||
238 | * @param string $operator The operation to use during search. |
||
239 | * @param mixed $value The value to search for. |
||
240 | * @param boolean $inverse Invert the results. |
||
241 | * @param string $position Return the first or last object in the collection. |
||
242 | * |
||
243 | * @return self Return the filtered collection. |
||
244 | */ |
||
245 | 24 | protected function getWhere($column, $operator, $value = null, $inverse = false, $position = null) |
|
269 | |||
270 | /** |
||
271 | * @param $item |
||
272 | * @param $column |
||
273 | * @param $value |
||
274 | * @param $operator |
||
275 | * @param $inverse |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | 12 | private function handleMultiTap($item, $column, $value, $operator, $inverse) |
|
293 | |||
294 | /** |
||
295 | * @param $column |
||
296 | * @param $item |
||
297 | * |
||
298 | * @return mixed |
||
299 | */ |
||
300 | 12 | private function tapThroughObjects($column, $item) |
|
314 | |||
315 | /** |
||
316 | * Compare the object and column passed with the value using the operator |
||
317 | * |
||
318 | * @param object $object The object we are searching. |
||
319 | * @param string $column The column to compare. |
||
320 | * @param string $operator What type of comparison operation to perform. |
||
321 | * @param mixed $value The value to search for. |
||
322 | * @param boolean $inverse Invert the results. |
||
323 | * |
||
324 | * @return boolean Return true if the object should be removed from the collection. |
||
325 | */ |
||
326 | 24 | private function whereObject($object, $column, $operator, $value = null, $inverse = false) |
|
342 | |||
343 | 8 | View Code Duplication | private function getWhereIn($object, $column, $value, $inverse) |
354 | |||
355 | 4 | private function getWhereBetween($object, $column, $value, $inverse) |
|
369 | |||
370 | 4 | View Code Duplication | private function getWhereLike($object, $column, $value, $inverse) |
381 | |||
382 | 4 | private function getWhereNull($object, $column, $value, $inverse) |
|
393 | |||
394 | 4 | private function getWhereDefault($object, $column, $value, $inverse) |
|
405 | } |
||
406 |