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 LazyKeyedIterableTrait 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 LazyKeyedIterableTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | trait LazyKeyedIterableTrait |
||
| 15 | { |
||
| 16 | public function toArray() |
||
| 25 | |||
| 26 | public function toValuesArray() |
||
| 35 | |||
| 36 | public function toKeysArray() |
||
| 45 | |||
| 46 | public function toVector() |
||
| 50 | |||
| 51 | public function toImmVector() |
||
| 55 | |||
| 56 | public function toMap() |
||
| 60 | |||
| 61 | public function toImmMap() |
||
| 65 | |||
| 66 | public function toSet() |
||
| 70 | |||
| 71 | public function toImmSet() |
||
| 75 | |||
| 76 | public function lazy() |
||
| 80 | |||
| 81 | public function values() |
||
| 85 | |||
| 86 | public function keys() |
||
| 90 | |||
| 91 | public function map(callable $callback) |
||
| 95 | |||
| 96 | public function mapWithKey($callback) |
||
| 100 | |||
| 101 | public function filter(callable $callback) |
||
| 105 | |||
| 106 | public function filterWithKey($callback) |
||
| 110 | |||
| 111 | public function zip($traversable) |
||
| 119 | |||
| 120 | public function take($size = 1) |
||
| 124 | |||
| 125 | public function takeWhile($fn) |
||
| 129 | |||
| 130 | public function skip($n) |
||
| 134 | |||
| 135 | public function skipWhile($fn) |
||
| 139 | |||
| 140 | public function slice($start, $len) |
||
| 144 | |||
| 145 | public function concat($Enumerable) |
||
| 153 | |||
| 154 | public function first() |
||
| 162 | |||
| 163 | public function firstKey() |
||
| 171 | |||
| 172 | public function last() |
||
| 180 | |||
| 181 | public function lastKey() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritDoc} |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function each(callable $callable) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | public function exists(callable $fn) |
||
| 216 | |||
| 217 | View Code Duplication | public function concatAll() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritDoc} |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function groupBy($callback) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritDoc} |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | View Code Duplication | public function indexBy($callback) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | public function reduce(callable $callback, $initial = null) |
||
| 277 | } |
||
| 278 |