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 StrictKeyedIterableTrait 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 StrictKeyedIterableTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | trait StrictKeyedIterableTrait |
||
| 10 | { |
||
| 11 | use CommonMutableContainerTrait; |
||
| 12 | |||
| 13 | public function keys() |
||
| 22 | |||
| 23 | View Code Duplication | public function map(callable $callback) |
|
| 32 | |||
| 33 | View Code Duplication | public function mapWithKey($callback) |
|
| 42 | |||
| 43 | View Code Duplication | public function filter(callable $callback) |
|
| 54 | |||
| 55 | View Code Duplication | public function filterWithKey($callback) |
|
| 66 | |||
| 67 | public function take($size = 1) |
||
| 82 | |||
| 83 | View Code Duplication | public function takeWhile($fn) |
|
| 95 | |||
| 96 | View Code Duplication | public function skip($n) |
|
| 109 | |||
| 110 | public function skipWhile($fn) |
||
| 126 | |||
| 127 | View Code Duplication | public function slice($start, $lenght) |
|
| 146 | |||
| 147 | public function first() |
||
| 155 | |||
| 156 | public function firstKey() |
||
| 164 | 1 | ||
| 165 | public function last() |
||
| 173 | 1 | ||
| 174 | public function lastKey() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritDoc} |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | public function each(callable $callable) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function exists(callable $fn) |
||
| 209 | |||
| 210 | View Code Duplication | public function concatAll() |
|
| 222 | |||
| 223 | View Code Duplication | protected function concatRecurse($array, $array1) |
|
| 241 | } |
||
| 242 |