Complex classes like ArrayManager 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 ArrayManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class ArrayManager implements \ArrayAccess, \Countable, \Iterator |
||
| 19 | { |
||
| 20 | use CondorcetVersion; |
||
| 21 | |||
| 22 | ////// |
||
| 23 | |||
| 24 | public static $CacheSize = 2000; |
||
| 25 | public static $MaxContainerLength = 2000; |
||
| 26 | |||
| 27 | protected $_Container = []; |
||
| 28 | protected $_DataHandler = null; |
||
| 29 | protected $_link = []; |
||
| 30 | |||
| 31 | protected $_Cache = []; |
||
| 32 | protected $_CacheMaxKey = 0; |
||
| 33 | protected $_CacheMinKey = 0; |
||
| 34 | |||
| 35 | protected $_cursor = null; |
||
| 36 | protected $_counter = 0; |
||
| 37 | protected $_maxKey = -1; |
||
| 38 | |||
| 39 | public function __construct () {} |
||
| 40 | |||
| 41 | 1 | public function __destruct () |
|
| 45 | |||
| 46 | 1 | public function __clone () |
|
| 50 | |||
| 51 | 2 | public function __sleep () : array |
|
| 59 | |||
| 60 | 1 | public function __wakeup () |
|
| 65 | |||
| 66 | |||
| 67 | /////////// Implement ArrayAccess /////////// |
||
| 68 | |||
| 69 | 101 | public function offsetSet($offset, $value) : void |
|
| 70 | { |
||
| 71 | 101 | if ($offset === null) : |
|
| 72 | 101 | $this->_Container[++$this->_maxKey] = $value; |
|
| 73 | 101 | ++$this->_counter; |
|
| 74 | else : |
||
| 75 | 1 | $state = $this->keyExist($offset); |
|
| 76 | 1 | $this->_Container[$offset] = $value; |
|
| 77 | |||
| 78 | 1 | if (!$state) : |
|
| 79 | ++$this->_counter; |
||
| 80 | |||
| 81 | if ($offset > $this->_maxKey) : |
||
| 82 | $this->_maxKey = $offset; |
||
| 83 | endif; |
||
| 84 | |||
| 85 | ksort($this->_Container,SORT_NUMERIC); |
||
| 86 | 1 | elseif ($this->_DataHandler !== null) : |
|
| 87 | 1 | $this->_DataHandler->deleteOneEntity($offset, true); |
|
| 88 | endif; |
||
| 89 | |||
| 90 | 1 | $this->clearCache(); |
|
| 91 | endif; |
||
| 92 | |||
| 93 | 101 | $this->checkRegularize(); |
|
| 94 | 101 | } |
|
| 95 | |||
| 96 | // Use by isset() function, must return false if offset value is null. |
||
| 97 | public function offsetExists($offset) : bool |
||
| 101 | |||
| 102 | 7 | public function offsetUnset($offset) : void |
|
| 120 | |||
| 121 | 84 | public function offsetGet($offset) |
|
| 141 | |||
| 142 | |||
| 143 | /////////// Implement Iterator /////////// |
||
| 144 | |||
| 145 | protected $valid = true; |
||
| 146 | |||
| 147 | 83 | public function rewind() : void { |
|
| 154 | |||
| 155 | 83 | public function current() { |
|
| 158 | |||
| 159 | 83 | public function key() : ?int |
|
| 167 | |||
| 168 | 83 | public function next() : void |
|
| 185 | |||
| 186 | 71 | protected function setCursorOnNextKeyInArray (array &$array) |
|
| 195 | |||
| 196 | 83 | public function valid() : bool { |
|
| 199 | |||
| 200 | |||
| 201 | /////////// Implement Countable /////////// |
||
| 202 | |||
| 203 | 11 | public function count () : int { |
|
| 206 | |||
| 207 | /////////// Array Methods /////////// |
||
| 208 | |||
| 209 | 16 | public function getFullDataSet () : array |
|
| 220 | |||
| 221 | 8 | public function keyExist ($offset) : bool |
|
| 229 | |||
| 230 | 83 | public function getFirstKey () : int |
|
| 240 | |||
| 241 | 5 | public function getContainerSize () : int |
|
| 245 | |||
| 246 | 1 | public function getCacheSize () : int |
|
| 250 | |||
| 251 | 1 | public function debugGetCache () : array |
|
| 255 | |||
| 256 | |||
| 257 | /////////// HANDLER API /////////// |
||
| 258 | |||
| 259 | abstract protected function preDeletedTask ($object) : void; |
||
| 260 | |||
| 261 | 6 | public function regularize () : bool |
|
| 271 | |||
| 272 | 101 | public function checkRegularize () : bool |
|
| 281 | |||
| 282 | 3 | protected function populateCache () : void |
|
| 297 | |||
| 298 | 6 | public function clearCache () : void |
|
| 308 | |||
| 309 | 81 | public function isUsingHandler () |
|
| 313 | |||
| 314 | /////////// HANDLER INTERRACTION /////////// |
||
| 315 | |||
| 316 | 5 | public function resetCounter () : int |
|
| 320 | |||
| 321 | 5 | public function resetMaxKey () : ?int |
|
| 335 | |||
| 336 | 4 | public function importHandler (DataHandlerDriverInterface $handler) : bool |
|
| 359 | |||
| 360 | 1 | public function closeHandler () : void |
|
| 374 | |||
| 375 | abstract public function getDataContextObject () : DataContextInterface; |
||
| 376 | } |
||
| 377 |