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) |
|
49 | { |
||
50 | 36 | $this->table = Nibii::getModelTable($this); |
|
51 | 36 | $this->adapter = $adapter; |
|
52 | 36 | foreach($this->behaviours as $behaviour) { |
|
53 | $behaviourInstance = $this->getComponentInstance($behaviour); |
||
54 | $behaviourInstance->setModel($this); |
||
55 | } |
||
56 | 36 | } |
|
57 | |||
58 | /** |
||
59 | * |
||
60 | * @return \ntentan\nibii\DriverAdapter |
||
61 | */ |
||
62 | //protected function getDataAdapter() |
||
|
|||
63 | //{ |
||
64 | |||
65 | /*if(!$this->adapter) |
||
66 | { |
||
67 | $this->adapter = DriverAdapter::getDefaultInstance(); |
||
68 | } |
||
69 | return $this->adapter;*/ |
||
70 | //} |
||
71 | |||
72 | /** |
||
73 | * |
||
74 | * @return ModelDescription |
||
75 | */ |
||
76 | 28 | public function getDescription() |
|
77 | { |
||
78 | 28 | return Cache::read( |
|
79 | 28 | (new \ReflectionClass($this))->getName() . '::desc', |
|
80 | 28 | function() |
|
81 | { |
||
82 | 28 | return new ModelDescription($this); |
|
83 | 28 | } |
|
84 | ); |
||
85 | } |
||
86 | |||
87 | 10 | public function count() |
|
88 | { |
||
89 | 10 | if ($this->dataSet) { |
|
90 | 10 | return count($this->getData()); |
|
91 | } else { |
||
92 | return $this->__call('count', []); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | 12 | private function retrieveItem($key) |
|
97 | { |
||
98 | 12 | $relationships = $this->getDescription()->getRelationships(); |
|
99 | 12 | if(isset($relationships[$key])) { |
|
100 | 8 | return $this->fetchRelatedFields($relationships[$key]); |
|
101 | } else { |
||
102 | 4 | return isset($this->modelData[$key]) ? $this->modelData[$key] : null; |
|
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Create a new instance of this Model |
||
108 | * @return \ntentan\nibii\RecordWrapper |
||
109 | */ |
||
110 | 34 | public static function createNew() |
|
111 | { |
||
112 | 34 | $class = get_called_class(); |
|
113 | 34 | return InjectionContainer::resolve($class); |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * @method |
||
118 | * @param type $name |
||
119 | * @param type $arguments |
||
120 | * @return type |
||
121 | */ |
||
122 | 34 | public function __call($name, $arguments) |
|
123 | { |
||
124 | 34 | if($this->dynamicOperations === null) { |
|
125 | 34 | $this->dynamicOperations = new Operations($this, InjectionContainer::singleton(DriverAdapter::class)); |
|
126 | } |
||
127 | 34 | return $this->dynamicOperations->perform($name, $arguments); |
|
128 | } |
||
129 | |||
130 | 26 | public static function __callStatic($name, $arguments) |
|
131 | { |
||
132 | 26 | return call_user_func_array([self::createNew(), $name], $arguments); |
|
133 | } |
||
134 | |||
135 | 8 | public function __set($name, $value) |
|
136 | { |
||
137 | 8 | $this->dataSet = true; |
|
138 | 8 | $this->modelData[$name] = $value; |
|
139 | 8 | } |
|
140 | |||
141 | 12 | public function __get($name) |
|
142 | { |
||
143 | 12 | return $this->retrieveItem($name); |
|
144 | } |
||
145 | |||
146 | 36 | public function getTable() |
|
147 | { |
||
148 | 36 | return $this->table; |
|
149 | } |
||
150 | |||
151 | 4 | private function expandArrayValue($array, $relationships, $depth, $index = null) |
|
152 | { |
||
153 | 4 | foreach($relationships as $name => $relationship) { |
|
154 | 4 | $array[$name] = $this->fetchRelatedFields($relationship, $index)->toArray($depth); |
|
155 | } |
||
156 | 4 | return $array; |
|
157 | } |
||
158 | |||
159 | 16 | public function toArray($depth = 0) |
|
160 | { |
||
161 | 16 | $relationships = $this->getDescription()->getRelationships(); |
|
162 | 16 | $array = $this->modelData; |
|
163 | 16 | if($depth > 0) { |
|
164 | 4 | if($this->hasMultipleData()) { |
|
165 | foreach($array as $i => $value) { |
||
166 | $array[$i] = $this->expandArrayValue($value, $relationships, $depth - 1, $i); |
||
167 | } |
||
168 | } else { |
||
169 | 4 | $array = $this->expandArrayValue($array, $relationships, $depth - 1); |
|
170 | } |
||
171 | } |
||
172 | 16 | return $array; |
|
173 | } |
||
174 | |||
175 | 10 | public function save() |
|
176 | { |
||
177 | 10 | $return = $this->__call('save', [$this->hasMultipleData()]); |
|
178 | 10 | $this->invalidFields = $this->dynamicOperations->getInvalidFields(); |
|
179 | 10 | return $return; |
|
180 | } |
||
181 | |||
182 | public function isValid() |
||
183 | { |
||
184 | return $this->__call('isValid', []); |
||
185 | } |
||
186 | |||
187 | 18 | private function hasMultipleData() |
|
188 | { |
||
189 | 18 | if(count($this->modelData) > 0) { |
|
190 | 16 | return is_numeric(array_keys($this->modelData)[0]); |
|
191 | } else { |
||
192 | 2 | return false; |
|
193 | } |
||
194 | } |
||
195 | |||
196 | 14 | public function getData() |
|
210 | |||
211 | 26 | public function setData($data) |
|
212 | { |
||
213 | 26 | $this->dataSet = true; |
|
214 | 26 | $this->modelData = $data; |
|
215 | 26 | } |
|
216 | |||
217 | public function mergeData($data) |
||
218 | { |
||
219 | foreach($data as $key => $value) { |
||
220 | $this->modelData[$key] = $value; |
||
221 | } |
||
222 | $this->dataSet = true; |
||
223 | } |
||
224 | |||
225 | 2 | public function offsetExists($offset) |
|
226 | { |
||
227 | 2 | return isset($this->modelData[$offset]); |
|
228 | } |
||
229 | |||
230 | 2 | public function offsetGet($offset) |
|
231 | { |
||
232 | 2 | if (is_numeric($offset)) { |
|
233 | 2 | return $this->wrap($offset); |
|
234 | } else { |
||
235 | 2 | return $this->retrieveItem($offset); |
|
236 | } |
||
237 | } |
||
238 | |||
239 | 2 | public function offsetSet($offset, $value) |
|
240 | { |
||
241 | 2 | $this->dataSet = true; |
|
242 | 2 | $this->modelData[$offset] = $value; |
|
243 | 2 | } |
|
244 | |||
245 | public function offsetUnset($offset) |
||
246 | { |
||
247 | unset($this->modelData[$offset]); |
||
248 | } |
||
249 | |||
250 | 6 | private function wrap($offset) |
|
251 | { |
||
252 | 6 | if(isset($this->modelData[$offset])) { |
|
253 | 6 | $newInstance = $this->createNew(); |
|
254 | 6 | $newInstance->setData($this->modelData[$offset]); |
|
255 | 6 | return $newInstance; |
|
256 | } else { |
||
257 | return null; |
||
258 | } |
||
259 | } |
||
260 | |||
261 | 4 | public function getInvalidFields() |
|
262 | { |
||
263 | 4 | return $this->invalidFields; |
|
264 | } |
||
265 | |||
266 | public function getHasMany() |
||
267 | { |
||
268 | return $this->hasMany; |
||
269 | } |
||
270 | |||
271 | public function getBelongsTo() |
||
272 | { |
||
273 | return $this->belongsTo; |
||
274 | } |
||
275 | |||
276 | 4 | public function current() |
|
277 | { |
||
278 | 4 | return $this->wrap($this->index); |
|
279 | } |
||
280 | |||
281 | public function key() |
||
282 | { |
||
283 | return $this->index; |
||
284 | } |
||
285 | |||
286 | 4 | public function next() |
|
287 | { |
||
288 | 4 | $this->index++; |
|
289 | 4 | } |
|
290 | |||
291 | 4 | public function rewind() |
|
292 | { |
||
293 | 4 | $this->index = 0; |
|
294 | 4 | } |
|
295 | |||
296 | 4 | public function valid() |
|
297 | { |
||
298 | 4 | return isset($this->modelData[$this->index]); |
|
299 | } |
||
300 | |||
301 | 12 | private function fetchRelatedFields($relationship, $index = null) |
|
315 | |||
316 | 28 | public function getRelationships() |
|
324 | |||
325 | public function usetField($field) |
||
326 | { |
||
327 | unset($this->modelData[$field]); |
||
328 | } |
||
329 | |||
330 | 8 | public function preSaveCallback() |
|
331 | { |
||
332 | |||
333 | 8 | } |
|
334 | |||
335 | 4 | public function postSaveCallback($id) |
|
336 | { |
||
337 | |||
338 | 4 | } |
|
339 | |||
340 | 2 | public function preUpdateCallback() |
|
341 | { |
||
342 | |||
343 | 2 | } |
|
344 | |||
345 | 2 | public function postUpdateCallback() |
|
346 | { |
||
349 | |||
350 | 10 | 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.