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 | * @var |
||
102 | */ |
||
103 | private $dynamicOperations; |
||
104 | private $index = 0; |
||
105 | private $dataSet = false; |
||
106 | private $className; |
||
107 | |||
108 | /** |
||
109 | * An instance of the driver adapter for interacting with the database. |
||
110 | * @var DriverAdapter |
||
111 | */ |
||
112 | private $adapter; |
||
113 | private $context; |
||
114 | private $keys = []; |
||
115 | private $initialized = false; |
||
116 | |||
117 | /** |
||
118 | * Initialize the record wrapper and setup the adapters, drivers, tables and schemas. |
||
119 | * @return void |
||
120 | */ |
||
121 | 36 | protected function initialize(): void |
|
122 | { |
||
123 | 36 | if ($this->initialized) { |
|
124 | 36 | return; |
|
125 | } |
||
126 | 36 | $this->context = ORMContext::getInstance(); |
|
127 | 36 | $this->adapter = $this->context->getDriverAdapter(); |
|
128 | 36 | $table = $this->table ?? $this->context->getModelTable($this); |
|
129 | 36 | $driver = $this->context->getDbContext()->getDriver(); |
|
130 | 36 | $this->adapter->setContext($this->context); |
|
131 | 36 | $this->className = (new \ReflectionClass($this))->getName(); |
|
132 | 36 | if (is_string($table)) { |
|
133 | 36 | $this->table = $this->unquotedTable = $table; |
|
134 | } else { |
||
135 | $this->table = $table['table']; |
||
136 | $this->schema = $table['schema']; |
||
137 | } |
||
138 | 36 | $this->quotedTable = ($this->schema ? "{$driver->quoteIdentifier($this->schema)}." : "") . $driver->quoteIdentifier($this->table); |
|
139 | 36 | $this->unquotedTable = ($this->schema ? "{$this->schema}." : "") . $this->table; |
|
140 | 36 | $this->adapter->setModel($this, $this->quotedTable); |
|
141 | 36 | $this->initialized = true; |
|
142 | 36 | } |
|
143 | |||
144 | public function __debugInfo() |
||
|
|||
145 | { |
||
146 | $data = $this->getData(); |
||
147 | return $this->hasMultipleItems() ? $data : isset($data[0]) ? $data[0] : []; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Return a description of the model wrapped by this wrapper. |
||
152 | * @return ModelDescription |
||
153 | */ |
||
154 | 28 | public function getDescription() |
|
155 | { |
||
156 | 28 | $this->initialize(); |
|
157 | 28 | return $this->context->getCache()->read( |
|
158 | 28 | "{$this->className}::desc", function () { |
|
159 | 28 | return $this->context->getModelDescription($this); |
|
160 | 28 | } |
|
161 | ); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Return the number of items stored in the model or matched by the query. |
||
166 | * Depending on the state of the model, the count method will return different values. For models that have data |
||
167 | * values set with calls to setData, this method returns the number of records that were added. On the other hand, |
||
168 | * for models that do not have data set, this method queries the database to find out the number of records that |
||
169 | * are either in the model, or for models that have been filtered, the number of records that match the filter. |
||
170 | * |
||
171 | * @param int|array|QueryParameters $query |
||
172 | * @return int |
||
173 | */ |
||
174 | 14 | public function count($query = null) |
|
175 | { |
||
176 | 14 | if ($this->dataSet) { |
|
177 | 14 | return count($this->getData()); |
|
178 | } |
||
179 | return $this->__call('count', [$query]); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Retrieve an item stored in the record. |
||
184 | * This method returns items that are directly stored in the model or lazy loads related items. The key could be a |
||
185 | * field in the model's table or the name of a related model. |
||
186 | * |
||
187 | * @param string $key A key identifying the item to be retrieved. |
||
188 | * @return mixed |
||
189 | */ |
||
190 | 12 | private function retrieveItem($key) |
|
191 | { |
||
192 | 12 | $relationships = $this->getDescription()->getRelationships(); |
|
193 | 12 | $decamelizedKey = Text::deCamelize($key); |
|
194 | 12 | if (isset($relationships[$key])) { |
|
195 | 8 | return $this->fetchRelatedFields($relationships[$key]); |
|
196 | } |
||
197 | 4 | return isset($this->modelData[$decamelizedKey]) ? $this->modelData[$decamelizedKey] : null; |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * Calls dynamic methods. |
||
202 | * |
||
203 | * @method |
||
204 | * @param string $name |
||
205 | * @param array $arguments |
||
206 | * @return type |
||
207 | */ |
||
208 | 34 | public function __call($name, $arguments) |
|
209 | { |
||
210 | 34 | $this->initialize(); |
|
211 | 34 | if ($this->dynamicOperations === null) { |
|
212 | 34 | $this->dynamicOperations = new Operations($this, $this->quotedTable); |
|
213 | } |
||
214 | 34 | return $this->dynamicOperations->perform($name, $arguments); |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * Set a value for a field in the model. |
||
219 | * |
||
220 | * @param string $name |
||
221 | * @param mixed $value |
||
222 | */ |
||
223 | 8 | public function __set($name, $value) |
|
224 | { |
||
225 | 8 | $this->dataSet = true; |
|
226 | 8 | $this->modelData[Text::deCamelize($name)] = $value; |
|
227 | 8 | } |
|
228 | |||
229 | 12 | public function __get($name) |
|
230 | { |
||
231 | 12 | return $this->retrieveItem($name); |
|
232 | } |
||
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 | 4 | public function postSaveCallback($id) |
|
399 | { |
||
400 | |||
401 | 4 | } |
|
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 | 4 | private function expandArrayValue($array, $relationships, $depth, $expandableModels = [], $index = null) |
|
435 | { |
||
436 | 4 | if (empty($expandableModels)) { |
|
437 | 4 | foreach ($relationships as $name => $relationship) { |
|
438 | 4 | $array[$name] = $this->fetchRelatedFields($relationship, $index)->toArray($depth); |
|
439 | } |
||
440 | } else { |
||
447 | |||
448 | 16 | public function toArray($depth = 0, $expandableModels = []) |
|
463 | |||
464 | } |
||
465 |
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.