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 | private $unquotedTable; |
||
86 | private $invalidFields; |
||
87 | private $dynamicOperations; |
||
88 | private $index = 0; |
||
89 | private $dataSet = false; |
||
90 | private $className; |
||
91 | |||
92 | /** |
||
93 | * An instance of the driver adapter for interacting with the database. |
||
94 | * @var DriverAdapter |
||
95 | */ |
||
96 | private $adapter; |
||
97 | private $context; |
||
98 | private $keys = []; |
||
99 | private $initialized = false; |
||
100 | |||
101 | /** |
||
102 | * Initialize the record wrapper and setup the adapters, drivers, tables and schemas. |
||
103 | * @return void |
||
104 | */ |
||
105 | 36 | protected function initialize(): void |
|
127 | |||
128 | public function __debugInfo() |
||
133 | |||
134 | /** |
||
135 | * Return a description of the model wrapped by this wrapper. |
||
136 | * @return ModelDescription |
||
137 | */ |
||
138 | 28 | public function getDescription() |
|
147 | |||
148 | /** |
||
149 | * Return the number of items stored in the model or matched by the query. |
||
150 | * Depending on the state of the model, the count method will return different values. For models that have data |
||
151 | * values set with calls to setData, this method returns the number of records that were added. On the other hand, |
||
152 | * for models that do not have data set, this method queries the database to find out the number of records that |
||
153 | * are either in the model, or for models that have been filtered, the number of records that match the filter. |
||
154 | * |
||
155 | * @param int|array|QueryParameters $query |
||
156 | * @return int |
||
157 | */ |
||
158 | 14 | public function count($query = null) |
|
165 | |||
166 | /** |
||
167 | * Retrieve an item stored in the record. |
||
168 | * This method returns items that are directly stored in the model or lazy |
||
169 | * loads related items. The key could be a field in the model's table or |
||
170 | * the name of a related model. |
||
171 | * @param string $key A key identifying the item to be retrieved. |
||
172 | * @return mixed |
||
173 | */ |
||
174 | 12 | private function retrieveItem($key) |
|
183 | |||
184 | /** |
||
185 | * @method |
||
186 | * @param string $name |
||
187 | * @param array $arguments |
||
188 | * @return type |
||
189 | */ |
||
190 | 34 | public function __call($name, $arguments) |
|
198 | |||
199 | 6 | public function __set($name, $value) |
|
204 | |||
205 | 12 | public function __get($name) |
|
209 | |||
210 | 4 | private function expandArrayValue($array, $relationships, $depth, $index = null) |
|
217 | |||
218 | 16 | public function toArray($depth = 0) |
|
233 | |||
234 | 10 | public function save() |
|
240 | |||
241 | 18 | private function hasMultipleItems() |
|
249 | |||
250 | 18 | public function getData() |
|
264 | |||
265 | 26 | public function setData($data) |
|
270 | |||
271 | public function mergeData($data) |
||
278 | |||
279 | 2 | public function offsetExists($offset) |
|
283 | |||
284 | 2 | public function offsetGet($offset) |
|
292 | |||
293 | 2 | public function offsetSet($offset, $value) |
|
298 | |||
299 | public function offsetUnset($offset) |
||
303 | |||
304 | 6 | private function wrap($offset) |
|
317 | |||
318 | 4 | public function getInvalidFields() |
|
322 | |||
323 | public function getHasMany() |
||
327 | |||
328 | public function getBelongsTo() |
||
332 | |||
333 | 4 | public function current() |
|
337 | |||
338 | public function key() |
||
342 | |||
343 | 4 | public function next() |
|
347 | |||
348 | 4 | public function rewind() |
|
353 | |||
354 | 4 | public function valid() |
|
358 | |||
359 | 10 | public function onValidate($errors) |
|
363 | |||
364 | 12 | private function fetchRelatedFields($relationship, $index = null) |
|
378 | |||
379 | 28 | public function getRelationships() |
|
387 | |||
388 | public function usetField($field) |
||
392 | |||
393 | 8 | public function preSaveCallback() |
|
397 | |||
398 | public function postSaveCallback($id) |
||
402 | |||
403 | 2 | public function preUpdateCallback() |
|
407 | |||
408 | 2 | public function postUpdateCallback() |
|
412 | |||
413 | 36 | public function getDBStoreInformation() |
|
423 | |||
424 | /** |
||
425 | * |
||
426 | * @return DriverAdapter |
||
427 | */ |
||
428 | 36 | public function getAdapter() |
|
433 | |||
434 | } |
||
435 |
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.