Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class Model extends Component |
||
| 27 | { |
||
| 28 | |||
| 29 | use traits\Model; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var BaseModel[] |
||
| 33 | */ |
||
| 34 | protected $cacheAll; |
||
| 35 | |||
| 36 | |||
| 37 | /******************************************* |
||
| 38 | * MODEL CLASSES |
||
| 39 | *******************************************/ |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | abstract public static function modelClass(): string; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | public static function modelClassInstance(): string |
||
| 53 | |||
| 54 | /******************************************* |
||
| 55 | * CREATE |
||
| 56 | *******************************************/ |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param array|Record $config |
||
| 60 | * @param string|null $toScenario |
||
| 61 | * @throws InvalidConfigException |
||
| 62 | * @return BaseModel |
||
| 63 | */ |
||
| 64 | public function create($config = [], string $toScenario = null): BaseModel |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param Record $record |
||
| 89 | * @param string|null $toScenario |
||
| 90 | * @throws InvalidConfigException |
||
| 91 | * @return BaseModel |
||
| 92 | */ |
||
| 93 | protected function createFromRecord(Record $record, string $toScenario = null): BaseModel |
||
| 111 | |||
| 112 | /******************************************* |
||
| 113 | * FIND/GET ALL |
||
| 114 | *******************************************/ |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $toScenario |
||
| 118 | * @return BaseModel[] |
||
| 119 | */ |
||
| 120 | public function findAll(string $toScenario = null) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string $toScenario |
||
| 140 | * @return BaseModel[] |
||
| 141 | * @throws ModelNotFoundException |
||
| 142 | */ |
||
| 143 | public function getAll(string $toScenario = null): array |
||
| 152 | |||
| 153 | /******************************************* |
||
| 154 | * FIND/GET |
||
| 155 | *******************************************/ |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param $identifier |
||
| 159 | * @param string $toScenario |
||
| 160 | * @return BaseModel|null |
||
| 161 | */ |
||
| 162 | public function find($identifier, string $toScenario = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param $identifier |
||
| 182 | * @param string $toScenario |
||
| 183 | * @return BaseModel |
||
| 184 | * @throws ModelNotFoundException |
||
| 185 | */ |
||
| 186 | public function get($identifier, string $toScenario = null): BaseModel |
||
| 196 | |||
| 197 | /******************************************* |
||
| 198 | * FIND/GET BY QUERY |
||
| 199 | *******************************************/ |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param QueryInterface $query |
||
| 203 | * @param string $toScenario |
||
| 204 | * @return BaseModel[] |
||
| 205 | */ |
||
| 206 | public function findAllByQuery(QueryInterface $query, string $toScenario = null): array |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param QueryInterface $query |
||
| 220 | * @param string $toScenario |
||
| 221 | * @return BaseModel|null |
||
| 222 | */ |
||
| 223 | public function findByQuery(QueryInterface $query, string $toScenario = null) |
||
| 233 | |||
| 234 | /******************************************* |
||
| 235 | * FIND/GET BY CONDITION |
||
| 236 | *******************************************/ |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param $condition |
||
| 240 | * @param string $toScenario |
||
| 241 | * @return BaseModel[] |
||
| 242 | */ |
||
| 243 | public function findAllByCondition($condition, string $toScenario = null): array |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param $condition |
||
| 260 | * @param string $toScenario |
||
| 261 | * @return BaseModel[] |
||
| 262 | * @throws ModelNotFoundException |
||
| 263 | */ |
||
| 264 | public function getAllByCondition($condition, string $toScenario = null): array |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param $condition |
||
| 276 | * @param string $toScenario |
||
| 277 | * @return BaseModel|null |
||
| 278 | */ |
||
| 279 | public function findByCondition($condition, string $toScenario = null) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $condition |
||
| 292 | * @param string $toScenario |
||
| 293 | * @return BaseModel |
||
| 294 | * @throws ModelNotFoundException |
||
| 295 | */ |
||
| 296 | public function getByCondition($condition, string $toScenario = null): BaseModel |
||
| 305 | |||
| 306 | /******************************************* |
||
| 307 | * FIND/GET BY CRITERIA |
||
| 308 | *******************************************/ |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param $criteria |
||
| 312 | * @param string $toScenario |
||
| 313 | * @return BaseModel[] |
||
| 314 | */ |
||
| 315 | public function findAllByCriteria($criteria, string $toScenario = null): array |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param $criteria |
||
| 333 | * @param string $toScenario |
||
| 334 | * @return BaseModel[] |
||
| 335 | * @throws ModelNotFoundException |
||
| 336 | */ |
||
| 337 | public function getAllByCriteria($criteria, string $toScenario = null): array |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param $criteria |
||
| 349 | * @param string $toScenario |
||
| 350 | * @return BaseModel|null |
||
| 351 | */ |
||
| 352 | public function findByCriteria($criteria, string $toScenario = null) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param $criteria |
||
| 365 | * @param string $toScenario |
||
| 366 | * @return BaseModel |
||
| 367 | * @throws ModelNotFoundException |
||
| 368 | */ |
||
| 369 | public function getByCriteria($criteria, string $toScenario = null): BaseModel |
||
| 378 | |||
| 379 | |||
| 380 | /******************************************* |
||
| 381 | * FIND/GET BY RECORD |
||
| 382 | *******************************************/ |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param Record $record |
||
| 386 | * @param string $toScenario |
||
| 387 | * @return BaseModel |
||
| 388 | */ |
||
| 389 | public function findByRecord(Record $record, string $toScenario = null): BaseModel |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param Record $record |
||
| 406 | * @param string $toScenario |
||
| 407 | * @return BaseModel |
||
| 408 | */ |
||
| 409 | public function getByRecord(Record $record, string $toScenario = null): BaseModel |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param BaseModel $model |
||
| 416 | * @return Record|null |
||
| 417 | */ |
||
| 418 | public function findRecordByModel(BaseModel $model) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param BaseModel $model |
||
| 425 | * @return Record |
||
| 426 | * @throws RecordNotFoundException |
||
| 427 | */ |
||
| 428 | public function getRecordByModel(BaseModel $model): Record |
||
| 437 | |||
| 438 | /******************************************* |
||
| 439 | * CACHE |
||
| 440 | *******************************************/ |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param $identifier |
||
| 444 | * @return BaseModel|null |
||
| 445 | */ |
||
| 446 | public function findCache($identifier) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param Record $record |
||
| 458 | * @return BaseModel|null |
||
| 459 | */ |
||
| 460 | public function findCacheByRecord(Record $record) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param BaseModel $model |
||
| 467 | * @return static |
||
| 468 | */ |
||
| 469 | public function addToCache(BaseModel $model) |
||
| 473 | |||
| 474 | |||
| 475 | /******************************************* |
||
| 476 | * EXCEPTIONS |
||
| 477 | *******************************************/ |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @throws ModelNotFoundException |
||
| 481 | */ |
||
| 482 | protected function notFoundException() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param null $criteria |
||
| 494 | * @throws ModelNotFoundException |
||
| 495 | */ |
||
| 496 | protected function notFoundByCriteriaException($criteria = null) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param null $condition |
||
| 509 | * @throws ModelNotFoundException |
||
| 510 | */ |
||
| 511 | protected function notFoundByConditionException($condition = null) |
||
| 521 | } |
||
| 522 |
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: