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 |
||
37 | class RecordWrapper implements \ArrayAccess, \Countable, \Iterator { |
||
38 | |||
39 | protected $hasMany = []; |
||
40 | protected $belongsTo = []; |
||
41 | protected $manyHaveMany = []; |
||
42 | protected $behaviours = []; |
||
43 | protected $table; |
||
44 | protected $schema; |
||
45 | protected $modelData = []; |
||
46 | private $quotedTable; |
||
47 | private $unquotedTable; |
||
48 | private $invalidFields; |
||
49 | private $dynamicOperations; |
||
50 | private $index = 0; |
||
51 | private $dataSet = false; |
||
52 | private $className; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * @var DriverAdapter |
||
57 | */ |
||
58 | private $adapter; |
||
59 | private $container; |
||
60 | private $context; |
||
61 | private $keys = []; |
||
62 | private $initialized = false; |
||
63 | |||
64 | 36 | protected function initialize() { |
|
65 | 36 | if($this->initialized) return; |
|
66 | 36 | $this->context = ORMContext::getInstance(); |
|
67 | 36 | $this->container = $this->context->getContainer(); |
|
68 | 36 | $this->adapter = $this->container->resolve(DriverAdapter::class); |
|
69 | 36 | $table = $this->table ?? $this->context->getModelTable($this); |
|
70 | 36 | $driver = $this->context->getDbContext()->getDriver(); |
|
71 | 36 | $this->adapter->setContext($this->context); |
|
72 | 36 | $this->className = (new \ReflectionClass($this))->getName(); |
|
73 | 36 | if (is_string($table)) { |
|
74 | //$this->quotedTable = $driver->quoteIdentifier($table); |
||
|
|||
75 | 36 | $this->table = $this->unquotedTable = $table; |
|
76 | } else { |
||
77 | $this->table = $table['table']; |
||
78 | $this->schema = $table['schema']; |
||
79 | } |
||
80 | 36 | $this->quotedTable = ($this->schema ? "{$driver->quoteIdentifier($this->schema)}." : "").$driver->quoteIdentifier($this->table); |
|
81 | 36 | $this->unquotedTable = ($this->schema ? "{$this->schema}." : "").$this->table; |
|
82 | 36 | $this->adapter->setModel($this, $this->quotedTable); |
|
83 | 36 | foreach ($this->behaviours as $behaviour) { |
|
84 | $behaviourInstance = $this->getComponentInstance($behaviour); |
||
85 | $behaviourInstance->setModel($this); |
||
86 | } |
||
87 | 36 | $this->initialized = true; |
|
88 | 36 | } |
|
89 | |||
90 | public function __debugInfo() { |
||
91 | $data = $this->getData(); |
||
92 | return $this->hasMultipleItems() ? $data : isset($data[0]) ? $data[0] : []; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * |
||
97 | * @return ModelDescription |
||
98 | */ |
||
99 | 28 | public function getDescription() { |
|
100 | 28 | $this->initialize(); |
|
101 | 28 | return $this->context->getCache()->read( |
|
102 | 28 | (new \ReflectionClass($this))->getName().'::desc', function() { |
|
103 | 28 | return $this->container->resolve(ModelDescription::class, ['model' => $this]); |
|
104 | 28 | } |
|
105 | ); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Return the number of items stored in the model or the query. |
||
110 | * @return integer |
||
111 | */ |
||
112 | 14 | public function count() { |
|
119 | |||
120 | /** |
||
121 | * Retrieve an item stored in the record. |
||
122 | * This method returns items that are directly stored in the model or lazy |
||
123 | * loads related items. The key could be a field in the model's table or |
||
124 | * the name of a related model. |
||
125 | * @param string $key A key identifying the item to be retrieved. |
||
126 | * @return mixed |
||
127 | */ |
||
128 | 12 | private function retrieveItem($key) { |
|
136 | |||
137 | /** |
||
138 | * @method |
||
139 | * @param type $name |
||
140 | * @param type $arguments |
||
141 | * @return type |
||
142 | */ |
||
143 | 34 | public function __call($name, $arguments) { |
|
155 | |||
156 | 8 | public function __set($name, $value) { |
|
160 | |||
161 | 12 | public function __get($name) { |
|
164 | |||
165 | 4 | private function expandArrayValue($array, $relationships, $depth, $index = null) { |
|
171 | |||
172 | 16 | public function toArray($depth = 0) { |
|
186 | |||
187 | 10 | public function save() { |
|
192 | |||
193 | 18 | private function hasMultipleItems() { |
|
200 | |||
201 | 18 | public function getData() { |
|
214 | |||
215 | 26 | public function setData($data) { |
|
219 | |||
220 | public function mergeData($data) { |
||
226 | |||
227 | 2 | public function offsetExists($offset) { |
|
230 | |||
231 | 2 | public function offsetGet($offset) { |
|
238 | |||
239 | 2 | public function offsetSet($offset, $value) { |
|
243 | |||
244 | public function offsetUnset($offset) { |
||
247 | |||
248 | 6 | private function wrap($offset) { |
|
259 | |||
260 | 4 | public function getInvalidFields() { |
|
263 | |||
264 | public function getHasMany() { |
||
267 | |||
268 | public function getBelongsTo() { |
||
271 | |||
272 | 4 | public function current() { |
|
275 | |||
276 | public function key() { |
||
279 | |||
280 | 4 | public function next() { |
|
283 | |||
284 | 4 | public function rewind() { |
|
288 | |||
289 | 4 | public function valid() { |
|
292 | |||
293 | 10 | public function onValidate($errors) { |
|
296 | |||
297 | 12 | private function fetchRelatedFields($relationship, $index = null) { |
|
310 | |||
311 | 28 | public function getRelationships() { |
|
318 | |||
319 | public function usetField($field) { |
||
322 | |||
323 | 8 | public function preSaveCallback() { |
|
326 | |||
327 | 4 | public function postSaveCallback($id) { |
|
330 | |||
331 | 2 | public function preUpdateCallback() { |
|
334 | |||
335 | 2 | public function postUpdateCallback() { |
|
338 | |||
339 | 36 | public function getDBStoreInformation() { |
|
348 | |||
349 | /** |
||
350 | * |
||
351 | * @return DataAdapter |
||
352 | */ |
||
353 | public function getAdapter() { |
||
357 | |||
358 | } |
||
359 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.