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 $dataSet = 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 | */ |
||
| 169 | 36 | protected function initialize(): void |
|
| 191 | |||
| 192 | public function __debugInfo() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Return a description of the model wrapped by this wrapper. |
||
| 201 | * |
||
| 202 | * @return ModelDescription |
||
| 203 | */ |
||
| 204 | 28 | public function getDescription() : ModelDescription |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Return the number of items stored in the model or matched by the query. |
||
| 217 | * Depending on the state of the model, the count method will return different values. For models that have data |
||
| 218 | * values set with calls to setData, this method returns the number of records that were added. On the other hand, |
||
| 219 | * for models that do not have data set, this method queries the database to find out the number of records that |
||
| 220 | * are either in the model, or for models that have been filtered, the number of records that match the filter. |
||
| 221 | * |
||
| 222 | * @param int|array|QueryParameters $query |
||
| 223 | * |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | 8 | public function count($query = null) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Retrieve an item stored in the record. |
||
| 237 | * This method returns items that are directly stored in the model, or lazy loads related items if needed. |
||
| 238 | * The key could be a field in the model's table or the name of a related model. |
||
| 239 | * |
||
| 240 | * @param string $key A key identifying the item to be retrieved. |
||
| 241 | * |
||
| 242 | * @return mixed |
||
| 243 | */ |
||
| 244 | 12 | private function retrieveItem($key) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Calls dynamic methods. |
||
| 260 | * |
||
| 261 | * @param string $name |
||
| 262 | * @param array $arguments |
||
| 263 | * |
||
| 264 | * @throws exceptions\NibiiException |
||
| 265 | * |
||
| 266 | * @return type |
||
| 267 | */ |
||
| 268 | 34 | public function __call($name, $arguments) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Set a value for a field in the model. |
||
| 280 | * |
||
| 281 | * @param string $name |
||
| 282 | * @param mixed $value |
||
| 283 | */ |
||
| 284 | 8 | public function __set($name, $value) |
|
| 289 | |||
| 290 | 12 | public function __get($name) |
|
| 294 | |||
| 295 | 10 | public function save() |
|
| 302 | |||
| 303 | 26 | private function hasMultipleItems() |
|
| 311 | |||
| 312 | 18 | public function getData() |
|
| 326 | |||
| 327 | 26 | public function setData($data) |
|
| 332 | |||
| 333 | public function mergeData($data) |
||
| 340 | |||
| 341 | 2 | public function offsetExists($offset) |
|
| 345 | |||
| 346 | 2 | public function offsetGet($offset) |
|
| 354 | |||
| 355 | 2 | public function offsetSet($offset, $value) |
|
| 360 | |||
| 361 | public function offsetUnset($offset) |
||
| 365 | |||
| 366 | 6 | private function wrap($offset) |
|
| 380 | |||
| 381 | 4 | public function getInvalidFields() |
|
| 385 | |||
| 386 | public function getHasMany() |
||
| 390 | |||
| 391 | public function getBelongsTo() |
||
| 395 | |||
| 396 | 4 | public function current() |
|
| 400 | |||
| 401 | public function key() |
||
| 405 | |||
| 406 | 4 | public function next() |
|
| 410 | |||
| 411 | 4 | public function rewind() |
|
| 416 | |||
| 417 | 4 | public function valid() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * A custom validator for the record wrapper. |
||
| 424 | * |
||
| 425 | * @return mixed |
||
| 426 | */ |
||
| 427 | 10 | public function onValidate($invalidFields) : array |
|
| 431 | |||
| 432 | 12 | private function fetchRelatedFields($relationship, $index = null) |
|
| 447 | |||
| 448 | 28 | public function getRelationships() |
|
| 456 | |||
| 457 | public function usetField($field) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Callback for when a record is either added or modified. |
||
| 464 | */ |
||
| 465 | 10 | public function preSaveCallback() |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Callback for when a record has been added or modified. |
||
| 471 | * |
||
| 472 | * @param $id |
||
| 473 | */ |
||
| 474 | 6 | public function postSaveCallback() |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Callback for when a new record is about to be created. |
||
| 480 | */ |
||
| 481 | 8 | public function preCreateCallback() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Callback for when a new record has been created. |
||
| 487 | * This callback can be most useful for obtaining the primary key of a newly created record. |
||
| 488 | * |
||
| 489 | * @param $id |
||
| 490 | */ |
||
| 491 | 4 | public function postCreateCallback($id) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Callback for when a record is about to be updated. |
||
| 497 | */ |
||
| 498 | 2 | public function preUpdateCallback() |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Callback for when a record has been updated. |
||
| 504 | */ |
||
| 505 | 2 | public function postUpdateCallback() |
|
| 508 | |||
| 509 | 36 | public function getDBStoreInformation() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * @return DriverAdapter |
||
| 523 | */ |
||
| 524 | 36 | public function getAdapter() |
|
| 529 | |||
| 530 | 4 | private function expandArrayValue($array, $relationships, $depth, $expandableModels = [], $index = null) |
|
| 544 | |||
| 545 | 10 | public function getValidationRules() : array |
|
| 549 | |||
| 550 | 24 | public function toArray($depth = 0, $expandableModels = []) |
|
| 566 | } |
||
| 567 |
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..