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 | * |
||
| 60 | * @return ModelDescription |
||
| 61 | */ |
||
| 62 | 28 | public function getDescription() |
|
| 63 | { |
||
| 64 | 28 | return Cache::read( |
|
| 65 | 28 | (new \ReflectionClass($this))->getName() . '::desc', |
|
| 66 | 28 | function() |
|
| 67 | { |
||
| 68 | 28 | return new ModelDescription($this); |
|
| 69 | 28 | } |
|
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | 10 | public function count() |
|
| 81 | |||
| 82 | 12 | private function retrieveItem($key) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Create a new instance of this Model |
||
| 94 | * @return \ntentan\nibii\RecordWrapper |
||
| 95 | */ |
||
| 96 | 34 | public static function createNew() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @method |
||
| 104 | * @param type $name |
||
| 105 | * @param type $arguments |
||
| 106 | * @return type |
||
| 107 | */ |
||
| 108 | 34 | public function __call($name, $arguments) |
|
| 117 | |||
| 118 | 26 | public static function __callStatic($name, $arguments) |
|
| 122 | |||
| 123 | 8 | public function __set($name, $value) |
|
| 128 | |||
| 129 | 12 | public function __get($name) |
|
| 133 | |||
| 134 | 36 | public function getTable() |
|
| 138 | |||
| 139 | 4 | private function expandArrayValue($array, $relationships, $depth, $index = null) |
|
| 146 | |||
| 147 | 16 | public function toArray($depth = 0) |
|
| 162 | |||
| 163 | 10 | public function save() |
|
| 169 | |||
| 170 | 18 | private function hasMultipleData() |
|
| 171 | { |
||
| 172 | 18 | if(count($this->modelData) > 0) { |
|
| 173 | 16 | return is_numeric(array_keys($this->modelData)[0]); |
|
| 174 | } else { |
||
| 175 | 2 | return false; |
|
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | 14 | public function getData() |
|
| 180 | { |
||
| 181 | 14 | $data = []; |
|
| 182 | |||
| 183 | 14 | if(count($this->modelData) == 0) { |
|
| 184 | 6 | $data = $this->modelData; |
|
| 185 | 12 | } else if($this->hasMultipleData()) { |
|
| 186 | 2 | $data = $this->modelData; |
|
| 187 | 12 | } else if(count($this->modelData) > 0) { |
|
| 188 | 12 | $data[] = $this->modelData; |
|
| 189 | } |
||
| 190 | |||
| 191 | 14 | return $data; |
|
| 192 | } |
||
| 193 | |||
| 194 | 26 | public function setData($data) |
|
| 195 | { |
||
| 196 | 26 | $this->dataSet = true; |
|
| 197 | 26 | $this->modelData = $data; |
|
| 198 | 26 | } |
|
| 199 | |||
| 200 | public function mergeData($data) |
||
| 201 | { |
||
| 202 | foreach($data as $key => $value) { |
||
| 203 | $this->modelData[$key] = $value; |
||
| 204 | } |
||
| 205 | $this->dataSet = true; |
||
| 206 | } |
||
| 207 | |||
| 208 | 2 | public function offsetExists($offset) |
|
| 209 | { |
||
| 210 | 2 | return isset($this->modelData[$offset]); |
|
| 211 | } |
||
| 212 | |||
| 213 | 2 | public function offsetGet($offset) |
|
| 214 | { |
||
| 215 | 2 | if (is_numeric($offset)) { |
|
| 216 | 2 | return $this->wrap($offset); |
|
| 217 | } else { |
||
| 218 | 2 | return $this->retrieveItem($offset); |
|
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | 2 | public function offsetSet($offset, $value) |
|
| 223 | { |
||
| 224 | 2 | $this->dataSet = true; |
|
| 225 | 2 | $this->modelData[$offset] = $value; |
|
| 226 | 2 | } |
|
| 227 | |||
| 228 | public function offsetUnset($offset) |
||
| 229 | { |
||
| 230 | unset($this->modelData[$offset]); |
||
| 231 | } |
||
| 232 | |||
| 233 | 6 | private function wrap($offset) |
|
| 234 | { |
||
| 235 | 6 | if(isset($this->modelData[$offset])) { |
|
| 236 | 6 | $newInstance = $this->createNew(); |
|
| 237 | 6 | $newInstance->setData($this->modelData[$offset]); |
|
| 238 | 6 | return $newInstance; |
|
| 239 | } else { |
||
| 240 | return null; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | 4 | public function getInvalidFields() |
|
| 245 | { |
||
| 246 | 4 | return $this->invalidFields; |
|
| 247 | } |
||
| 248 | |||
| 249 | public function getHasMany() |
||
| 250 | { |
||
| 251 | return $this->hasMany; |
||
| 252 | } |
||
| 253 | |||
| 254 | public function getBelongsTo() |
||
| 255 | { |
||
| 256 | return $this->belongsTo; |
||
| 257 | } |
||
| 258 | |||
| 259 | 4 | public function current() |
|
| 260 | { |
||
| 261 | 4 | return $this->wrap($this->index); |
|
| 262 | } |
||
| 263 | |||
| 264 | public function key() |
||
| 265 | { |
||
| 266 | return $this->index; |
||
| 267 | } |
||
| 268 | |||
| 269 | 4 | public function next() |
|
| 270 | { |
||
| 271 | 4 | $this->index++; |
|
| 272 | 4 | } |
|
| 273 | |||
| 274 | 4 | public function rewind() |
|
| 275 | { |
||
| 276 | 4 | $this->index = 0; |
|
| 277 | 4 | } |
|
| 278 | |||
| 279 | 4 | public function valid() |
|
| 280 | { |
||
| 281 | 4 | return isset($this->modelData[$this->index]); |
|
| 282 | } |
||
| 283 | |||
| 284 | 6 | public function onValidate() |
|
| 285 | { |
||
| 286 | 6 | return true; |
|
| 287 | } |
||
| 288 | |||
| 289 | 12 | private function fetchRelatedFields($relationship, $index = null) |
|
| 303 | |||
| 304 | 28 | public function getRelationships() |
|
| 312 | |||
| 313 | public function usetField($field) |
||
| 317 | |||
| 318 | 8 | public function preSaveCallback() |
|
| 322 | |||
| 323 | 4 | public function postSaveCallback($id) |
|
| 327 | |||
| 328 | 2 | public function preUpdateCallback() |
|
| 332 | |||
| 333 | 2 | public function postUpdateCallback() |
|
| 337 | |||
| 338 | 10 | public function getBehaviours() |
|
| 342 | } |
||
| 343 |
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: