Complex classes like ForeachMethodCollection 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 ForeachMethodCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ForeachMethodCollection extends Collection |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | public function map(callable $callback) |
||
26 | { |
||
27 | $collection = Factory::create(); |
||
28 | $index = 0; |
||
29 | foreach ($this->items as $key => $value) { |
||
30 | $collection->set($key, $callback($value, $key, $index++)); |
||
31 | } |
||
32 | |||
33 | return $collection; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function filter(callable $callback) |
||
40 | { |
||
41 | $collection = Factory::create(); |
||
42 | $index = 0; |
||
43 | foreach ($this->items as $key => $value) { |
||
44 | if ($callback($value, $key, $index++)) { |
||
45 | $collection->set($key, $value); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | return $collection; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function reduce(callable $callback, $initial = null) |
||
56 | { |
||
57 | $accumulator = $initial; |
||
58 | $index = 0; |
||
59 | foreach ($this->items as $key => $value) { |
||
60 | $accumulator = $callback($accumulator, $value, $key, $index++); |
||
61 | } |
||
62 | |||
63 | return $accumulator; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function combine($values, $inPlace = false) |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function each(callable $callback) |
||
97 | { |
||
98 | $index = 0; |
||
99 | foreach ($this->items as $key => $value) { |
||
100 | $callback($value, $key, $index++); |
||
101 | } |
||
102 | |||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function flip() |
||
110 | { |
||
111 | $collection = Factory::create([], static::class); |
||
112 | foreach ($this->items as $key => $value) { |
||
113 | $collection->set($value, $key); |
||
114 | } |
||
115 | |||
116 | return $collection; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function values() |
||
123 | { |
||
124 | $collection = Factory::create([], static::class); |
||
125 | foreach ($this->items as $value) { |
||
126 | $collection->add($value); |
||
127 | } |
||
128 | |||
129 | return $collection; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function keys() |
||
136 | { |
||
137 | $collection = Factory::create([], static::class); |
||
138 | foreach ($this->items as $key => $value) { |
||
139 | $collection->add($key); |
||
140 | } |
||
141 | |||
142 | return $collection; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function unique() |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function contains($value) |
||
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | public function merge($items, $inPlace = false) |
||
180 | { |
||
181 | if (!is_array($items) && !($items instanceof Traversable)) { |
||
182 | $this->doThrow('Invalid input type for '.__METHOD__.', cannot merge.', $items); |
||
183 | } |
||
184 | $collection = $inPlace ? $this : clone $this; |
||
185 | foreach ($items as $key => $value) { |
||
186 | $collection->set($key, $value); |
||
187 | } |
||
188 | |||
189 | return $collection; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | public function union($items, $inPlace = false) |
||
196 | { |
||
197 | if (!is_array($items) && !($items instanceof Traversable)) { |
||
198 | $this->doThrow('Invalid input type for '.__METHOD__.', cannot union.', $items); |
||
199 | } |
||
200 | $collection = $inPlace ? $this : clone $this; |
||
201 | foreach ($items as $key => $value) { |
||
202 | if (!$collection->containsKey($key)) { |
||
203 | $collection->set($key, $value); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | return $collection; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function reverse() |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function shift() |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | public function pop() |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | public function chunk($size) |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | public function slice($offset, $length = PHP_INT_MAX) |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function diff($items) |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function diffKeys($items) |
||
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | public function intersect($items) |
||
325 | |||
326 | /** |
||
327 | * {@inheritdoc} |
||
328 | */ |
||
329 | public function intersectKeys($items) |
||
339 | } |
||
340 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.