Complex classes like ArrayMap 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 ArrayMap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class ArrayMap implements MapInterface |
||
| 9 | { |
||
| 10 | /** @var array */ |
||
| 11 | protected $items; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @param array $items |
||
| 15 | */ |
||
| 16 | 24 | public function __construct(array $items = []) |
|
| 20 | |||
| 21 | /** |
||
| 22 | * {@inheritdoc} |
||
| 23 | */ |
||
| 24 | 1 | public function __toString() |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | public function toArray() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | 1 | public function all() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | public function isEmpty() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | 2 | public function count() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | 1 | function jsonSerialize() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | 1 | public function offsetExists($offset) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | 2 | public function offsetGet($offset) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | 2 | public function offsetSet($offset, $value) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | 1 | public function offsetUnset($offset) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | 1 | public function getIterator() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 1 | public function serialize() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | 1 | public function unserialize($serialized) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function clear() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | 1 | public function contains(...$values) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | 3 | public function get($key, $default = null) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | 3 | public function set($key, $value) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | 2 | public function remove(...$keys) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | 1 | public function has(...$keys) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | 1 | public function keys() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | 1 | public function values() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | 1 | public function map(callable $handler) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | */ |
||
| 237 | 1 | public function reduce(callable $handler, $initial = null) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param mixed $value |
||
| 247 | * @param string $method |
||
| 248 | * @param int $order |
||
| 249 | */ |
||
| 250 | 4 | private function assertIsNotNull($value, $method, $order = 1) |
|
| 256 | } |
||
| 257 |