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 | /** |
||
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 | * A quoted string of the table name used for building queries. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | private $quotedTable; |
||
85 | |||
86 | /** |
||
87 | * The raw table name without any quotes. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | private $unquotedTable; |
||
92 | |||
93 | /** |
||
94 | * An array of fields that contain validation errors after an attempted save. |
||
95 | * |
||
96 | * @var array |
||
97 | */ |
||
98 | private $invalidFields; |
||
99 | |||
100 | /** |
||
101 | * An instance of the operations dispatcher. |
||
102 | * |
||
103 | * @var Operations |
||
104 | */ |
||
105 | private $dynamicOperations; |
||
106 | |||
107 | /** |
||
108 | * Location of the RecordWrapper's internal iterator. |
||
109 | * |
||
110 | * @var int |
||
111 | */ |
||
112 | private $index = 0; |
||
113 | |||
114 | /** |
||
115 | * This flag is set whenever data is manually put in the model with the setData method. |
||
116 | * |
||
117 | * @var bool |
||
118 | */ |
||
119 | private $dataSet = false; |
||
120 | |||
121 | /** |
||
122 | * The name of the class for this model obtained through reflection. |
||
123 | * |
||
124 | * @var string |
||
125 | */ |
||
126 | private $className; |
||
127 | |||
128 | /** |
||
129 | * An instance of the driver adapter for interacting with the database. |
||
130 | * |
||
131 | * @var DriverAdapter |
||
132 | */ |
||
133 | private $adapter; |
||
134 | |||
135 | /** |
||
136 | * An instance of the ORMContext through which this model is operating. |
||
137 | * |
||
138 | * @var ORMContext |
||
139 | */ |
||
140 | private $context; |
||
141 | |||
142 | /** |
||
143 | * Keys for the various fields when model is accessed as an associative array. |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | private $keys = []; |
||
148 | |||
149 | /** |
||
150 | * This flag is set after the model has been properly initialized. |
||
151 | * Useful after model is unserialized or accessed through the static interface. |
||
152 | * |
||
153 | * @var bool |
||
154 | */ |
||
155 | private $initialized = false; |
||
156 | |||
157 | /** |
||
158 | * Initialize the record wrapper and setup the adapters, drivers, tables and schemas. |
||
159 | * After initialization, this method sets the initialized flag. |
||
160 | * @return void |
||
161 | */ |
||
162 | 36 | protected function initialize(): void |
|
184 | |||
185 | public function __debugInfo() |
||
190 | |||
191 | /** |
||
192 | * Return a description of the model wrapped by this wrapper. |
||
193 | * @return ModelDescription |
||
194 | */ |
||
195 | 28 | public function getDescription() : ModelDescription |
|
204 | |||
205 | /** |
||
206 | * Return the number of items stored in the model or matched by the query. |
||
207 | * Depending on the state of the model, the count method will return different values. For models that have data |
||
208 | * values set with calls to setData, this method returns the number of records that were added. On the other hand, |
||
209 | * for models that do not have data set, this method queries the database to find out the number of records that |
||
210 | * are either in the model, or for models that have been filtered, the number of records that match the filter. |
||
211 | * |
||
212 | * @param int|array|QueryParameters $query |
||
213 | * @return int |
||
214 | */ |
||
215 | 14 | public function count($query = null) |
|
222 | |||
223 | /** |
||
224 | * Retrieve an item stored in the record. |
||
225 | * This method returns items that are directly stored in the model or lazy loads related items. The key could be a |
||
226 | * field in the model's table or the name of a related model. |
||
227 | * |
||
228 | * @param string $key A key identifying the item to be retrieved. |
||
229 | * @return mixed |
||
230 | */ |
||
231 | 12 | private function retrieveItem($key) |
|
240 | |||
241 | /** |
||
242 | * Calls dynamic methods. |
||
243 | * |
||
244 | * @param string $name |
||
245 | * @param array $arguments |
||
246 | * @return type |
||
247 | * @throws exceptions\NibiiException |
||
248 | */ |
||
249 | 34 | public function __call($name, $arguments) |
|
257 | |||
258 | /** |
||
259 | * Set a value for a field in the model. |
||
260 | * |
||
261 | * @param string $name |
||
262 | * @param mixed $value |
||
263 | */ |
||
264 | 8 | public function __set($name, $value) |
|
269 | |||
270 | 12 | public function __get($name) |
|
274 | |||
275 | 10 | public function save() |
|
281 | |||
282 | 18 | private function hasMultipleItems() |
|
290 | |||
291 | 18 | public function getData() |
|
305 | |||
306 | 26 | public function setData($data) |
|
311 | |||
312 | public function mergeData($data) |
||
319 | |||
320 | 2 | public function offsetExists($offset) |
|
324 | |||
325 | 2 | public function offsetGet($offset) |
|
333 | |||
334 | 2 | public function offsetSet($offset, $value) |
|
339 | |||
340 | public function offsetUnset($offset) |
||
344 | |||
345 | 6 | private function wrap($offset) |
|
358 | |||
359 | 4 | public function getInvalidFields() |
|
363 | |||
364 | public function getHasMany() |
||
368 | |||
369 | public function getBelongsTo() |
||
373 | |||
374 | 4 | public function current() |
|
378 | |||
379 | public function key() |
||
383 | |||
384 | 4 | public function next() |
|
388 | |||
389 | 4 | public function rewind() |
|
394 | |||
395 | 4 | public function valid() |
|
399 | |||
400 | 10 | public function onValidate($errors) |
|
404 | |||
405 | 12 | private function fetchRelatedFields($relationship, $index = null) |
|
419 | |||
420 | 28 | public function getRelationships() |
|
428 | |||
429 | public function usetField($field) |
||
433 | |||
434 | 8 | public function preSaveCallback() |
|
438 | |||
439 | 4 | public function postSaveCallback($id) |
|
443 | |||
444 | 2 | public function preUpdateCallback() |
|
448 | |||
449 | 2 | public function postUpdateCallback() |
|
453 | |||
454 | 36 | public function getDBStoreInformation() |
|
464 | |||
465 | /** |
||
466 | * |
||
467 | * @return DriverAdapter |
||
468 | */ |
||
469 | 36 | public function getAdapter() |
|
474 | |||
475 | 4 | private function expandArrayValue($array, $relationships, $depth, $expandableModels = [], $index = null) |
|
488 | |||
489 | 16 | public function toArray($depth = 0, $expandableModels = []) |
|
504 | |||
505 | } |
||
506 |
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
@return
annotation as described here.