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 \Traversable */ |
||
| 11 | protected $iterator; |
||
| 12 | |||
| 13 | /** @var array */ |
||
| 14 | protected $items; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param array|\Traversable $items |
||
| 18 | */ |
||
| 19 | 49 | public function __construct($items = []) |
|
| 20 | { |
||
| 21 | 49 | if ($items instanceof Traversable) { |
|
| 22 | 2 | $this->iterator = $items; // for lazy iterate |
|
| 23 | } else { |
||
| 24 | 48 | $this->items = array_values($items); |
|
| 25 | } |
||
| 26 | 49 | } |
|
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | 1 | public function __toString() |
|
| 32 | { |
||
| 33 | 1 | $string = static::class . " [\n"; |
|
| 34 | 1 | foreach ($this as $item) { |
|
| 35 | 1 | $string .= " "; |
|
| 36 | 1 | if (is_string($item)) { |
|
| 37 | 1 | $string .= "\"{$item}\",\n"; |
|
| 38 | 1 | } elseif (is_scalar($item)) { |
|
| 39 | 1 | $string .= "{$item},\n"; |
|
| 40 | 1 | } elseif (is_null($item)) { |
|
| 41 | 1 | $string .= "null,\n"; |
|
| 42 | 1 | } elseif (is_array($item)) { |
|
| 43 | 1 | $string .= "[array],\n"; |
|
| 44 | 1 | } elseif (is_object($item)) { |
|
| 45 | 1 | $string .= "[" . get_class($item) . "],\n"; |
|
| 46 | } else { |
||
| 47 | 1 | $string .= "[unknown],\n"; |
|
| 48 | } |
||
| 49 | } |
||
| 50 | 1 | return $string . ']'; |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | 42 | public function toArray() |
|
| 57 | { |
||
| 58 | 42 | $arr = []; |
|
| 59 | 42 | foreach ($this as $item) { |
|
| 60 | 42 | if (method_exists($item, 'toArray')) { |
|
| 61 | 1 | $arr[] = $item->toArray(); |
|
| 62 | } else { |
||
| 63 | 42 | $arr[] = $item; |
|
| 64 | } |
||
| 65 | } |
||
| 66 | 42 | return $arr; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | 22 | public function all() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 7 | public function count() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 1 | function jsonSerialize() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | 47 | public function getIterator() |
|
| 99 | { |
||
| 100 | 47 | if (isset($this->iterator)) { |
|
| 101 | 3 | $this->items = []; |
|
| 102 | 3 | foreach ($this->iterator as $item) { |
|
| 103 | 3 | yield $this->items[] = $item; |
|
| 104 | } |
||
| 105 | 3 | $this->iterator = null; |
|
| 106 | 3 | return; |
|
| 107 | } |
||
| 108 | 46 | foreach ($this->items as $item) { |
|
| 109 | 46 | yield $item; |
|
| 110 | } |
||
| 111 | 46 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | 1 | public function offsetExists($offset) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | 2 | public function offsetGet($offset) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | 4 | public function offsetSet($offset, $value) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | 1 | public function offsetUnset($offset) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | 1 | public function serialize() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | 1 | public function unserialize($serialized) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function clear() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | 3 | public function contains(...$values) |
|
| 177 | { |
||
| 178 | 3 | $this->executeIterator(); |
|
| 179 | 3 | foreach ($values as $value) { |
|
| 180 | 3 | if (!in_array($value, $this->items, true)) { |
|
| 181 | 3 | return false; |
|
| 182 | } |
||
| 183 | } |
||
| 184 | 3 | return true; |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | 3 | public function get($key, $default = null) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | 6 | public function set($key, $value) |
|
| 200 | { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | 2 | public function remove(...$keys) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | 1 | public function has(...$keys) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | 1 | public function filter(callable $handler = null) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | 1 | public function map(callable $handler) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | 1 | public function reduce(callable $handler, $initial = null) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | */ |
||
| 274 | 2 | public function groupBy(callable $handler) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | */ |
||
| 290 | 2 | public function keyBy(callable $handler) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | 4 | public function combine(ListInterface $list) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | 2 | public function first(callable $handler = null, $default = null) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * {@inheritdoc} |
||
| 328 | */ |
||
| 329 | 2 | public function last(callable $handler = null, $default = null) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * {@inheritdoc} |
||
| 348 | */ |
||
| 349 | 8 | public function intersect(ListInterface $list) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | 4 | public function union(ListInterface $list) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | 4 | public function merge(ListInterface $list) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * {@inheritdoc} |
||
| 375 | */ |
||
| 376 | 2 | public function implode($glue = null) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * {@inheritdoc} |
||
| 384 | */ |
||
| 385 | 2 | public function isEmpty() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * {@inheritdoc} |
||
| 392 | */ |
||
| 393 | 3 | public function pop() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * {@inheritdoc} |
||
| 401 | */ |
||
| 402 | 1 | public function push(...$values) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * {@inheritdoc} |
||
| 411 | */ |
||
| 412 | 1 | public function shift() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * {@inheritdoc} |
||
| 420 | */ |
||
| 421 | 1 | public function unshift(...$values) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * {@inheritdoc} |
||
| 430 | */ |
||
| 431 | 2 | public function reverse() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * {@inheritdoc} |
||
| 439 | */ |
||
| 440 | 2 | public function shuffle() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * {@inheritdoc} |
||
| 450 | */ |
||
| 451 | 1 | public function sort(callable $callback = null) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * {@inheritdoc} |
||
| 465 | */ |
||
| 466 | 2 | public function slice($offset, $length = null) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * {@inheritdoc} |
||
| 474 | */ |
||
| 475 | 4 | public function splice($offset, $length = null, $replacement = null) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * {@inheritdoc} |
||
| 486 | */ |
||
| 487 | 1 | public function unique() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * @param mixed $value |
||
| 495 | * @param string $method |
||
| 496 | * @param int $order |
||
| 497 | */ |
||
| 498 | 4 | private function assertIsNullOrIntegerLessSize($value, $method, $order = 1) |
|
| 513 | |||
| 514 | 55 | private function executeIterator() |
|
| 521 | } |
||
| 522 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: