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 | use \ntentan\panie\ComponentContainerTrait; |
||
37 | |||
38 | protected $hasMany = []; |
||
39 | protected $belongsTo = []; |
||
40 | protected $manyHaveMany = []; |
||
41 | protected $behaviours = []; |
||
42 | protected $table; |
||
43 | protected $schema; |
||
44 | private $quotedTable; |
||
45 | private $unquotedTable; |
||
46 | private $modelData = []; |
||
47 | private $invalidFields; |
||
48 | private $dynamicOperations; |
||
49 | private $index = 0; |
||
50 | private $dataSet = false; |
||
51 | protected $adapter; |
||
52 | private $container; |
||
53 | private $context; |
||
54 | |||
55 | 37 | public function __construct(DriverAdapter $adapter, ORMContext $context) { |
|
77 | |||
78 | public function __debugInfo() { |
||
79 | return $this->getData(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * |
||
84 | * @return ModelDescription |
||
85 | */ |
||
86 | 28 | public function getDescription() { |
|
93 | |||
94 | /** |
||
95 | * Return the number of items stored in the model or the query. |
||
96 | * @return integer |
||
97 | */ |
||
98 | 14 | public function count() { |
|
99 | 14 | if ($this->dataSet) { |
|
100 | 14 | return count($this->getData()); |
|
101 | } else { |
||
102 | return $this->__call('count', []); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Retrieve an item stored in the record. |
||
108 | * This method returns items that are directly stored in the model or lazy |
||
109 | * loads related items. The key could be a field in the model's table or |
||
110 | * the name of a related model. |
||
111 | * @param string $key A key identifying the item to be retrieved. |
||
112 | * @return mixed |
||
113 | */ |
||
114 | 12 | private function retrieveItem($key) { |
|
122 | |||
123 | /** |
||
124 | * Create a new instance of this Model |
||
125 | * @return \ntentan\nibii\RecordWrapper |
||
126 | */ |
||
127 | 35 | public static function createNew() { |
|
131 | |||
132 | /** |
||
133 | * @method |
||
134 | * @param type $name |
||
135 | * @param type $arguments |
||
136 | * @return type |
||
137 | */ |
||
138 | 35 | public function __call($name, $arguments) { |
|
149 | |||
150 | 27 | public static function __callStatic($name, $arguments) { |
|
153 | |||
154 | 8 | public function __set($name, $value) { |
|
158 | |||
159 | 12 | public function __get($name) { |
|
162 | |||
163 | 4 | private function expandArrayValue($array, $relationships, $depth, $index = null) { |
|
169 | |||
170 | 16 | public function toArray($depth = 0) { |
|
184 | |||
185 | 10 | public function save() { |
|
190 | |||
191 | 18 | private function hasMultipleData() { |
|
198 | |||
199 | 18 | public function getData() { |
|
212 | |||
213 | 27 | public function setData($data) { |
|
217 | |||
218 | public function mergeData($data) { |
||
219 | foreach ($data as $key => $value) { |
||
220 | $this->modelData[$key] = $value; |
||
221 | } |
||
222 | $this->dataSet = true; |
||
223 | } |
||
224 | |||
225 | 2 | public function offsetExists($offset) { |
|
228 | |||
229 | 2 | public function offsetGet($offset) { |
|
236 | |||
237 | 2 | public function offsetSet($offset, $value) { |
|
241 | |||
242 | public function offsetUnset($offset) { |
||
243 | unset($this->modelData[$offset]); |
||
244 | } |
||
245 | |||
246 | 6 | private function wrap($offset) { |
|
255 | |||
256 | 4 | public function getInvalidFields() { |
|
259 | |||
260 | public function getHasMany() { |
||
261 | return $this->hasMany; |
||
262 | } |
||
263 | |||
264 | public function getBelongsTo() { |
||
265 | return $this->belongsTo; |
||
266 | } |
||
267 | |||
268 | 4 | public function current() { |
|
271 | |||
272 | public function key() { |
||
273 | return $this->index; |
||
274 | } |
||
275 | |||
276 | 4 | public function next() { |
|
279 | |||
280 | 4 | public function rewind() { |
|
283 | |||
284 | 4 | public function valid() { |
|
287 | |||
288 | 6 | public function onValidate() { |
|
291 | |||
292 | 12 | private function fetchRelatedFields($relationship, $index = null) { |
|
293 | 12 | if ($index === null) { |
|
294 | 12 | $data = $this->modelData; |
|
295 | } else { |
||
296 | $data = $this->modelData[$index]; |
||
297 | } |
||
298 | 12 | $model = $relationship->getModelInstance(); |
|
299 | 12 | if (empty($data)) { |
|
300 | return $model; |
||
301 | } |
||
302 | 12 | $query = $relationship->getQuery($data); |
|
303 | 12 | return $query ? $model->fetch($query) : $model; |
|
304 | } |
||
305 | |||
306 | 28 | public function getRelationships() { |
|
313 | |||
314 | public function usetField($field) { |
||
315 | unset($this->modelData[$field]); |
||
316 | } |
||
317 | |||
318 | 8 | public function preSaveCallback() { |
|
321 | |||
322 | 4 | public function postSaveCallback($id) { |
|
325 | |||
326 | 2 | public function preUpdateCallback() { |
|
329 | |||
330 | 2 | public function postUpdateCallback() { |
|
333 | |||
334 | 10 | public function getBehaviours() { |
|
337 | |||
338 | 37 | public function getDBStoreInformation() { |
|
346 | |||
347 | } |
||
348 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.