Complex classes like ArrayList 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 ArrayList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ArrayList implements ListInterface |
||
9 | { |
||
10 | /** @var array */ |
||
11 | protected $items; |
||
12 | |||
13 | /** |
||
14 | * @param array|\Traversable $items |
||
15 | */ |
||
16 | 53 | public function __construct($items = []) |
|
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | 1 | public function __toString() |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 43 | public function toArray() |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 22 | public function all() |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 7 | public function count() |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 1 | function jsonSerialize() |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 49 | public function getIterator() |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 1 | public function offsetExists($offset) |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | 4 | public function offsetGet($offset) |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 4 | public function offsetSet($offset, $value) |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 1 | public function offsetUnset($offset) |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 1 | public function serialize() |
|
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | 1 | public function unserialize($serialized) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function clear() |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 3 | public function contains(...$values) |
|
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | 5 | public function get($key, $default = null) |
|
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | 6 | public function set($key, $value) |
|
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | 2 | public function remove(...$keys) |
|
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | 1 | public function has(...$keys) |
|
213 | |||
214 | /** |
||
215 | * {@inheritdoc} |
||
216 | */ |
||
217 | 1 | public function filter(callable $handler = null) |
|
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | 3 | public function map(callable $handler) |
|
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | 1 | public function reduce(callable $handler, $initial = null) |
|
247 | |||
248 | /** |
||
249 | * {@inheritdoc} |
||
250 | */ |
||
251 | 2 | public function groupBy(callable $handler) |
|
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | 2 | public function keyBy(callable $handler) |
|
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | 4 | public function combine(ListInterface $list) |
|
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | 2 | public function first(callable $handler = null, $default = null) |
|
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | 2 | public function last(callable $handler = null, $default = null) |
|
319 | |||
320 | /** |
||
321 | * {@inheritdoc} |
||
322 | */ |
||
323 | 8 | public function intersect(ListInterface $list) |
|
327 | |||
328 | /** |
||
329 | * {@inheritdoc} |
||
330 | */ |
||
331 | 4 | public function union(ListInterface $list) |
|
335 | |||
336 | /** |
||
337 | * {@inheritdoc} |
||
338 | */ |
||
339 | 4 | public function merge(ListInterface $list) |
|
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | 2 | public function implode($glue = null) |
|
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | 2 | public function isEmpty() |
|
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | 3 | public function pop() |
|
367 | |||
368 | /** |
||
369 | * {@inheritdoc} |
||
370 | */ |
||
371 | 1 | public function push(...$values) |
|
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | 1 | public function shift() |
|
384 | |||
385 | /** |
||
386 | * {@inheritdoc} |
||
387 | */ |
||
388 | 1 | public function unshift(...$values) |
|
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | 2 | public function reverse() |
|
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | 2 | public function shuffle() |
|
411 | |||
412 | /** |
||
413 | * {@inheritdoc} |
||
414 | */ |
||
415 | 1 | public function sort(callable $callback = null) |
|
425 | |||
426 | /** |
||
427 | * {@inheritdoc} |
||
428 | */ |
||
429 | 2 | public function slice($offset, $length = null) |
|
433 | |||
434 | /** |
||
435 | * {@inheritdoc} |
||
436 | */ |
||
437 | 4 | public function splice($offset, $length = null, $replacement = null) |
|
444 | |||
445 | /** |
||
446 | * {@inheritdoc} |
||
447 | */ |
||
448 | 1 | public function unique() |
|
452 | |||
453 | /** |
||
454 | * @param mixed $value |
||
455 | * @param string $method |
||
456 | * @param int $order |
||
457 | */ |
||
458 | 4 | private function assertIsNullOrIntegerLessSize($value, $method, $order = 1) |
|
473 | } |
||
474 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: