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 | protected $hasMany = []; |
||
| 40 | protected $belongsTo = []; |
||
| 41 | protected $manyHaveMany = []; |
||
| 42 | protected $behaviours = []; |
||
| 43 | protected $table; |
||
| 44 | protected $schema; |
||
| 45 | private $quotedTable; |
||
| 46 | private $unquotedTable; |
||
| 47 | private $modelData = []; |
||
| 48 | private $invalidFields; |
||
| 49 | private $dynamicOperations; |
||
| 50 | private $index = 0; |
||
| 51 | private $dataSet = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * @var DriverAdapter |
||
| 56 | */ |
||
| 57 | private $adapter; |
||
| 58 | private $container; |
||
| 59 | private $context; |
||
| 60 | private $keys = []; |
||
| 61 | private $initialized = false; |
||
| 62 | |||
| 63 | 37 | private function initialize() { |
|
| 87 | |||
| 88 | public function __debugInfo() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * |
||
| 95 | * @return ModelDescription |
||
| 96 | */ |
||
| 97 | 28 | public function getDescription() { |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Return the number of items stored in the model or the query. |
||
| 108 | * @return integer |
||
| 109 | */ |
||
| 110 | 14 | public function count() { |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Retrieve an item stored in the record. |
||
| 120 | * This method returns items that are directly stored in the model or lazy |
||
| 121 | * loads related items. The key could be a field in the model's table or |
||
| 122 | * the name of a related model. |
||
| 123 | * @param string $key A key identifying the item to be retrieved. |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | 12 | private function retrieveItem($key) { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Create a new instance of this Model |
||
| 137 | * @return \ntentan\nibii\RecordWrapper |
||
| 138 | */ |
||
| 139 | 35 | public static function createNew() { |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @method |
||
| 147 | * @param type $name |
||
| 148 | * @param type $arguments |
||
| 149 | * @return type |
||
| 150 | */ |
||
| 151 | 35 | public function __call($name, $arguments) { |
|
| 163 | |||
| 164 | 27 | public static function __callStatic($name, $arguments) { |
|
| 167 | |||
| 168 | 8 | public function __set($name, $value) { |
|
| 172 | |||
| 173 | 12 | public function __get($name) { |
|
| 176 | |||
| 177 | 4 | private function expandArrayValue($array, $relationships, $depth, $index = null) { |
|
| 183 | |||
| 184 | 16 | public function toArray($depth = 0) { |
|
| 198 | |||
| 199 | 10 | public function save() { |
|
| 204 | |||
| 205 | 18 | private function hasMultipleItems() { |
|
| 212 | |||
| 213 | 18 | public function getData() { |
|
| 226 | |||
| 227 | 27 | public function setData($data) { |
|
| 231 | |||
| 232 | public function mergeData($data) { |
||
| 238 | |||
| 239 | 2 | public function offsetExists($offset) { |
|
| 242 | |||
| 243 | 2 | public function offsetGet($offset) { |
|
| 250 | |||
| 251 | 2 | public function offsetSet($offset, $value) { |
|
| 255 | |||
| 256 | public function offsetUnset($offset) { |
||
| 259 | |||
| 260 | 6 | private function wrap($offset) { |
|
| 269 | |||
| 270 | 4 | public function getInvalidFields() { |
|
| 271 | 4 | return $this->invalidFields; |
|
| 272 | } |
||
| 273 | |||
| 274 | public function getHasMany() { |
||
| 277 | |||
| 278 | public function getBelongsTo() { |
||
| 281 | |||
| 282 | 4 | public function current() { |
|
| 285 | |||
| 286 | public function key() { |
||
| 289 | |||
| 290 | 4 | public function next() { |
|
| 293 | |||
| 294 | 4 | public function rewind() { |
|
| 298 | |||
| 299 | 4 | public function valid() { |
|
| 302 | |||
| 303 | 10 | public function onValidate($errors) { |
|
| 306 | |||
| 307 | 12 | private function fetchRelatedFields($relationship, $index = null) { |
|
| 320 | |||
| 321 | 28 | public function getRelationships() { |
|
| 328 | |||
| 329 | public function usetField($field) { |
||
| 332 | |||
| 333 | 8 | public function preSaveCallback() { |
|
| 336 | |||
| 337 | 4 | public function postSaveCallback($id) { |
|
| 340 | |||
| 341 | 2 | public function preUpdateCallback() { |
|
| 344 | |||
| 345 | 2 | public function postUpdateCallback() { |
|
| 348 | |||
| 349 | 37 | public function getDBStoreInformation() { |
|
| 358 | |||
| 359 | /** |
||
| 360 | * |
||
| 361 | * @return DataAdapter |
||
| 362 | */ |
||
| 363 | public function getAdapter() { |
||
| 367 | |||
| 368 | } |
||
| 369 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: