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 |
||
32 | class Relationship extends BaseObject implements RelationshipInterface |
||
33 | { |
||
34 | /** |
||
35 | * The element the relations are related to |
||
36 | * |
||
37 | * @var ElementInterface|null |
||
38 | */ |
||
39 | private $element; |
||
40 | |||
41 | /** |
||
42 | * The field which accesses the relations |
||
43 | * |
||
44 | * @var RelationalInterface|Field |
||
45 | */ |
||
46 | private $field; |
||
47 | |||
48 | /** |
||
49 | * @var Collection|null |
||
50 | */ |
||
51 | private $relations; |
||
52 | |||
53 | /** |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $mutated = false; |
||
57 | |||
58 | /** |
||
59 | * @param ElementInterface|null $element |
||
60 | * @param RelationalInterface $field |
||
61 | * @param array $config |
||
62 | */ |
||
63 | public function __construct(RelationalInterface $field, ElementInterface $element = null, array $config = []) |
||
70 | |||
71 | |||
72 | /************************************************************ |
||
73 | * QUERY |
||
74 | ************************************************************/ |
||
75 | |||
76 | /** |
||
77 | * @param Association|ElementInterface|int|string $object |
||
78 | * @return Association |
||
79 | */ |
||
80 | public function findOrCreate($object): Association |
||
88 | |||
89 | /** |
||
90 | * @param Association|ElementInterface|int|string $object |
||
91 | * @return Association |
||
92 | * @throws Exception |
||
93 | */ |
||
94 | public function findOrFail($object): Association |
||
102 | |||
103 | /** |
||
104 | * @param Association|ElementInterface|int|string|null $object |
||
105 | * @return Association|null |
||
106 | */ |
||
107 | public function findOne($object = null) |
||
115 | |||
116 | /************************************************************ |
||
117 | * COLLECTIONS |
||
118 | ************************************************************/ |
||
119 | |||
120 | /** |
||
121 | * @return Collection |
||
122 | */ |
||
123 | public function getCollection(): Collection |
||
134 | |||
135 | /** |
||
136 | * @inheritDoc |
||
137 | */ |
||
138 | public function getRelationships(): Collection |
||
146 | |||
147 | /** |
||
148 | * @return Collection |
||
149 | */ |
||
150 | protected function existingRelationships() |
||
172 | |||
173 | /************************************************************ |
||
174 | * ADD / REMOVE |
||
175 | ************************************************************/ |
||
176 | |||
177 | /** |
||
178 | * @inheritDoc |
||
179 | */ |
||
180 | public function add($objects, array $attributes = []): RelationshipInterface |
||
188 | |||
189 | /** |
||
190 | * @param $object |
||
191 | * @param array $attributes |
||
192 | * @return RelationshipInterface |
||
193 | */ |
||
194 | protected function addOne($object, array $attributes = []): RelationshipInterface |
||
225 | |||
226 | /** |
||
227 | * @inheritDoc |
||
228 | */ |
||
229 | public function remove($objects): RelationshipInterface |
||
239 | |||
240 | |||
241 | /******************************************* |
||
242 | * SAVE |
||
243 | *******************************************/ |
||
244 | |||
245 | /** |
||
246 | * @return bool |
||
247 | */ |
||
248 | public function save(): bool |
||
280 | |||
281 | |||
282 | /************************************************************ |
||
283 | * UTILITIES |
||
284 | ************************************************************/ |
||
285 | |||
286 | /** |
||
287 | * @inheritDoc |
||
288 | */ |
||
289 | public function clear(): RelationshipInterface |
||
294 | |||
295 | /** |
||
296 | * @inheritDoc |
||
297 | */ |
||
298 | public function reset(): RelationshipInterface |
||
304 | |||
305 | /** |
||
306 | * @inheritDoc |
||
307 | */ |
||
308 | public function isMutated(): bool |
||
312 | |||
313 | /** |
||
314 | * @inheritDoc |
||
315 | */ |
||
316 | public function exists($object): bool |
||
320 | |||
321 | /** |
||
322 | * @inheritDoc |
||
323 | */ |
||
324 | public function count() |
||
328 | |||
329 | |||
330 | /************************************************************ |
||
331 | * DELTA |
||
332 | ************************************************************/ |
||
333 | |||
334 | /** |
||
335 | * @inheritDoc |
||
336 | */ |
||
337 | protected function delta(): array |
||
371 | |||
372 | /** |
||
373 | * @param Association $new |
||
374 | * @param Association $existing |
||
375 | * @return bool |
||
376 | */ |
||
377 | private function hasChanged(Association $new, Association $existing): bool |
||
381 | |||
382 | /** |
||
383 | * @param Association $from |
||
384 | * @param Association $to |
||
385 | * |
||
386 | * @return Association |
||
387 | */ |
||
388 | private function sync(Association $to, Association $from): Association |
||
400 | |||
401 | |||
402 | /******************************************* |
||
403 | * RESOLVERS |
||
404 | *******************************************/ |
||
405 | |||
406 | /** |
||
407 | * Ensure we're working with an array of objects, not configs, etc |
||
408 | * |
||
409 | * @param array|QueryInterface|Collection|ElementInterface|Association $objects |
||
410 | * @return array |
||
411 | */ |
||
412 | protected function objectArray($objects): array |
||
425 | |||
426 | |||
427 | /******************************************* |
||
428 | * COLLECTION UTILS |
||
429 | *******************************************/ |
||
430 | |||
431 | /** |
||
432 | * @param Association[] $associations |
||
433 | * @param bool $mutated |
||
434 | * @return static |
||
435 | */ |
||
436 | protected function newRelations(array $associations, bool $mutated = true): self |
||
443 | |||
444 | /** |
||
445 | * @param Association $association |
||
446 | * @return static |
||
447 | */ |
||
448 | protected function addToRelations(Association $association): self |
||
459 | |||
460 | /** |
||
461 | * @param int $key |
||
462 | * @return static |
||
463 | */ |
||
464 | protected function removeFromRelations(int $key): self |
||
471 | |||
472 | /** |
||
473 | * @param array $associations |
||
474 | * @return Collection |
||
475 | */ |
||
476 | protected function createRelations(array $associations = []): Collection |
||
485 | |||
486 | /** |
||
487 | * Position the relationship based on the sort order |
||
488 | * |
||
489 | * @inheritDoc |
||
490 | */ |
||
491 | protected function insertCollection(Collection $collection, Association $association) |
||
500 | |||
501 | /** |
||
502 | * @inheritDoc |
||
503 | */ |
||
504 | protected function updateCollection(Collection $collection, Association $association) |
||
512 | |||
513 | |||
514 | /************************************************************ |
||
515 | * QUERY |
||
516 | ************************************************************/ |
||
517 | |||
518 | /** |
||
519 | * @return AssociationQuery |
||
520 | */ |
||
521 | protected function associationQuery(): AssociationQuery |
||
531 | |||
532 | |||
533 | /******************************************* |
||
534 | * CREATE |
||
535 | *******************************************/ |
||
536 | |||
537 | /** |
||
538 | * Create a new relationship object |
||
539 | * |
||
540 | * @param $object |
||
541 | * @return Association |
||
542 | */ |
||
543 | protected function create($object): Association |
||
557 | |||
558 | |||
559 | /******************************************* |
||
560 | * UTILS |
||
561 | *******************************************/ |
||
562 | |||
563 | /** |
||
564 | * @param UserAssociation|int|array|null $object |
||
565 | * @return int|null |
||
566 | */ |
||
567 | protected function findKey($object = null) |
||
579 | |||
580 | /** |
||
581 | * @param $identifier |
||
582 | * @return int|string|null |
||
583 | */ |
||
584 | private function findRelationshipKey($identifier) |
||
595 | |||
596 | /** |
||
597 | * @param ElementInterface|Association|int|array|null $element |
||
598 | * @return ElementInterface|null |
||
599 | */ |
||
600 | protected function resolveElement($element = null) |
||
622 | |||
623 | |||
624 | /******************************************* |
||
625 | * MAGIC (pass calls onto query) |
||
626 | *******************************************/ |
||
627 | |||
628 | /** |
||
629 | * @param string $name |
||
630 | * @return mixed |
||
631 | */ |
||
632 | public function __get($name) |
||
640 | |||
641 | /** |
||
642 | * @param string $name |
||
643 | * @param mixed $value |
||
644 | */ |
||
645 | public function __set($name, $value) |
||
653 | |||
654 | /** |
||
655 | * @param string $name |
||
656 | * @param array $params |
||
657 | * @return mixed |
||
658 | */ |
||
659 | public function __call($name, $params) |
||
669 | } |
||
670 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: