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) |
||
70 | { |
||
71 | if (!is_array($values) && !($values instanceof Traversable)) { |
||
72 | $this->doThrow('Invalid input type for '.($inPlace ? 'replace' : 'combine').'.', $values); |
||
73 | } |
||
74 | |||
75 | if (count($values) != count($this->items)) { |
||
76 | $this->doThrow( |
||
77 | 'Invalid input for '.($inPlace ? 'replace' : 'combine').', number of items does not match.', |
||
78 | $values |
||
79 | ); |
||
80 | } |
||
81 | $values = Factory::getArrayForItems($values); |
||
82 | $collection = Factory::create([], static::class); |
||
83 | $this->rewind(); |
||
84 | foreach ($values as $value) { |
||
85 | $collection->set($this->current(), $value); |
||
86 | $this->next(); |
||
87 | } |
||
88 | $this->rewind(); |
||
89 | |||
90 | return $collection; |
||
91 | } |
||
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() |
||
149 | { |
||
150 | $collection = Factory::create([], static::class); |
||
151 | foreach ($this->items as $key => $value) { |
||
152 | // testing with in_array to not be dependant as in_array is faster and this collection would use foreach |
||
153 | // we want to test unique here, not in_array |
||
154 | if (!in_array($value, $collection->toArray(), true)) { |
||
155 | $collection->set($key, $value); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | return $collection; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function contains($value) |
||
166 | { |
||
167 | foreach ($this->items as $val) { |
||
168 | if ($value === $val) { |
||
169 | return true; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | return false; |
||
174 | } |
||
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() |
||
214 | { |
||
215 | $collection = Factory::create([], static::class); |
||
216 | $count = $this->count(); |
||
217 | $keys = $this->keys(); |
||
218 | $values = $this->values(); |
||
219 | |||
220 | for ($i = $count - 1; $i >= 0; $i--) { |
||
221 | $collection->set($keys[$i], $values[$i]); |
||
222 | } |
||
223 | |||
224 | return $collection; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function shift() |
||
231 | { |
||
232 | reset($this->items); |
||
233 | |||
234 | return $this->pull($this->key()); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | public function pop() |
||
241 | { |
||
242 | end($this->items); |
||
243 | |||
244 | return $this->pull($this->key()); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | public function chunk($size) |
||
251 | { |
||
252 | $collection = Factory::create(); |
||
253 | $chunk = Factory::create(); |
||
254 | foreach ($this->items as $key => $value) { |
||
255 | $chunk->set($key, $value); |
||
256 | if ($chunk->count() == $size) { |
||
257 | $collection->add($chunk); |
||
258 | $chunk = Factory::create(); |
||
259 | } |
||
260 | } |
||
261 | if (!$chunk->isEmpty()) { |
||
262 | $collection->add($chunk); |
||
263 | } |
||
264 | |||
265 | return $collection; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | public function slice($offset, $length = PHP_INT_MAX) |
||
272 | { |
||
273 | if ($offset < 0) { |
||
274 | $offset = $this->count() + $offset; |
||
275 | } |
||
276 | |||
277 | return $this->filter( |
||
278 | function ($value, $key, $index) use ($offset, $length) { |
||
279 | return ($index >= $offset) && ($index < $offset + $length); |
||
280 | } |
||
281 | ); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function diff($items) |
||
288 | { |
||
289 | $itemsCollection = Factory::create($items); |
||
290 | |||
291 | return $this->filter( |
||
292 | function ($value, $key, $index) use ($itemsCollection) { |
||
293 | return !$itemsCollection->contains($value); |
||
294 | } |
||
295 | ); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function diffKeys($items) |
||
302 | { |
||
303 | $itemsCollection = Factory::create($items); |
||
304 | |||
305 | return $this->filter( |
||
306 | function ($value, $key, $index) use ($itemsCollection) { |
||
307 | return !$itemsCollection->containsKey($key); |
||
308 | } |
||
309 | ); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | public function intersect($items) |
||
316 | { |
||
317 | $itemsCollection = Factory::create($items); |
||
318 | |||
319 | return $this->filter( |
||
320 | function ($value, $key, $index) use ($itemsCollection) { |
||
321 | return $itemsCollection->contains($value); |
||
322 | } |
||
323 | ); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * {@inheritdoc} |
||
328 | */ |
||
329 | public function intersectKeys($items) |
||
330 | { |
||
331 | $itemsCollection = Factory::create($items); |
||
332 | |||
333 | return $this->filter( |
||
334 | function ($value, $key, $index) use ($itemsCollection) { |
||
335 | return $itemsCollection->containsKey($key); |
||
336 | } |
||
337 | ); |
||
338 | } |
||
339 | } |
||
340 |