Complex classes like RecordWrapper 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 RecordWrapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class RecordWrapper implements \ArrayAccess, \Countable, \Iterator |
||
| 38 | { |
||
| 39 | |||
| 40 | protected $hasMany = []; |
||
| 41 | protected $belongsTo = []; |
||
| 42 | protected $manyHaveMany = []; |
||
| 43 | protected $behaviours = []; |
||
| 44 | protected $table; |
||
| 45 | protected $schema; |
||
| 46 | protected $modelData = []; |
||
| 47 | private $quotedTable; |
||
| 48 | private $unquotedTable; |
||
| 49 | private $invalidFields; |
||
| 50 | private $dynamicOperations; |
||
| 51 | private $index = 0; |
||
| 52 | private $dataSet = false; |
||
| 53 | private $className; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * |
||
| 57 | * @var DriverAdapter |
||
| 58 | */ |
||
| 59 | private $adapter; |
||
| 60 | private $container; |
||
| 61 | private $context; |
||
| 62 | private $keys = []; |
||
| 63 | private $initialized = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Initialize the record wrapper and setup the adapters, drivers, tables and schemas. |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | 36 | protected function initialize(): void |
|
| 92 | |||
| 93 | public function __debugInfo() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * |
||
| 101 | * @return ModelDescription |
||
| 102 | */ |
||
| 103 | 28 | public function getDescription() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Return the number of items stored in the model or the query. |
||
| 115 | * @return integer |
||
| 116 | */ |
||
| 117 | 14 | public function count() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Retrieve an item stored in the record. |
||
| 128 | * This method returns items that are directly stored in the model or lazy |
||
| 129 | * loads related items. The key could be a field in the model's table or |
||
| 130 | * the name of a related model. |
||
| 131 | * @param string $key A key identifying the item to be retrieved. |
||
| 132 | * @return mixed |
||
| 133 | */ |
||
| 134 | 12 | private function retrieveItem($key) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @method |
||
| 146 | * @param type $name |
||
| 147 | * @param type $arguments |
||
| 148 | * @return type |
||
| 149 | */ |
||
| 150 | 34 | public function __call($name, $arguments) |
|
| 158 | |||
| 159 | 8 | public function __set($name, $value) |
|
| 164 | |||
| 165 | 12 | public function __get($name) |
|
| 169 | |||
| 170 | 4 | private function expandArrayValue($array, $relationships, $depth, $index = null) |
|
| 177 | |||
| 178 | 16 | public function toArray($depth = 0) |
|
| 193 | |||
| 194 | 10 | public function save() |
|
| 200 | |||
| 201 | 18 | private function hasMultipleItems() |
|
| 209 | |||
| 210 | 18 | public function getData() |
|
| 224 | |||
| 225 | 26 | public function setData($data) |
|
| 230 | |||
| 231 | public function mergeData($data) |
||
| 238 | |||
| 239 | 2 | public function offsetExists($offset) |
|
| 243 | |||
| 244 | 2 | public function offsetGet($offset) |
|
| 252 | |||
| 253 | 2 | public function offsetSet($offset, $value) |
|
| 258 | |||
| 259 | public function offsetUnset($offset) |
||
| 263 | |||
| 264 | 6 | private function wrap($offset) |
|
| 277 | |||
| 278 | 4 | public function getInvalidFields() |
|
| 282 | |||
| 283 | public function getHasMany() |
||
| 287 | |||
| 288 | public function getBelongsTo() |
||
| 292 | |||
| 293 | 4 | public function current() |
|
| 297 | |||
| 298 | public function key() |
||
| 302 | |||
| 303 | 4 | public function next() |
|
| 307 | |||
| 308 | 4 | public function rewind() |
|
| 313 | |||
| 314 | 4 | public function valid() |
|
| 318 | |||
| 319 | 10 | public function onValidate($errors) |
|
| 323 | |||
| 324 | 12 | private function fetchRelatedFields($relationship, $index = null) |
|
| 338 | |||
| 339 | 28 | public function getRelationships() |
|
| 347 | |||
| 348 | public function usetField($field) |
||
| 352 | |||
| 353 | 8 | public function preSaveCallback() |
|
| 357 | |||
| 358 | 4 | public function postSaveCallback($id) |
|
| 362 | |||
| 363 | 2 | public function preUpdateCallback() |
|
| 367 | |||
| 368 | 2 | public function postUpdateCallback() |
|
| 372 | |||
| 373 | 36 | public function getDBStoreInformation() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * |
||
| 386 | * @return DataAdapter |
||
| 387 | */ |
||
| 388 | 36 | public function getAdapter() |
|
| 393 | |||
| 394 | } |
||
| 395 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.