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) |
|
24 | { |
||
25 | $res = new static(); |
||
26 | foreach ($this as $k => $v) { |
||
27 | $res[$k] = $callback($v); |
||
28 | } |
||
29 | |||
30 | return $res; |
||
31 | } |
||
32 | |||
33 | View Code Duplication | public function mapWithKey($callback) |
|
34 | { |
||
35 | $res = new static(); |
||
36 | foreach ($this as $k => $v) { |
||
37 | $res[$k] = $callback($k, $v); |
||
38 | } |
||
39 | |||
40 | return $res; |
||
41 | } |
||
42 | |||
43 | View Code Duplication | public function filter(callable $callback) |
|
44 | { |
||
45 | $res = new static(); |
||
46 | foreach ($this as $k => $v) { |
||
47 | if ($callback($v)) { |
||
48 | $res[$k] = $v; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | return $res; |
||
53 | } |
||
54 | |||
55 | View Code Duplication | public function filterWithKey($callback) |
|
56 | { |
||
57 | $res = new static(); |
||
58 | foreach ($this as $k => $v) { |
||
59 | if ($callback($k, $v)) { |
||
60 | $res[$k] = $v; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | return $res; |
||
65 | } |
||
66 | |||
67 | View Code Duplication | public function zip($iterable) |
|
68 | { |
||
69 | $res = new static(); |
||
70 | $it = $iterable->getIterator(); |
||
71 | foreach ($this as $k => $v) { |
||
72 | if (!$it->valid()) { |
||
73 | break; |
||
74 | } |
||
75 | $res[$k] = new Pair($v, $it->current()); |
||
76 | $it->next(); |
||
77 | } |
||
78 | |||
79 | return $res; |
||
80 | } |
||
81 | |||
82 | public function take($size = 1) |
||
97 | |||
98 | View Code Duplication | public function takeWhile($fn) |
|
99 | { |
||
100 | $res = new static(); |
||
101 | foreach ($this as $k => $v) { |
||
102 | if (!$fn($v)) { |
||
103 | break; |
||
104 | } |
||
105 | $res[$k] = $v; |
||
106 | } |
||
107 | |||
108 | return $res; |
||
109 | } |
||
110 | |||
111 | View Code Duplication | public function skip($n) |
|
112 | { |
||
113 | $res = new static(); |
||
114 | foreach ($this as $k => $v) { |
||
115 | if ($n <= 0) { |
||
116 | $res[$k] = $v; |
||
117 | } else { |
||
118 | --$n; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | return $res; |
||
123 | } |
||
124 | |||
125 | public function skipWhile($fn) |
||
141 | |||
142 | View Code Duplication | public function slice($start, $lenght) |
|
143 | { |
||
144 | $res = new static(); |
||
145 | if ($lenght <= 0) { |
||
146 | return $res; |
||
147 | } |
||
148 | foreach ($this as $k => $v) { |
||
161 | |||
162 | 1 | public function first() |
|
170 | 1 | ||
171 | 1 | public function firstKey() |
|
179 | |||
180 | public function last() |
||
188 | |||
189 | public function lastKey() |
||
197 | |||
198 | /** |
||
199 | * {@inheritDoc} |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function each(callable $callable) |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | public function exists(callable $fn) |
||
224 | |||
225 | View Code Duplication | public function concatAll() |
|
237 | |||
238 | View Code Duplication | private function concatRecurse($array, $array1) |
|
256 | } |
||
257 |