Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | public abstract 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 $config |
||
| 58 | * @throws InvalidConfigException |
||
| 59 | * @return BaseObject |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function create($config = []): 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 |
||
| 112 | |||
| 113 | |||
| 114 | /******************************************* |
||
| 115 | * FIND/GET ALL |
||
| 116 | *******************************************/ |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $toScenario |
||
| 120 | * @return BaseObject[] |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function findAll(string $toScenario = null) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $toScenario |
||
| 149 | * @return BaseObject[] |
||
| 150 | * @throws ObjectNotFoundException |
||
| 151 | */ |
||
| 152 | public function getAll(string $toScenario = null): array |
||
| 164 | |||
| 165 | /******************************************* |
||
| 166 | * FIND/GET |
||
| 167 | *******************************************/ |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param $identifier |
||
| 171 | * @param string $toScenario |
||
| 172 | * @return BaseObject|null |
||
| 173 | */ |
||
| 174 | public function find($identifier, string $toScenario = null) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param $identifier |
||
| 195 | * @param string $toScenario |
||
| 196 | * @return BaseObject |
||
| 197 | * @throws ObjectNotFoundException |
||
| 198 | */ |
||
| 199 | public function get($identifier, string $toScenario = null): BaseObject |
||
| 212 | |||
| 213 | /******************************************* |
||
| 214 | * FIND/GET BY QUERY |
||
| 215 | *******************************************/ |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param QueryInterface $query |
||
| 219 | * @param string $toScenario |
||
| 220 | * @return BaseObject[] |
||
| 221 | */ |
||
| 222 | View Code Duplication | public function findAllByQuery(QueryInterface $query, string $toScenario = null): array |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @param QueryInterface $query |
||
| 237 | * @param string $toScenario |
||
| 238 | * @return BaseObject|null |
||
| 239 | */ |
||
| 240 | View Code Duplication | public function findByQuery(QueryInterface $query, string $toScenario = null) |
|
| 251 | |||
| 252 | /******************************************* |
||
| 253 | * FIND/GET BY CONDITION |
||
| 254 | *******************************************/ |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param $condition |
||
| 258 | * @param string $toScenario |
||
| 259 | * @return BaseObject[] |
||
| 260 | */ |
||
| 261 | View Code Duplication | public function findAllByCondition($condition, string $toScenario = null): array |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param $condition |
||
| 281 | * @param string $toScenario |
||
| 282 | * @return BaseObject[] |
||
| 283 | * @throws ObjectNotFoundException |
||
| 284 | */ |
||
| 285 | public function getAllByCondition($condition, string $toScenario = null): array |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $condition |
||
| 300 | * @param string $toScenario |
||
| 301 | * @return BaseObject|null |
||
| 302 | */ |
||
| 303 | View Code Duplication | public function findByCondition($condition, string $toScenario = null) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param $condition |
||
| 317 | * @param string $toScenario |
||
| 318 | * @return BaseObject |
||
| 319 | * @throws ObjectNotFoundException |
||
| 320 | */ |
||
| 321 | public function getByCondition($condition, string $toScenario = null): BaseObject |
||
| 333 | |||
| 334 | /******************************************* |
||
| 335 | * FIND/GET BY CRITERIA |
||
| 336 | *******************************************/ |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param $criteria |
||
| 340 | * @param string $toScenario |
||
| 341 | * @return BaseObject[] |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function findAllByCriteria($criteria, string $toScenario = null): array |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @param $criteria |
||
| 364 | * @param string $toScenario |
||
| 365 | * @return BaseObject[] |
||
| 366 | * @throws ObjectNotFoundException |
||
| 367 | */ |
||
| 368 | public function getAllByCriteria($criteria, string $toScenario = null): array |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param $criteria |
||
| 383 | * @param string $toScenario |
||
| 384 | * @return BaseObject|null |
||
| 385 | */ |
||
| 386 | View Code Duplication | public function findByCriteria($criteria, string $toScenario = null) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param $criteria |
||
| 400 | * @param string $toScenario |
||
| 401 | * @return BaseObject |
||
| 402 | * @throws ObjectNotFoundException |
||
| 403 | */ |
||
| 404 | public function getByCriteria($criteria, string $toScenario = null): BaseObject |
||
| 416 | |||
| 417 | |||
| 418 | /******************************************* |
||
| 419 | * FIND/GET BY RECORD |
||
| 420 | *******************************************/ |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param Record $record |
||
| 424 | * @param string $toScenario |
||
| 425 | * @return BaseObject |
||
| 426 | */ |
||
| 427 | View Code Duplication | public function findByRecord(Record $record, string $toScenario = null): BaseObject |
|
| 444 | |||
| 445 | /** |
||
| 446 | * @param Record $record |
||
| 447 | * @param string $toScenario |
||
| 448 | * @return BaseObject |
||
| 449 | */ |
||
| 450 | public function getByRecord(Record $record, string $toScenario = null): BaseObject |
||
| 454 | |||
| 455 | |||
| 456 | /******************************************* |
||
| 457 | * CACHE |
||
| 458 | *******************************************/ |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param $identifier |
||
| 462 | * @return BaseObject|null |
||
| 463 | */ |
||
| 464 | public function findCache($identifier) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param Record $record |
||
| 479 | * @return BaseObject|null |
||
| 480 | */ |
||
| 481 | public function findCacheByRecord(Record $record) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param BaseObject $object |
||
| 488 | * @return static |
||
| 489 | */ |
||
| 490 | public function addToCache(BaseObject $object) |
||
| 494 | |||
| 495 | |||
| 496 | /******************************************* |
||
| 497 | * EXCEPTIONS |
||
| 498 | *******************************************/ |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @throws ObjectNotFoundException |
||
| 502 | */ |
||
| 503 | protected function notFoundException() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param null $criteria |
||
| 516 | * @throws ObjectNotFoundException |
||
| 517 | */ |
||
| 518 | protected function notFoundByCriteriaException($criteria = null) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param null $condition |
||
| 532 | * @throws ObjectNotFoundException |
||
| 533 | */ |
||
| 534 | protected function notFoundByConditionException($condition = null) |
||
| 545 | |||
| 546 | } |
||
| 547 |