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 | protected $table; |
||
| 40 | private $modelData = []; |
||
| 41 | private $invalidFields; |
||
| 42 | private $dynamicOperations; |
||
| 43 | private $index = 0; |
||
| 44 | private $dataSet = false; |
||
| 45 | private $adapter; |
||
| 46 | |||
| 47 | public function __construct() |
||
| 60 | 36 | ||
| 61 | /** |
||
| 62 | * |
||
| 63 | * @return \ntentan\nibii\DriverAdapter |
||
| 64 | */ |
||
| 65 | protected function getDataAdapter() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * |
||
| 76 | * @return ModelDescription |
||
| 77 | */ |
||
| 78 | public function getDescription() |
||
| 88 | |||
| 89 | public function count() |
||
| 97 | |||
| 98 | private function retrieveItem($key) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Create a new instance of this Model |
||
| 110 | * @return \ntentan\nibii\RecordWrapper |
||
| 111 | */ |
||
| 112 | public static function createNew() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @method |
||
| 120 | * @param type $name |
||
| 121 | * @param type $arguments |
||
| 122 | * @return type |
||
| 123 | */ |
||
| 124 | public function __call($name, $arguments) |
||
| 132 | |||
| 133 | public static function __callStatic($name, $arguments) |
||
| 137 | |||
| 138 | public function __set($name, $value) |
||
| 143 | 8 | ||
| 144 | public function __get($name) |
||
| 148 | |||
| 149 | public function getTable() |
||
| 153 | |||
| 154 | private function expandArrayValue($array, $relationships, $depth, $index = null) |
||
| 161 | |||
| 162 | public function toArray($depth = 0) |
||
| 177 | |||
| 178 | public function save() |
||
| 184 | |||
| 185 | public function isValid() |
||
| 189 | 16 | ||
| 190 | private function hasMultipleData() |
||
| 198 | |||
| 199 | 14 | public function getData() |
|
| 213 | 26 | ||
| 214 | 26 | public function setData($data) |
|
| 219 | |||
| 220 | public function mergeData($data) |
||
| 227 | |||
| 228 | public function offsetExists($offset) |
||
| 232 | 2 | ||
| 233 | public function offsetGet($offset) |
||
| 241 | 2 | ||
| 242 | 2 | public function offsetSet($offset, $value) |
|
| 247 | |||
| 248 | public function offsetUnset($offset) |
||
| 252 | 6 | ||
| 253 | 6 | private function wrap($offset) |
|
| 263 | |||
| 264 | public function getInvalidFields() |
||
| 268 | |||
| 269 | public function getHasMany() |
||
| 273 | |||
| 274 | public function getBelongsTo() |
||
| 278 | |||
| 279 | public function current() |
||
| 283 | |||
| 284 | public function key() |
||
| 288 | 4 | ||
| 289 | public function next() |
||
| 293 | 4 | ||
| 294 | public function rewind() |
||
| 298 | |||
| 299 | public function valid() |
||
| 303 | 12 | ||
| 304 | 12 | private function fetchRelatedFields($relationship, $index = null) |
|
| 318 | 8 | ||
| 319 | 8 | public function getRelationships() |
|
| 327 | |||
| 328 | public function usetField($field) |
||
| 332 | 8 | ||
| 333 | public function preSaveCallback() |
||
| 337 | 4 | ||
| 338 | public function postSaveCallback($id) |
||
| 342 | 2 | ||
| 343 | public function preUpdateCallback() |
||
| 347 | 2 | ||
| 348 | public function postUpdateCallback() |
||
| 352 | |||
| 353 | public function getBehaviours() |
||
| 357 | } |
||
| 358 |
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: