Complex classes like OrderedMap 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 OrderedMap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | trait OrderedMap |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $keys = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $values = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $keyIdentityPositionMap = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected $length = 0; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | protected $largestIntKey = -1; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritDoc} |
||
| 39 | */ |
||
| 40 | public function keys() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritDoc} |
||
| 47 | */ |
||
| 48 | public function values() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritDoc} |
||
| 55 | */ |
||
| 56 | public function map(callable $function) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritDoc} |
||
| 73 | */ |
||
| 74 | public function walk(callable $function) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritDoc} |
||
| 85 | */ |
||
| 86 | public function groupBy(callable $groupKeyFunction) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritDoc} |
||
| 111 | */ |
||
| 112 | public function multisort(array $orderByFunctions, array $isAscending) |
||
| 154 | |||
| 155 | public function count() |
||
| 159 | |||
| 160 | private function loadLargestIntKey() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritDoc} |
||
| 172 | */ |
||
| 173 | public function &get($key) |
||
| 174 | { |
||
| 175 | $identityHash = Identity::hash($key); |
||
| 176 | |||
| 177 | if (isset($this->keyIdentityPositionMap[$identityHash])) { |
||
| 178 | return $this->values[$this->keyIdentityPositionMap[$identityHash]]; |
||
| 179 | } else { |
||
| 180 | $null = null; |
||
| 181 | |||
| 182 | return $null; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritDoc} |
||
| 188 | */ |
||
| 189 | public function contains($key) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritDoc} |
||
| 196 | */ |
||
| 197 | public function set($key, $value) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritDoc} |
||
| 204 | */ |
||
| 205 | public function setRef($key, &$value) |
||
| 209 | |||
| 210 | final protected function setInternal($key, &$value, $identityHash, $reference = false) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritDoc} |
||
| 232 | */ |
||
| 233 | public function remove($key) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritDoc} |
||
| 262 | */ |
||
| 263 | public function clear() |
||
| 271 | |||
| 272 | public function offsetExists($offset) |
||
| 276 | |||
| 277 | public function &offsetGet($offset) |
||
| 281 | |||
| 282 | public function offsetSet($offset, $value) |
||
| 289 | |||
| 290 | public function offsetUnset($offset) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | final public function isArrayCompatible() |
||
| 302 | } |
||
| 303 |