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 |
||
34 | class RecordWrapper implements \ArrayAccess, \Countable, \Iterator |
||
35 | { |
||
36 | /** |
||
37 | * An associative array of models to which this model has a one to may relationship. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $hasMany = []; |
||
42 | |||
43 | /** |
||
44 | * An associative array of models which have a one-to-many relationship with this model. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $belongsTo = []; |
||
49 | |||
50 | /** |
||
51 | * An associative array of models with which this model has a many to many relationship. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $manyHaveMany = []; |
||
56 | |||
57 | /** |
||
58 | * The name of the database table. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $table; |
||
63 | |||
64 | /** |
||
65 | * The name of the schema to which this table belongs. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $schema; |
||
70 | |||
71 | /** |
||
72 | * Temporary data held in the model object. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $modelData = []; |
||
77 | |||
78 | /** |
||
79 | * A quoted string of the table name used for building queries. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | private $quotedTable; |
||
84 | |||
85 | /** |
||
86 | * The raw table name without any quotes. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | private $unquotedTable; |
||
91 | |||
92 | /** |
||
93 | * An array of fields that contain validation errors after an attempted save. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | private $invalidFields; |
||
98 | |||
99 | /** |
||
100 | * An instance of the operations dispatcher. |
||
101 | * |
||
102 | * @var Operations |
||
103 | */ |
||
104 | private $dynamicOperations; |
||
105 | |||
106 | /** |
||
107 | * Location of the RecordWrapper's internal iterator. |
||
108 | * |
||
109 | * @var int |
||
110 | */ |
||
111 | private $index = 0; |
||
112 | |||
113 | /** |
||
114 | * This flag is set whenever data is manually put in the model with the setData method. |
||
115 | * |
||
116 | * @var bool |
||
117 | */ |
||
118 | private $dataSet = false; |
||
119 | |||
120 | /** |
||
121 | * The name of the class for this model obtained through reflection. |
||
122 | * |
||
123 | * @var string |
||
124 | */ |
||
125 | private $className; |
||
126 | |||
127 | /** |
||
128 | * An instance of the driver adapter for interacting with the database. |
||
129 | * |
||
130 | * @var DriverAdapter |
||
131 | */ |
||
132 | private $adapter; |
||
133 | |||
134 | /** |
||
135 | * An instance of the ORMContext through which this model is operating. |
||
136 | * |
||
137 | * @var ORMContext |
||
138 | */ |
||
139 | private $context; |
||
140 | |||
141 | /** |
||
142 | * Keys for the various fields when model is accessed as an associative array. |
||
143 | * |
||
144 | * @var array |
||
145 | */ |
||
146 | private $keys = []; |
||
147 | |||
148 | /** |
||
149 | * This flag is set after the model has been properly initialized. |
||
150 | * Useful after model is unserialized or accessed through the static interface. |
||
151 | * |
||
152 | * @var bool |
||
153 | */ |
||
154 | private $initialized = false; |
||
155 | |||
156 | /** |
||
157 | * Initialize the record wrapper and setup the adapters, drivers, tables and schemas. |
||
158 | * After initialization, this method sets the initialized flag. |
||
159 | * |
||
160 | * @return void |
||
161 | */ |
||
162 | 36 | protected function initialize(): void |
|
184 | |||
185 | public function __debugInfo() |
||
191 | |||
192 | /** |
||
193 | * Return a description of the model wrapped by this wrapper. |
||
194 | * |
||
195 | * @return ModelDescription |
||
196 | */ |
||
197 | 28 | public function getDescription() : ModelDescription |
|
207 | |||
208 | /** |
||
209 | * Return the number of items stored in the model or matched by the query. |
||
210 | * Depending on the state of the model, the count method will return different values. For models that have data |
||
211 | * values set with calls to setData, this method returns the number of records that were added. On the other hand, |
||
212 | * for models that do not have data set, this method queries the database to find out the number of records that |
||
213 | * are either in the model, or for models that have been filtered, the number of records that match the filter. |
||
214 | * |
||
215 | * @param int|array|QueryParameters $query |
||
216 | * |
||
217 | * @return int |
||
218 | */ |
||
219 | 14 | public function count($query = null) |
|
227 | |||
228 | /** |
||
229 | * Retrieve an item stored in the record. |
||
230 | * This method returns items that are directly stored in the model or lazy loads related items. The key could be a |
||
231 | * field in the model's table or the name of a related model. |
||
232 | * |
||
233 | * @param string $key A key identifying the item to be retrieved. |
||
234 | * |
||
235 | * @return mixed |
||
236 | */ |
||
237 | 12 | private function retrieveItem($key) |
|
247 | |||
248 | /** |
||
249 | * Calls dynamic methods. |
||
250 | * |
||
251 | * @param string $name |
||
252 | * @param array $arguments |
||
253 | * |
||
254 | * @throws exceptions\NibiiException |
||
255 | * |
||
256 | * @return type |
||
257 | */ |
||
258 | 34 | public function __call($name, $arguments) |
|
267 | |||
268 | /** |
||
269 | * Set a value for a field in the model. |
||
270 | * |
||
271 | * @param string $name |
||
272 | * @param mixed $value |
||
273 | */ |
||
274 | 8 | public function __set($name, $value) |
|
279 | |||
280 | 12 | public function __get($name) |
|
284 | |||
285 | 10 | public function save() |
|
292 | |||
293 | 18 | private function hasMultipleItems() |
|
301 | |||
302 | 18 | public function getData() |
|
316 | |||
317 | 26 | public function setData($data) |
|
322 | |||
323 | public function mergeData($data) |
||
330 | |||
331 | 2 | public function offsetExists($offset) |
|
335 | |||
336 | 2 | public function offsetGet($offset) |
|
344 | |||
345 | 2 | public function offsetSet($offset, $value) |
|
350 | |||
351 | public function offsetUnset($offset) |
||
355 | |||
356 | 6 | private function wrap($offset) |
|
370 | |||
371 | 4 | public function getInvalidFields() |
|
375 | |||
376 | public function getHasMany() |
||
380 | |||
381 | public function getBelongsTo() |
||
385 | |||
386 | 4 | public function current() |
|
390 | |||
391 | public function key() |
||
395 | |||
396 | 4 | public function next() |
|
400 | |||
401 | 4 | public function rewind() |
|
406 | |||
407 | 4 | public function valid() |
|
411 | |||
412 | /** |
||
413 | * A custom validator for the record wrapper. |
||
414 | * |
||
415 | * @return mixed |
||
416 | */ |
||
417 | 10 | public function validate() |
|
421 | |||
422 | 12 | private function fetchRelatedFields($relationship, $index = null) |
|
437 | |||
438 | 28 | public function getRelationships() |
|
446 | |||
447 | public function usetField($field) |
||
451 | |||
452 | /** |
||
453 | * Callback for when a record is either added or modified. |
||
454 | */ |
||
455 | 10 | public function preSaveCallback() |
|
458 | |||
459 | /** |
||
460 | * Callback for when a record has been added or modified. |
||
461 | * |
||
462 | * @param $id |
||
463 | */ |
||
464 | 6 | public function postSaveCallback() |
|
467 | |||
468 | /** |
||
469 | * Callback for when a new record is about to be created. |
||
470 | */ |
||
471 | 8 | public function preCreateCallback() |
|
474 | |||
475 | /** |
||
476 | * Callback for when a new record has been created. |
||
477 | * This callback can be most useful for obtaining the primary key of a newly created record. |
||
478 | * |
||
479 | * @param $id |
||
480 | */ |
||
481 | 4 | public function postCreateCallback($id) |
|
484 | |||
485 | /** |
||
486 | * Callback for when a record is about to be updated. |
||
487 | */ |
||
488 | 2 | public function preUpdateCallback() |
|
491 | |||
492 | /** |
||
493 | * Callback for when a record has been updated. |
||
494 | */ |
||
495 | 2 | public function postUpdateCallback() |
|
498 | |||
499 | 36 | public function getDBStoreInformation() |
|
510 | |||
511 | /** |
||
512 | * @return DriverAdapter |
||
513 | */ |
||
514 | 36 | public function getAdapter() |
|
520 | |||
521 | 4 | private function expandArrayValue($array, $relationships, $depth, $expandableModels = [], $index = null) |
|
535 | |||
536 | 24 | public function toArray($depth = 0, $expandableModels = []) |
|
552 | } |
||
553 |
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..