Complex classes like Iterator 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 Iterator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | final class Iterator |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private $values = []; |
||
15 | |||
16 | /** |
||
17 | * Iterator constructor. |
||
18 | * |
||
19 | * @param array $values |
||
20 | */ |
||
21 | 34 | public function __construct(array $values) |
|
25 | |||
26 | /** |
||
27 | * @param string $glue |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | 10 | public function implode(string $glue = ''): string |
|
35 | |||
36 | /** |
||
37 | * @return array |
||
38 | */ |
||
39 | 12 | public function collect(): array |
|
43 | |||
44 | /** |
||
45 | * @return bool |
||
46 | */ |
||
47 | public function isEmpty(): bool |
||
51 | |||
52 | /** |
||
53 | * @return int |
||
54 | */ |
||
55 | 1 | public function length(): int |
|
59 | |||
60 | /** |
||
61 | * @return Iterator |
||
|
|||
62 | */ |
||
63 | 1 | public function values(): self |
|
67 | |||
68 | /** |
||
69 | * @return Iterator |
||
70 | */ |
||
71 | 1 | public function keys(): self |
|
75 | |||
76 | /** |
||
77 | * @return mixed |
||
78 | */ |
||
79 | 3 | public function first() |
|
83 | |||
84 | /** |
||
85 | * @return mixed |
||
86 | */ |
||
87 | 1 | public function last() |
|
91 | |||
92 | /** |
||
93 | * @return mixed |
||
94 | */ |
||
95 | 3 | public function next() |
|
99 | |||
100 | /** |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function previous() |
||
107 | |||
108 | /** |
||
109 | * @return mixed |
||
110 | */ |
||
111 | public function peek() |
||
118 | |||
119 | /** |
||
120 | * @return mixed |
||
121 | */ |
||
122 | 1 | public function popBack() |
|
126 | |||
127 | /** |
||
128 | * @return mixed |
||
129 | */ |
||
130 | 1 | public function popFront() |
|
134 | |||
135 | /** |
||
136 | * @param int $amount |
||
137 | * |
||
138 | * @return Iterator |
||
139 | */ |
||
140 | 6 | public function take(int $amount): self |
|
144 | |||
145 | /** |
||
146 | * @param int $amount |
||
147 | * |
||
148 | * @return Iterator |
||
149 | */ |
||
150 | 6 | public function skip(int $amount): self |
|
154 | |||
155 | /** |
||
156 | * @param int $times |
||
157 | * |
||
158 | * @return Iterator |
||
159 | */ |
||
160 | 1 | public function repeat(int $times): self |
|
169 | |||
170 | /** |
||
171 | * @param int $from |
||
172 | * @param int $too |
||
173 | * |
||
174 | * @return Iterator |
||
175 | */ |
||
176 | 1 | public function slice(int $from, int $too): self |
|
180 | |||
181 | /** |
||
182 | * @param int $size |
||
183 | * |
||
184 | * @return Iterator |
||
185 | */ |
||
186 | 1 | public function chunks(int $size): self |
|
190 | |||
191 | /** |
||
192 | * @param callable $callback |
||
193 | * @param null $initial |
||
194 | * |
||
195 | * @return mixed |
||
196 | */ |
||
197 | 1 | public function fold(callable $callback, $initial = null) |
|
201 | |||
202 | /** |
||
203 | * @param callable|null $callback |
||
204 | * |
||
205 | * @return Iterator |
||
206 | */ |
||
207 | 2 | public function filter(callable $callback = null): self |
|
215 | |||
216 | /** |
||
217 | * @param callable $callback |
||
218 | * |
||
219 | * @return Iterator |
||
220 | */ |
||
221 | public function map(callable $callback): self |
||
225 | |||
226 | /** |
||
227 | * @return Iterator |
||
228 | */ |
||
229 | public function unique(): self |
||
233 | |||
234 | /** |
||
235 | * @return Iterator |
||
236 | */ |
||
237 | public function reverse(): self |
||
241 | |||
242 | /** |
||
243 | * @param callable $callback |
||
244 | * |
||
245 | * @return int |
||
246 | */ |
||
247 | 2 | private function countWhile(callable $callback): int |
|
260 | |||
261 | /** |
||
262 | * @param callable $callback |
||
263 | * |
||
264 | * @return int |
||
265 | */ |
||
266 | 8 | private function countUntil(callable $callback): int |
|
279 | |||
280 | /** |
||
281 | * @param callable $callback |
||
282 | * |
||
283 | * @return Iterator |
||
284 | */ |
||
285 | 1 | public function takeWhile(callable $callback): self |
|
289 | |||
290 | /** |
||
291 | * @param callable $callback |
||
292 | * |
||
293 | * @return Iterator |
||
294 | */ |
||
295 | 1 | public function skipWhile(callable $callback): self |
|
299 | |||
300 | /** |
||
301 | * @param $value |
||
302 | * |
||
303 | * @return Iterator |
||
304 | */ |
||
305 | 4 | public function from($value): self |
|
313 | |||
314 | /** |
||
315 | * @param $value |
||
316 | * |
||
317 | * @return Iterator |
||
318 | */ |
||
319 | public function until($value): self |
||
327 | |||
328 | /** |
||
329 | * @param $value |
||
330 | * |
||
331 | * @return Iterator |
||
332 | */ |
||
333 | 2 | public function before($value): self |
|
337 | |||
338 | /** |
||
339 | * @param $value |
||
340 | * |
||
341 | * @return Iterator |
||
342 | */ |
||
343 | 2 | public function after($value): self |
|
347 | |||
348 | /** |
||
349 | * @param $value |
||
350 | * |
||
351 | * @return mixed |
||
352 | */ |
||
353 | 2 | public function keyOf($value) |
|
357 | |||
358 | /** |
||
359 | * @param $value |
||
360 | * |
||
361 | * @return Iterator |
||
362 | */ |
||
363 | 1 | public function keysOf($value): self |
|
367 | |||
368 | /** |
||
369 | * @param callable $callback |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | 1 | public function all(callable $callback): bool |
|
383 | |||
384 | /** |
||
385 | * @param callable $callback |
||
386 | * |
||
387 | * @return bool |
||
388 | */ |
||
389 | 1 | public function any(callable $callback): bool |
|
399 | } |
||
400 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.