Complex classes like Object 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 Object, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | abstract class Object extends Component |
||
| 26 | { |
||
| 27 | |||
| 28 | use traits\Object; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var BaseObject[] |
||
| 32 | */ |
||
| 33 | protected $cacheAll; |
||
| 34 | |||
| 35 | /******************************************* |
||
| 36 | * OBJECT CLASSES |
||
| 37 | *******************************************/ |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | abstract public static function objectClass(): string; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public static function objectClassInstance(): string |
||
| 51 | |||
| 52 | /******************************************* |
||
| 53 | * CREATE |
||
| 54 | *******************************************/ |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param array|Record $config |
||
| 58 | * @param string|null $toScenario |
||
| 59 | * @throws InvalidConfigException |
||
| 60 | * @return BaseObject |
||
| 61 | */ |
||
| 62 | public function create($config = [], string $toScenario = null): BaseObject |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param Record $record |
||
| 88 | * @param string|null $toScenario |
||
| 89 | * @throws InvalidConfigException |
||
| 90 | * @return BaseObject |
||
| 91 | */ |
||
| 92 | protected function createFromRecord(Record $record, string $toScenario = null): BaseObject |
||
| 111 | |||
| 112 | |||
| 113 | /******************************************* |
||
| 114 | * FIND/GET ALL |
||
| 115 | *******************************************/ |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $toScenario |
||
| 119 | * @return BaseObject[] |
||
| 120 | */ |
||
| 121 | public function findAll(string $toScenario = null) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $toScenario |
||
| 141 | * @return BaseObject[] |
||
| 142 | * @throws ObjectNotFoundException |
||
| 143 | */ |
||
| 144 | public function getAll(string $toScenario = null): array |
||
| 153 | |||
| 154 | /******************************************* |
||
| 155 | * FIND/GET |
||
| 156 | *******************************************/ |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param $identifier |
||
| 160 | * @param string $toScenario |
||
| 161 | * @return BaseObject|null |
||
| 162 | */ |
||
| 163 | public function find($identifier, string $toScenario = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param $identifier |
||
| 179 | * @param string $toScenario |
||
| 180 | * @return BaseObject |
||
| 181 | * @throws ObjectNotFoundException |
||
| 182 | */ |
||
| 183 | public function get($identifier, string $toScenario = null): BaseObject |
||
| 193 | |||
| 194 | /******************************************* |
||
| 195 | * FIND/GET BY QUERY |
||
| 196 | *******************************************/ |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param QueryInterface $query |
||
| 200 | * @param string $toScenario |
||
| 201 | * @return BaseObject[] |
||
| 202 | */ |
||
| 203 | public function findAllByQuery(QueryInterface $query, string $toScenario = null): array |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param QueryInterface $query |
||
| 217 | * @param string $toScenario |
||
| 218 | * @return BaseObject|null |
||
| 219 | */ |
||
| 220 | public function findByQuery(QueryInterface $query, string $toScenario = null) |
||
| 230 | |||
| 231 | /******************************************* |
||
| 232 | * FIND/GET BY CONDITION |
||
| 233 | *******************************************/ |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param $condition |
||
| 237 | * @param string $toScenario |
||
| 238 | * @return BaseObject[] |
||
| 239 | */ |
||
| 240 | public function findAllByCondition($condition, string $toScenario = null): array |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param $condition |
||
| 257 | * @param string $toScenario |
||
| 258 | * @return BaseObject[] |
||
| 259 | * @throws ObjectNotFoundException |
||
| 260 | */ |
||
| 261 | public function getAllByCondition($condition, string $toScenario = null): array |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param $condition |
||
| 273 | * @param string $toScenario |
||
| 274 | * @return BaseObject|null |
||
| 275 | */ |
||
| 276 | public function findByCondition($condition, string $toScenario = null) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param $condition |
||
| 289 | * @param string $toScenario |
||
| 290 | * @return BaseObject |
||
| 291 | * @throws ObjectNotFoundException |
||
| 292 | */ |
||
| 293 | public function getByCondition($condition, string $toScenario = null): BaseObject |
||
| 302 | |||
| 303 | /******************************************* |
||
| 304 | * FIND/GET BY CRITERIA |
||
| 305 | *******************************************/ |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param $criteria |
||
| 309 | * @param string $toScenario |
||
| 310 | * @return BaseObject[] |
||
| 311 | */ |
||
| 312 | public function findAllByCriteria($criteria, string $toScenario = null): array |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param $criteria |
||
| 330 | * @param string $toScenario |
||
| 331 | * @return BaseObject[] |
||
| 332 | * @throws ObjectNotFoundException |
||
| 333 | */ |
||
| 334 | public function getAllByCriteria($criteria, string $toScenario = null): array |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param $criteria |
||
| 346 | * @param string $toScenario |
||
| 347 | * @return BaseObject|null |
||
| 348 | */ |
||
| 349 | public function findByCriteria($criteria, string $toScenario = null) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param $criteria |
||
| 362 | * @param string $toScenario |
||
| 363 | * @return BaseObject |
||
| 364 | * @throws ObjectNotFoundException |
||
| 365 | */ |
||
| 366 | public function getByCriteria($criteria, string $toScenario = null): BaseObject |
||
| 375 | |||
| 376 | |||
| 377 | /******************************************* |
||
| 378 | * FIND/GET BY RECORD |
||
| 379 | *******************************************/ |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param Record $record |
||
| 383 | * @param string $toScenario |
||
| 384 | * @return BaseObject |
||
| 385 | */ |
||
| 386 | public function findByRecord(Record $record, string $toScenario = null): BaseObject |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param Record $record |
||
| 403 | * @param string $toScenario |
||
| 404 | * @return BaseObject |
||
| 405 | */ |
||
| 406 | public function getByRecord(Record $record, string $toScenario = null): BaseObject |
||
| 410 | |||
| 411 | |||
| 412 | /******************************************* |
||
| 413 | * CACHE |
||
| 414 | *******************************************/ |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param $identifier |
||
| 418 | * @return BaseObject|null |
||
| 419 | */ |
||
| 420 | public function findCache($identifier) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param Record $record |
||
| 432 | * @return BaseObject|null |
||
| 433 | */ |
||
| 434 | public function findCacheByRecord(Record $record) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param BaseObject $object |
||
| 441 | * @return static |
||
| 442 | */ |
||
| 443 | public function addToCache(BaseObject $object) |
||
| 447 | |||
| 448 | |||
| 449 | /******************************************* |
||
| 450 | * EXCEPTIONS |
||
| 451 | *******************************************/ |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @throws ObjectNotFoundException |
||
| 455 | */ |
||
| 456 | protected function notFoundException() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param null $criteria |
||
| 468 | * @throws ObjectNotFoundException |
||
| 469 | */ |
||
| 470 | protected function notFoundByCriteriaException($criteria = null) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param null $condition |
||
| 483 | * @throws ObjectNotFoundException |
||
| 484 | */ |
||
| 485 | protected function notFoundByConditionException($condition = null) |
||
| 495 | } |
||
| 496 |
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: