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 |
||
| 31 | class RecordWrapper implements \ArrayAccess, \Countable, \Iterator |
||
| 32 | { |
||
| 33 | use \ntentan\utils\DependencyInjector; |
||
| 34 | |||
| 35 | protected $hasMany = []; |
||
| 36 | protected $belongsTo = []; |
||
| 37 | protected $manyHaveMany = []; |
||
| 38 | protected $behaviours = []; |
||
| 39 | |||
| 40 | protected $table; |
||
| 41 | private $modelData = []; |
||
| 42 | private $invalidFields; |
||
| 43 | private $dynamicOperations; |
||
| 44 | private $index = 0; |
||
| 45 | private $dataSet = false; |
||
| 46 | private $adapter; |
||
| 47 | |||
| 48 | 36 | public function __construct() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * |
||
| 64 | * @return \ntentan\nibii\DriverAdapter |
||
| 65 | */ |
||
| 66 | 34 | protected function getDataAdapter() |
|
| 67 | { |
||
| 68 | 34 | if(!$this->adapter) |
|
| 69 | 34 | { |
|
| 70 | 34 | $this->adapter = DriverAdapter::getDefaultInstance(); |
|
| 71 | 34 | } |
|
| 72 | 34 | return $this->adapter; |
|
| 73 | } |
||
| 74 | |||
| 75 | protected function getDriver() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * |
||
| 82 | * @return ModelDescription |
||
| 83 | */ |
||
| 84 | 28 | public function getDescription() |
|
| 94 | |||
| 95 | 10 | public function count() |
|
| 96 | { |
||
| 97 | 10 | if ($this->dataSet) { |
|
| 98 | 10 | return count($this->getData()); |
|
| 99 | } else { |
||
| 100 | return $this->__call('count', []); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | 12 | private function retrieveItem($key) |
|
| 105 | { |
||
| 106 | 12 | $relationships = $this->getDescription()->getRelationships(); |
|
| 107 | 12 | if(isset($relationships[$key])) { |
|
| 108 | 8 | return $this->fetchRelatedFields($relationships[$key]); |
|
| 109 | } else { |
||
| 110 | 4 | return isset($this->modelData[$key]) ? $this->modelData[$key] : null; |
|
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Create a new instance of this Model |
||
| 116 | * @return \ntentan\nibii\RecordWrapper |
||
| 117 | */ |
||
| 118 | 34 | public static function createNew() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @method |
||
| 126 | * @param type $name |
||
| 127 | * @param type $arguments |
||
| 128 | * @return type |
||
| 129 | */ |
||
| 130 | 34 | public function __call($name, $arguments) |
|
| 138 | |||
| 139 | 26 | public static function __callStatic($name, $arguments) |
|
| 143 | |||
| 144 | 8 | public function __set($name, $value) |
|
| 149 | |||
| 150 | 12 | public function __get($name) |
|
| 151 | { |
||
| 152 | 12 | return $this->retrieveItem($name); |
|
| 153 | } |
||
| 154 | |||
| 155 | 32 | public function getTable() |
|
| 159 | |||
| 160 | 4 | private function expandArrayValue($array, $relationships, $depth, $index = null) |
|
| 161 | 2 | { |
|
| 162 | 4 | foreach($relationships as $name => $relationship) { |
|
| 163 | 4 | $array[$name] = $this->fetchRelatedFields($relationship, $index)->toArray($depth); |
|
| 164 | 4 | } |
|
| 165 | 4 | return $array; |
|
| 166 | } |
||
| 167 | |||
| 168 | 16 | public function toArray($depth = 0) |
|
| 169 | { |
||
| 170 | 16 | $relationships = $this->getDescription()->getRelationships(); |
|
| 171 | 16 | $array = $this->modelData; |
|
| 172 | 16 | if($depth > 0) { |
|
| 173 | 4 | if($this->hasMultipleData()) { |
|
| 174 | foreach($array as $i => $value) { |
||
| 175 | $array[$i] = $this->expandArrayValue($value, $relationships, $depth - 1, $i); |
||
| 176 | } |
||
| 177 | } else { |
||
| 178 | 4 | $array = $this->expandArrayValue($array, $relationships, $depth - 1); |
|
| 179 | } |
||
| 180 | 4 | } |
|
| 181 | 16 | return $array; |
|
| 182 | } |
||
| 183 | |||
| 184 | 10 | public function save() |
|
| 185 | { |
||
| 186 | 10 | $return = $this->__call('save', [$this->hasMultipleData()]); |
|
| 187 | 10 | $this->invalidFields = $this->dynamicOperations->getInvalidFields(); |
|
| 188 | 10 | return $return; |
|
| 189 | } |
||
| 190 | |||
| 191 | 18 | private function hasMultipleData() |
|
| 199 | |||
| 200 | 14 | public function getData() |
|
| 201 | { |
||
| 202 | 14 | $data = []; |
|
| 203 | |||
| 204 | 14 | if(count($this->modelData) == 0) { |
|
| 205 | 6 | $data = $this->modelData; |
|
| 206 | 14 | } else if($this->hasMultipleData()) { |
|
| 207 | 2 | $data = $this->modelData; |
|
| 208 | 12 | } else if(count($this->modelData) > 0) { |
|
| 209 | 12 | $data[] = $this->modelData; |
|
| 210 | 12 | } |
|
| 211 | |||
| 212 | 14 | return $data; |
|
| 213 | } |
||
| 214 | |||
| 215 | 26 | public function setData($data) |
|
| 216 | { |
||
| 217 | 26 | $this->dataSet = true; |
|
| 218 | 26 | $this->modelData = $data; |
|
| 219 | 26 | } |
|
| 220 | |||
| 221 | public function mergeData($data) |
||
| 228 | |||
| 229 | 2 | public function offsetExists($offset) |
|
| 230 | { |
||
| 231 | 2 | return isset($this->modelData[$offset]); |
|
| 232 | } |
||
| 233 | |||
| 234 | 2 | public function offsetGet($offset) |
|
| 235 | { |
||
| 236 | 2 | if (is_numeric($offset)) { |
|
| 237 | 2 | return $this->wrap($offset); |
|
| 238 | } else { |
||
| 239 | 2 | return $this->retrieveItem($offset); |
|
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | 2 | public function offsetSet($offset, $value) |
|
| 248 | |||
| 249 | public function offsetUnset($offset) |
||
| 253 | |||
| 254 | 6 | private function wrap($offset) |
|
| 255 | { |
||
| 256 | 6 | if(isset($this->modelData[$offset])) { |
|
| 257 | 6 | $newInstance = $this->createNew(); |
|
| 258 | 6 | $newInstance->setData($this->modelData[$offset]); |
|
| 259 | 6 | return $newInstance; |
|
| 260 | } else { |
||
| 261 | return null; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | 4 | public function getInvalidFields() |
|
| 266 | { |
||
| 267 | 4 | return $this->invalidFields; |
|
| 268 | } |
||
| 269 | |||
| 270 | public function getHasMany() |
||
| 274 | |||
| 275 | public function getBelongsTo() |
||
| 279 | |||
| 280 | 4 | public function current() |
|
| 281 | { |
||
| 282 | 4 | return $this->wrap($this->index); |
|
| 283 | } |
||
| 284 | |||
| 285 | public function key() |
||
| 289 | |||
| 290 | 4 | public function next() |
|
| 291 | { |
||
| 292 | 4 | $this->index++; |
|
| 293 | 4 | } |
|
| 294 | |||
| 295 | 4 | public function rewind() |
|
| 296 | { |
||
| 297 | 4 | $this->index = 0; |
|
| 298 | 4 | } |
|
| 299 | |||
| 300 | 4 | public function valid() |
|
| 301 | { |
||
| 302 | 4 | return isset($this->modelData[$this->index]); |
|
| 303 | } |
||
| 304 | |||
| 305 | 12 | private function fetchRelatedFields($relationship, $index = null) |
|
| 306 | { |
||
| 307 | 12 | if($index === null) { |
|
| 308 | 12 | $data = $this->modelData; |
|
| 309 | 12 | } else { |
|
| 310 | $data = $this->modelData[$index]; |
||
| 311 | } |
||
| 312 | 12 | $model = $relationship->getModelInstance(); |
|
| 313 | 12 | if(empty($data)) { |
|
| 314 | return $model; |
||
| 315 | } else { |
||
| 316 | 12 | return $model->fetch($relationship->getQuery($data)); |
|
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | 8 | public function getRelationships() |
|
| 328 | |||
| 329 | public function usetField($field) |
||
| 333 | |||
| 334 | 8 | public function preSaveCallback() |
|
| 335 | { |
||
| 336 | |||
| 337 | 8 | } |
|
| 338 | |||
| 339 | 4 | public function postSaveCallback($id) |
|
| 340 | { |
||
| 341 | |||
| 342 | 4 | } |
|
| 343 | |||
| 344 | 2 | public function preUpdateCallback() |
|
| 345 | { |
||
| 346 | |||
| 347 | 2 | } |
|
| 348 | |||
| 349 | 2 | public function postUpdateCallback() |
|
| 350 | { |
||
| 351 | |||
| 352 | 2 | } |
|
| 353 | |||
| 354 | 10 | public function getBehaviours() |
|
| 355 | { |
||
| 358 | } |
||
| 359 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: