Complex classes like Relationship 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 Relationship, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Relationship extends BaseObject implements RelationshipInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * The element the relations are related to |
||
| 37 | * |
||
| 38 | * @var ElementInterface|null |
||
| 39 | */ |
||
| 40 | private $element; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The field which accesses the relations |
||
| 44 | * |
||
| 45 | * @var RelationalInterface |
||
| 46 | */ |
||
| 47 | private $field; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The association records |
||
| 51 | * |
||
| 52 | * @var Collection|null |
||
| 53 | */ |
||
| 54 | private $collection; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | protected $mutated = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param ElementInterface|null $element |
||
| 63 | * @param RelationalInterface $field |
||
| 64 | * @param array $config |
||
| 65 | */ |
||
| 66 | public function __construct(RelationalInterface $field, ElementInterface $element = null, array $config = []) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @inheritDoc |
||
| 76 | */ |
||
| 77 | public function isMutated(): bool |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @inheritDoc |
||
| 84 | */ |
||
| 85 | public function exists($object): bool |
||
| 89 | |||
| 90 | /************************************************************ |
||
| 91 | * QUERY |
||
| 92 | ************************************************************/ |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @inheritDoc |
||
| 96 | */ |
||
| 97 | public function getQuery(): ElementQueryInterface |
||
| 101 | |||
| 102 | |||
| 103 | /************************************************************ |
||
| 104 | * COLLECTIONS |
||
| 105 | ************************************************************/ |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return Collection |
||
| 109 | */ |
||
| 110 | public function getElements(): Collection |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @inheritDoc |
||
| 134 | */ |
||
| 135 | public function getCollection(): Collection |
||
| 145 | |||
| 146 | |||
| 147 | /************************************************************ |
||
| 148 | * ADD / REMOVE |
||
| 149 | ************************************************************/ |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @inheritDoc |
||
| 153 | */ |
||
| 154 | public function add($objects, array $attributes = []): RelationshipInterface |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritDoc |
||
| 177 | */ |
||
| 178 | public function remove($objects): RelationshipInterface |
||
| 188 | |||
| 189 | |||
| 190 | /******************************************* |
||
| 191 | * COMMIT |
||
| 192 | *******************************************/ |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | public function save(): bool |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @inheritDoc |
||
| 234 | */ |
||
| 235 | public function reset(): RelationshipInterface |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * @inheritDoc |
||
| 245 | */ |
||
| 246 | protected function delta(): array |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Ensure we're working with an array of objects, not configs, etc |
||
| 279 | * @param array|QueryInterface|Collection|ElementInterface|Association $objects |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | protected function objectArray($objects): array |
||
| 295 | |||
| 296 | |||
| 297 | /******************************************* |
||
| 298 | * CACHE |
||
| 299 | *******************************************/ |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param Association[] $associations |
||
| 303 | * @param bool $mutated |
||
| 304 | * @return static |
||
| 305 | */ |
||
| 306 | protected function setCache(array $associations, bool $mutated = true): self |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param Association $association |
||
| 316 | * @return static |
||
| 317 | */ |
||
| 318 | protected function addToCache(Association $association): self |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param int $key |
||
| 332 | * @return static |
||
| 333 | */ |
||
| 334 | protected function removeFromCache(int $key): self |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param array $criteria |
||
| 344 | * @return AssociationQuery |
||
| 345 | */ |
||
| 346 | protected function query(array $criteria = []): AssociationQuery |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Create a new relationship object |
||
| 367 | * |
||
| 368 | * @param $object |
||
| 369 | * @return Association |
||
| 370 | */ |
||
| 371 | protected function create($object): Association |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param Association|ElementInterface|int|string|null $object |
||
| 384 | * @return Association|null |
||
| 385 | */ |
||
| 386 | protected function findOne($object = null) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param UserAssociation|int|array|null $object |
||
| 397 | * @return int|null |
||
| 398 | */ |
||
| 399 | protected function findKey($object = null) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param ElementInterface|Association|int|array|null $element |
||
| 421 | * @return ElementInterface|null |
||
| 422 | */ |
||
| 423 | protected function resolveElement($element = null) |
||
| 445 | |||
| 446 | |||
| 447 | /******************************************* |
||
| 448 | * MAGIC |
||
| 449 | *******************************************/ |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $name |
||
| 453 | * @return mixed |
||
| 454 | */ |
||
| 455 | public function __get($name) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $name |
||
| 466 | * @param mixed $value |
||
| 467 | */ |
||
| 468 | public function __set($name, $value) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $name |
||
| 479 | * @param array $params |
||
| 480 | * @return mixed |
||
| 481 | */ |
||
| 482 | public function __call($name, $params) |
||
| 490 | } |
||
| 491 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: