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 StrictIterableTrait 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 StrictIterableTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | trait StrictIterableTrait |
||
| 9 | { |
||
| 10 | use CommonMutableContainerTrait; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * {@inheritDoc} |
||
| 14 | * @return $this |
||
| 15 | */ |
||
| 16 | 1 | public function map(callable $callable) |
|
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritDoc} |
||
| 28 | * @return $this |
||
| 29 | */ |
||
| 30 | View Code Duplication | public function mapWithKey($callback) |
|
| 31 | { |
||
| 32 | $res = new static(); |
||
| 33 | foreach ($this as $k => $v) { |
||
| 34 | $res[$k] = $callback($k, $v); |
||
| 35 | } |
||
| 36 | |||
| 37 | return $res; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritDoc} |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | 4 | View Code Duplication | public function filter(callable $callable) |
| 45 | { |
||
| 46 | 2 | $res = new static(); |
|
| 47 | 2 | foreach ($this as $v) { |
|
| 48 | 2 | if ($callable($v)) { |
|
| 49 | 2 | $res[] = $v; |
|
| 50 | 2 | } |
|
| 51 | 2 | } |
|
| 52 | |||
| 53 | 4 | return $res; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritDoc} |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function filterWithKey($callback) |
|
| 61 | { |
||
| 62 | $res = new static(); |
||
| 63 | foreach ($this as $k => $v) { |
||
| 64 | if ($callback($k, $v)) { |
||
| 65 | $res[$k] = $v; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | return $res; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritDoc} |
||
| 74 | * @return $this |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function zip(Iterable $iterable) |
|
| 77 | { |
||
| 78 | $res = new static(); |
||
| 79 | $it = $iterable->getIterator(); |
||
| 80 | foreach ($this as $v) { |
||
| 81 | if (!$it->valid()) { |
||
| 82 | break; |
||
| 83 | } |
||
| 84 | $res[] = new Pair($v, $it->current()); |
||
| 85 | $it->next(); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $res; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritDoc} |
||
| 93 | * @return $this |
||
| 94 | */ |
||
| 95 | 1 | public function take($size = 1) |
|
| 112 | |||
| 113 | View Code Duplication | public function takeWhile(callable $callable) |
|
| 114 | { |
||
| 115 | $res = new static(); |
||
| 116 | foreach ($this as $v) { |
||
| 117 | if (!$callable($v)) { |
||
| 118 | break; |
||
| 119 | } |
||
| 120 | $res[] = $v; |
||
| 121 | } |
||
| 122 | |||
| 123 | return $res; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function skip($n) |
||
| 139 | |||
| 140 | public function skipWhile(callable $callable) |
||
| 156 | |||
| 157 | View Code Duplication | public function slice($start, $length) |
|
| 158 | { |
||
| 159 | $res = new static(); |
||
| 160 | if ($length <= 0) { |
||
| 161 | return $res; |
||
| 176 | |||
| 177 | 1 | /** |
|
| 178 | * {@inheritDoc} |
||
| 179 | 1 | * @return $this |
|
| 180 | */ |
||
| 181 | 1 | public function first() |
|
| 189 | |||
| 190 | 1 | /** |
|
| 191 | * {@inheritDoc} |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function last() |
||
| 200 | 1 | ||
| 201 | 1 | /** |
|
| 202 | * {@inheritDoc} |
||
| 203 | 1 | * @return $this |
|
| 204 | */ |
||
| 205 | public function each(callable $callable) |
||
| 213 | |||
| 214 | 2 | /** |
|
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | public function exists(callable $fn) |
||
| 227 | |||
| 228 | View Code Duplication | public function concatAll() |
|
| 240 | |||
| 241 | 1 | /** |
|
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | public function reduce(callable $callback, $initial = null) |
||
| 252 | |||
| 253 | View Code Duplication | private function concatRecurse($array, $array1) |
|
| 273 | } |
||
| 274 |