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 |
||
| 35 | class RecordWrapper implements \ArrayAccess, \Countable, \Iterator |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * An associative array of models to which this model has a one to may relationship. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $hasMany = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * An associative array of models which have a one-to-many relationship with this model. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $belongsTo = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * An associative array of models with which this model has a many to many relationship. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $manyHaveMany = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The name of the database table. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $table; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The name of the schema to which this table belongs. |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $schema; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Temporary data held in the model object. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $modelData = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Extra validation rules to use over the model's inherent validation requirements. |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $validationRules = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * A quoted string of the table name used for building queries. |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | private $quotedTable; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The raw table name without any quotes. |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $unquotedTable; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * An array of fields that contain validation errors after an attempted save. |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | private $invalidFields; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * An instance of the operations dispatcher. |
||
| 108 | * |
||
| 109 | * @var Operations |
||
| 110 | */ |
||
| 111 | private $dynamicOperations; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Location of the RecordWrapper's internal iterator. |
||
| 115 | * |
||
| 116 | * @var int |
||
| 117 | */ |
||
| 118 | private $index = 0; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * This flag is set whenever data is manually put in the model with the setData method. |
||
| 122 | * |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | private $hasDataBeenSet = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * The name of the class for this model obtained through reflection. |
||
| 129 | * |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | private $className; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * An instance of the driver adapter for interacting with the database. |
||
| 136 | * |
||
| 137 | * @var DriverAdapter |
||
| 138 | */ |
||
| 139 | private $adapter; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * An instance of the ORMContext through which this model is operating. |
||
| 143 | * |
||
| 144 | * @var ORMContext |
||
| 145 | */ |
||
| 146 | private $context; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Keys for the various fields when model is accessed as an associative array. |
||
| 150 | * |
||
| 151 | * @var array |
||
| 152 | */ |
||
| 153 | private $keys = []; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * This flag is set after the model has been properly initialized. |
||
| 157 | * Useful after model is unserialized or accessed through the static interface. |
||
| 158 | * |
||
| 159 | * @var bool |
||
| 160 | */ |
||
| 161 | private $initialized = false; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Initialize the record wrapper and setup the adapters, drivers, tables and schemas. |
||
| 165 | * After initialization, this method sets the initialized flag. |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | * @throws NibiiException |
||
| 169 | * @throws \ReflectionException |
||
| 170 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
| 171 | */ |
||
| 172 | 32 | protected function initialize(): void |
|
| 194 | |||
| 195 | public function __debugInfo() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Return a description of the model wrapped by this wrapper. |
||
| 203 | * |
||
| 204 | * @return ModelDescription |
||
| 205 | * @throws NibiiException |
||
| 206 | * @throws \ReflectionException |
||
| 207 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
| 208 | */ |
||
| 209 | 24 | public function getDescription() : ModelDescription |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Return the number of items stored in the model or matched by the query. |
||
| 222 | * Depending on the state of the model, the count method will return different values. For models that have data |
||
| 223 | * values set with calls to setData, this method returns the number of records that were added. On the other hand, |
||
| 224 | * for models that do not have data set, this method queries the database to find out the number of records that |
||
| 225 | * are either in the model, or for models that have been filtered, the number of records that match the filter. |
||
| 226 | * |
||
| 227 | * @param int|array|QueryParameters $query |
||
| 228 | * |
||
| 229 | * @return int |
||
| 230 | * @throws NibiiException |
||
| 231 | * @throws \ReflectionException |
||
| 232 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
| 233 | */ |
||
| 234 | 4 | public function count($query = null) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Retrieve an item stored in the record. |
||
| 245 | * This method returns items that are directly stored in the model, or lazy loads related items if needed. |
||
| 246 | * The key could be a field in the model's table or the name of a related model. |
||
| 247 | * |
||
| 248 | * @param string $key A key identifying the item to be retrieved. |
||
| 249 | * |
||
| 250 | * @return mixed |
||
| 251 | * @throws NibiiException |
||
| 252 | * @throws \ReflectionException |
||
| 253 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
| 254 | */ |
||
| 255 | 8 | private function retrieveItem($key) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Calls dynamic methods. |
||
| 272 | * |
||
| 273 | * @param string $name |
||
| 274 | * @param array $arguments |
||
| 275 | * |
||
| 276 | * @return type |
||
| 277 | * @throws NibiiException |
||
| 278 | * @throws \ReflectionException |
||
| 279 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
| 280 | */ |
||
| 281 | 30 | public function __call($name, $arguments) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Set a value for a field in the model. |
||
| 293 | * |
||
| 294 | * @param string $name |
||
| 295 | * @param mixed $value |
||
| 296 | */ |
||
| 297 | 8 | public function __set($name, $value) |
|
| 302 | |||
| 303 | 8 | public function __get($name) |
|
| 307 | |||
| 308 | 10 | public function save() |
|
| 315 | |||
| 316 | 22 | private function hasMultipleItems() |
|
| 324 | |||
| 325 | 14 | public function getData() |
|
| 339 | |||
| 340 | 22 | public function setData($data) |
|
| 345 | |||
| 346 | public function mergeData($data) |
||
| 353 | |||
| 354 | 2 | public function offsetExists($offset) |
|
| 358 | |||
| 359 | 2 | public function offsetGet($offset) |
|
| 367 | |||
| 368 | 2 | public function offsetSet($offset, $value) |
|
| 373 | |||
| 374 | public function offsetUnset($offset) |
||
| 378 | |||
| 379 | 4 | private function wrap($offset) |
|
| 393 | |||
| 394 | 4 | public function getInvalidFields() |
|
| 398 | |||
| 399 | public function getHasMany() |
||
| 403 | |||
| 404 | public function getBelongsTo() |
||
| 408 | |||
| 409 | 2 | public function current() |
|
| 413 | |||
| 414 | public function key() |
||
| 418 | |||
| 419 | 2 | public function next() |
|
| 423 | |||
| 424 | 2 | public function rewind() |
|
| 429 | |||
| 430 | 2 | public function valid() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * A custom validator for the record wrapper. |
||
| 437 | * |
||
| 438 | * @return mixed |
||
| 439 | */ |
||
| 440 | 10 | public function onValidate($invalidFields) : array |
|
| 444 | |||
| 445 | 8 | private function fetchRelatedFields(Relationship $relationship, $index = null) |
|
| 467 | |||
| 468 | 24 | public function getRelationships() |
|
| 476 | |||
| 477 | public function usetField($field) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Callback for when a record is either added or modified. |
||
| 484 | */ |
||
| 485 | 10 | public function preSaveCallback() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Callback for when a record has been added or modified. |
||
| 491 | * |
||
| 492 | * @param $id |
||
| 493 | */ |
||
| 494 | 6 | public function postSaveCallback() |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Callback for when a new record is about to be created. |
||
| 500 | */ |
||
| 501 | 8 | public function preCreateCallback() |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Callback for when a new record has been created. |
||
| 507 | * This callback can be most useful for obtaining the primary key of a newly created record. |
||
| 508 | * |
||
| 509 | * @param $id |
||
| 510 | */ |
||
| 511 | 4 | public function postCreateCallback($id) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Callback for when a record is about to be updated. |
||
| 517 | */ |
||
| 518 | 2 | public function preUpdateCallback() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Callback for when a record has been updated. |
||
| 524 | */ |
||
| 525 | 2 | public function postUpdateCallback() |
|
| 528 | |||
| 529 | 32 | public function getDBStoreInformation() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * @return DriverAdapter |
||
| 543 | * @throws NibiiException |
||
| 544 | * @throws \ReflectionException |
||
| 545 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
| 546 | */ |
||
| 547 | 32 | public function getAdapter() |
|
| 552 | |||
| 553 | 4 | private function expandArrayValue($array, $relationships, $depth, $expandableModels = [], $index = null) |
|
| 564 | |||
| 565 | 10 | public function getValidationRules() : array |
|
| 569 | |||
| 570 | 20 | public function toArray($depth = 0, $expandableModels = []) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Return an instance of the model populated with array data. |
||
| 592 | */ |
||
| 593 | 16 | public function fromArray($data) |
|
| 605 | } |
||
| 606 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..