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) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Allow a method to be run on the entire collection. |
||
| 54 | * |
||
| 55 | * @param string $method |
||
| 56 | * @param array $args |
||
| 57 | * |
||
| 58 | * @return Collection |
||
| 59 | */ |
||
| 60 | 24 | public function __call($method, $args) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Insert into an object |
||
| 85 | * |
||
| 86 | * Should be able to do this with methods |
||
| 87 | * that already exist on collection. |
||
| 88 | * |
||
| 89 | * @param mixed $value |
||
| 90 | * @param int $afterKey |
||
| 91 | * |
||
| 92 | * @return Collection |
||
| 93 | */ |
||
| 94 | public function insertAfter($value, $afterKey) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Turn a collection into a drop down for an html select element. |
||
| 113 | * |
||
| 114 | * @param string $firstOptionText Text for the first object in the select array. |
||
| 115 | * @param string $id The column to use for the id column in the option element. |
||
| 116 | * @param string $name The column to use for the name column in the option element. |
||
| 117 | * |
||
| 118 | * @return array The new select element array. |
||
| 119 | */ |
||
| 120 | public function toSelectArray($firstOptionText = 'Select one', $id = 'id', $name = 'name') |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Turn the magic getWhere into a real where query. |
||
| 137 | * |
||
| 138 | * @param $method |
||
| 139 | * @param $args |
||
| 140 | * |
||
| 141 | * @return Collection |
||
| 142 | */ |
||
| 143 | 24 | private function magicWhere($method, $args) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param $whereStatement |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | 22 | private function determineMagicWhereDetails($whereStatement) |
|
| 212 | |||
| 213 | 22 | private function checkMagicWhereFinalOperator($operator, $finalOperator) |
|
| 221 | |||
| 222 | 22 | private function checkMagicWherePosition($operator, $position) |
|
| 230 | |||
| 231 | 22 | private function checkMagicWhereNot($operator, $not) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Search a collection for the value specified. |
||
| 242 | * |
||
| 243 | * @param string $column The column to search. |
||
| 244 | * @param string $operator The operation to use during search. |
||
| 245 | * @param mixed $value The value to search for. |
||
| 246 | * @param boolean $inverse Invert the results. |
||
| 247 | * @param string $position Return the first or last object in the collection. |
||
| 248 | * |
||
| 249 | * @return self Return the filtered collection. |
||
| 250 | */ |
||
| 251 | 24 | protected function getWhere($column, $operator, $value = null, $inverse = false, $position = null) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param $item |
||
| 278 | * @param $column |
||
| 279 | * @param $value |
||
| 280 | * @param $operator |
||
| 281 | * @param $inverse |
||
| 282 | * |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | 12 | private function handleMultiTap($item, $column, $value, $operator, $inverse) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param $column |
||
| 302 | * @param $item |
||
| 303 | * |
||
| 304 | * @return mixed |
||
| 305 | */ |
||
| 306 | 12 | private function tapThroughObjects($column, $item) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Compare the object and column passed with the value using the operator |
||
| 323 | * |
||
| 324 | * @param object $object The object we are searching. |
||
| 325 | * @param string $column The column to compare. |
||
| 326 | * @param string $operator What type of comparison operation to perform. |
||
| 327 | * @param mixed $value The value to search for. |
||
| 328 | * @param boolean $inverse Invert the results. |
||
| 329 | * |
||
| 330 | * @return boolean Return true if the object should be removed from the collection. |
||
| 331 | */ |
||
| 332 | 24 | private function whereObject($object, $column, $operator, $value = null, $inverse = false) |
|
| 348 | |||
| 349 | 8 | View Code Duplication | private function getWhereIn($object, $column, $value, $inverse) |
| 360 | |||
| 361 | 4 | private function getWhereBetween($object, $column, $value, $inverse) |
|
| 375 | |||
| 376 | 4 | View Code Duplication | private function getWhereLike($object, $column, $value, $inverse) |
| 387 | |||
| 388 | 4 | private function getWhereNull($object, $column, $value, $inverse) |
|
| 399 | |||
| 400 | 4 | private function getWhereDefault($object, $column, $value, $inverse) |
|
| 411 | } |
||
| 412 |