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 |
||
32 | class RecordWrapper implements \ArrayAccess, \Countable, \Iterator |
||
33 | { |
||
34 | use \ntentan\panie\ComponentContainerTrait; |
||
35 | |||
36 | protected $hasMany = []; |
||
37 | protected $belongsTo = []; |
||
38 | protected $manyHaveMany = []; |
||
39 | protected $behaviours = []; |
||
40 | protected $table; |
||
41 | private $modelData = []; |
||
42 | private $invalidFields; |
||
43 | private $dynamicOperations; |
||
44 | private $index = 0; |
||
45 | private $dataSet = false; |
||
46 | protected $adapter; |
||
47 | |||
48 | 36 | public function __construct(DriverAdapter $adapter) |
|
57 | |||
58 | /** |
||
59 | 36 | * |
|
60 | 36 | * @return \ntentan\nibii\DriverAdapter |
|
61 | */ |
||
62 | //protected function getDataAdapter() |
||
|
|||
63 | //{ |
||
64 | |||
65 | /*if(!$this->adapter) |
||
66 | 34 | { |
|
67 | $this->adapter = DriverAdapter::getDefaultInstance(); |
||
68 | 34 | } |
|
69 | 34 | return $this->adapter;*/ |
|
70 | 34 | //} |
|
71 | 34 | ||
72 | 34 | /** |
|
73 | * |
||
74 | * @return ModelDescription |
||
75 | */ |
||
76 | public function getDescription() |
||
86 | |||
87 | 28 | public function count() |
|
95 | |||
96 | private function retrieveItem($key) |
||
105 | 4 | ||
106 | /** |
||
107 | * Create a new instance of this Model |
||
108 | * @return \ntentan\nibii\RecordWrapper |
||
109 | */ |
||
110 | public static function createNew() |
||
115 | 34 | ||
116 | 34 | /** |
|
117 | * @method |
||
118 | * @param type $name |
||
119 | * @param type $arguments |
||
120 | * @return type |
||
121 | */ |
||
122 | public function __call($name, $arguments) |
||
129 | 34 | ||
130 | public static function __callStatic($name, $arguments) |
||
134 | 26 | ||
135 | public function __set($name, $value) |
||
140 | |||
141 | 8 | public function __get($name) |
|
145 | 12 | ||
146 | public function getTable() |
||
150 | 32 | ||
151 | private function expandArrayValue($array, $relationships, $depth, $index = null) |
||
158 | 4 | ||
159 | 4 | public function toArray($depth = 0) |
|
174 | |||
175 | 4 | public function save() |
|
181 | 10 | ||
182 | 10 | public function isValid() |
|
186 | 18 | ||
187 | private function hasMultipleData() |
||
195 | 14 | ||
196 | public function getData() |
||
210 | 26 | ||
211 | public function setData($data) |
||
216 | |||
217 | public function mergeData($data) |
||
224 | 2 | ||
225 | public function offsetExists($offset) |
||
229 | 2 | ||
230 | public function offsetGet($offset) |
||
238 | 2 | ||
239 | public function offsetSet($offset, $value) |
||
244 | |||
245 | public function offsetUnset($offset) |
||
249 | 6 | ||
250 | private function wrap($offset) |
||
260 | 4 | ||
261 | public function getInvalidFields() |
||
265 | |||
266 | public function getHasMany() |
||
270 | |||
271 | public function getBelongsTo() |
||
275 | 4 | ||
276 | public function current() |
||
280 | |||
281 | public function key() |
||
285 | 4 | ||
286 | public function next() |
||
290 | 4 | ||
291 | public function rewind() |
||
295 | 4 | ||
296 | public function valid() |
||
300 | 12 | ||
301 | private function fetchRelatedFields($relationship, $index = null) |
||
315 | 8 | ||
316 | public function getRelationships() |
||
324 | |||
325 | public function usetField($field) |
||
329 | 8 | ||
330 | public function preSaveCallback() |
||
334 | 4 | ||
335 | public function postSaveCallback($id) |
||
339 | 2 | ||
340 | public function preUpdateCallback() |
||
344 | 2 | ||
345 | public function postUpdateCallback() |
||
349 | 10 | ||
350 | public function getBehaviours() |
||
354 | } |
||
355 |
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.