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 |
||
| 34 | class Relationship extends BaseObject implements RelationshipInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * The element the relations are related to |
||
| 38 | * |
||
| 39 | * @var ElementInterface|null |
||
| 40 | */ |
||
| 41 | private $element; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The field which accesses the relations |
||
| 45 | * |
||
| 46 | * @var RelationalInterface |
||
| 47 | */ |
||
| 48 | private $field; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The association records |
||
| 52 | * |
||
| 53 | * @var Collection|null |
||
| 54 | */ |
||
| 55 | private $relations; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | protected $mutated = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param ElementInterface|null $element |
||
| 64 | * @param RelationalInterface $field |
||
| 65 | * @param array $config |
||
| 66 | */ |
||
| 67 | public function __construct(RelationalInterface $field, ElementInterface $element = null, array $config = []) |
||
| 74 | |||
| 75 | |||
| 76 | /************************************************************ |
||
| 77 | * QUERY |
||
| 78 | ************************************************************/ |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param Association|ElementInterface|int|string $object |
||
| 82 | * @return Association |
||
| 83 | */ |
||
| 84 | public function findOrCreate($object): Association |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param Association|ElementInterface|int|string $object |
||
| 95 | * @return Association |
||
| 96 | * @throws Exception |
||
| 97 | */ |
||
| 98 | public function findOrFail($object): Association |
||
| 106 | |||
| 107 | |||
| 108 | /************************************************************ |
||
| 109 | * COLLECTIONS |
||
| 110 | ************************************************************/ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return Collection |
||
| 114 | */ |
||
| 115 | public function getCollection(): Collection |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @inheritDoc |
||
| 139 | */ |
||
| 140 | public function getRelationships(): Collection |
||
| 148 | |||
| 149 | |||
| 150 | /************************************************************ |
||
| 151 | * ADD / REMOVE |
||
| 152 | ************************************************************/ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @inheritDoc |
||
| 156 | */ |
||
| 157 | public function add($objects, array $attributes = []): RelationshipInterface |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @inheritDoc |
||
| 180 | */ |
||
| 181 | public function remove($objects): RelationshipInterface |
||
| 191 | |||
| 192 | |||
| 193 | /******************************************* |
||
| 194 | * COMMIT |
||
| 195 | *******************************************/ |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function save(): bool |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritDoc |
||
| 236 | */ |
||
| 237 | public function clear(): RelationshipInterface |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @inheritDoc |
||
| 246 | */ |
||
| 247 | public function reset(): RelationshipInterface |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @inheritDoc |
||
| 256 | */ |
||
| 257 | public function isMutated(): bool |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @inheritDoc |
||
| 264 | */ |
||
| 265 | public function exists($object): bool |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @inheritDoc |
||
| 272 | */ |
||
| 273 | public function count() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @inheritDoc |
||
| 280 | */ |
||
| 281 | protected function delta(): array |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Ensure we're working with an array of objects, not configs, etc |
||
| 314 | * |
||
| 315 | * @param array|QueryInterface|Collection|ElementInterface|Association $objects |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | protected function objectArray($objects): array |
||
| 331 | |||
| 332 | |||
| 333 | /******************************************* |
||
| 334 | * CACHE |
||
| 335 | *******************************************/ |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param Association[] $associations |
||
| 339 | * @param bool $mutated |
||
| 340 | * @return static |
||
| 341 | */ |
||
| 342 | protected function newRelations(array $associations, bool $mutated = true): self |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param Association $association |
||
| 352 | * @return static |
||
| 353 | */ |
||
| 354 | protected function addToRelations(Association $association): self |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param int $key |
||
| 368 | * @return static |
||
| 369 | */ |
||
| 370 | protected function removeFromRelations(int $key): self |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param array $criteria |
||
| 380 | * @return AssociationQuery |
||
| 381 | */ |
||
| 382 | protected function query(array $criteria = []): AssociationQuery |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Create a new relationship object |
||
| 403 | * |
||
| 404 | * @param $object |
||
| 405 | * @return Association |
||
| 406 | */ |
||
| 407 | protected function create($object): Association |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param Association|ElementInterface|int|string|null $object |
||
| 420 | * @return Association|null |
||
| 421 | */ |
||
| 422 | public function findOne($object = null) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param UserAssociation|int|array|null $object |
||
| 433 | * @return int|null |
||
| 434 | */ |
||
| 435 | protected function findKey($object = null) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param ElementInterface|Association|int|array|null $element |
||
| 457 | * @return ElementInterface|null |
||
| 458 | */ |
||
| 459 | protected function resolveElement($element = null) |
||
| 481 | |||
| 482 | |||
| 483 | /******************************************* |
||
| 484 | * MAGIC (pass calls onto query) |
||
| 485 | *******************************************/ |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @param string $name |
||
| 489 | * @return mixed |
||
| 490 | */ |
||
| 491 | public function __get($name) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $name |
||
| 502 | * @param mixed $value |
||
| 503 | */ |
||
| 504 | public function __set($name, $value) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param string $name |
||
| 515 | * @param array $params |
||
| 516 | * @return mixed |
||
| 517 | */ |
||
| 518 | public function __call($name, $params) |
||
| 528 | } |
||
| 529 |
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: