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|Field |
||
46 | */ |
||
47 | private $field; |
||
48 | |||
49 | /** |
||
50 | * @var Collection|null |
||
51 | */ |
||
52 | private $relations; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $mutated = false; |
||
58 | |||
59 | /** |
||
60 | * @param ElementInterface|null $element |
||
61 | * @param RelationalInterface $field |
||
62 | * @param array $config |
||
63 | */ |
||
64 | public function __construct(RelationalInterface $field, ElementInterface $element = null, array $config = []) |
||
71 | |||
72 | |||
73 | /************************************************************ |
||
74 | * QUERY |
||
75 | ************************************************************/ |
||
76 | |||
77 | /** |
||
78 | * @param Association|ElementInterface|int|string $object |
||
79 | * @return Association |
||
80 | */ |
||
81 | public function findOrCreate($object): Association |
||
89 | |||
90 | /** |
||
91 | * @param Association|ElementInterface|int|string $object |
||
92 | * @return Association |
||
93 | * @throws Exception |
||
94 | */ |
||
95 | public function findOrFail($object): Association |
||
103 | |||
104 | /** |
||
105 | * @param Association|ElementInterface|int|string|null $object |
||
106 | * @return Association|null |
||
107 | */ |
||
108 | public function findOne($object = null) |
||
116 | |||
117 | /************************************************************ |
||
118 | * COLLECTIONS |
||
119 | ************************************************************/ |
||
120 | |||
121 | /** |
||
122 | * @return Collection |
||
123 | */ |
||
124 | public function getCollection(): Collection |
||
147 | |||
148 | /** |
||
149 | * @inheritDoc |
||
150 | */ |
||
151 | public function getRelationships(): Collection |
||
159 | |||
160 | /** |
||
161 | * @return Collection |
||
162 | */ |
||
163 | protected function existingRelationships() |
||
169 | |||
170 | |||
171 | /************************************************************ |
||
172 | * QUERY |
||
173 | ************************************************************/ |
||
174 | |||
175 | /** |
||
176 | * @return ElementQueryInterface |
||
177 | */ |
||
178 | public function getQuery(): ElementQueryInterface |
||
182 | |||
183 | /************************************************************ |
||
184 | * ADD / REMOVE |
||
185 | ************************************************************/ |
||
186 | |||
187 | /** |
||
188 | * @inheritDoc |
||
189 | */ |
||
190 | public function add($objects, array $attributes = []): RelationshipInterface |
||
198 | |||
199 | /** |
||
200 | * @param $object |
||
201 | * @param array $attributes |
||
202 | * @return RelationshipInterface |
||
203 | */ |
||
204 | protected function addOne($object, array $attributes = []): RelationshipInterface |
||
234 | |||
235 | /** |
||
236 | * @inheritDoc |
||
237 | */ |
||
238 | public function remove($objects): RelationshipInterface |
||
248 | |||
249 | |||
250 | /******************************************* |
||
251 | * SAVE |
||
252 | *******************************************/ |
||
253 | |||
254 | /** |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function save(): bool |
||
289 | |||
290 | |||
291 | /************************************************************ |
||
292 | * UTILITIES |
||
293 | ************************************************************/ |
||
294 | |||
295 | /** |
||
296 | * @inheritDoc |
||
297 | */ |
||
298 | public function clear(): RelationshipInterface |
||
303 | |||
304 | /** |
||
305 | * @inheritDoc |
||
306 | */ |
||
307 | public function reset(): RelationshipInterface |
||
313 | |||
314 | /** |
||
315 | * @inheritDoc |
||
316 | */ |
||
317 | public function isMutated(): bool |
||
321 | |||
322 | /** |
||
323 | * @inheritDoc |
||
324 | */ |
||
325 | public function exists($object): bool |
||
329 | |||
330 | /** |
||
331 | * @inheritDoc |
||
332 | */ |
||
333 | public function count() |
||
337 | |||
338 | |||
339 | /************************************************************ |
||
340 | * DELTA |
||
341 | ************************************************************/ |
||
342 | |||
343 | /** |
||
344 | * @inheritDoc |
||
345 | */ |
||
346 | protected function delta(): array |
||
377 | |||
378 | /** |
||
379 | * @param Association $new |
||
380 | * @param Association $existing |
||
381 | * @return bool |
||
382 | */ |
||
383 | private function hasChanged(Association $new, Association $existing): bool |
||
388 | |||
389 | /** |
||
390 | * @param Association $from |
||
391 | * @param Association $to |
||
392 | * |
||
393 | * @return Association |
||
394 | */ |
||
395 | private function sync(Association $to, Association $from): Association |
||
403 | |||
404 | |||
405 | /******************************************* |
||
406 | * RESOLVERS |
||
407 | *******************************************/ |
||
408 | |||
409 | /** |
||
410 | * Ensure we're working with an array of objects, not configs, etc |
||
411 | * |
||
412 | * @param array|QueryInterface|Collection|ElementInterface|Association $objects |
||
413 | * @return array |
||
414 | */ |
||
415 | protected function objectArray($objects): array |
||
428 | |||
429 | |||
430 | /******************************************* |
||
431 | * COLLECTION UTILS |
||
432 | *******************************************/ |
||
433 | |||
434 | /** |
||
435 | * @param Association[] $associations |
||
436 | * @param bool $mutated |
||
437 | * @return static |
||
438 | */ |
||
439 | protected function newRelations(array $associations, bool $mutated = true): self |
||
446 | |||
447 | /** |
||
448 | * @param Association $association |
||
449 | * @return static |
||
450 | */ |
||
451 | protected function addToRelations(Association $association): self |
||
452 | { |
||
453 | $this->insertCollection($this->getRelationships(), $association); |
||
454 | $this->mutated = true; |
||
455 | |||
456 | return $this; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * @param int $key |
||
461 | * @return static |
||
462 | */ |
||
463 | protected function removeFromRelations(int $key): self |
||
464 | { |
||
465 | $this->relations->forget($key); |
||
466 | $this->mutated = true; |
||
467 | |||
468 | return $this; |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * @param array $associations |
||
473 | * @return Collection |
||
474 | */ |
||
475 | protected function createRelations(array $associations = []): Collection |
||
476 | { |
||
477 | $collection = new Collection(); |
||
478 | foreach ($associations as $association) { |
||
479 | $this->insertCollection($collection, $association); |
||
480 | } |
||
481 | |||
482 | return $collection; |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Position the relationship based on the sort order |
||
487 | * |
||
488 | * @inheritDoc |
||
489 | */ |
||
490 | protected function insertCollection(Collection $collection, Association $association) |
||
491 | { |
||
492 | if ($this->field->ensureSortOrder() && $association->sortOrder > 0) { |
||
493 | $collection->splice($association->sortOrder - 1, 0, [$association]); |
||
494 | return; |
||
495 | } |
||
496 | |||
497 | $collection->push($association); |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * @inheritDoc |
||
502 | */ |
||
503 | protected function updateCollection(Collection $collection, Association $association) |
||
504 | { |
||
505 | if (!$this->field->ensureSortOrder()) { |
||
506 | return; |
||
507 | } |
||
508 | |||
509 | if (null !== ($key = $this->findKey($association))) { |
||
510 | $collection->offsetUnset($key); |
||
511 | } |
||
512 | |||
513 | $this->insertCollection($collection, $association); |
||
514 | } |
||
515 | |||
516 | |||
517 | /************************************************************ |
||
518 | * QUERY |
||
519 | ************************************************************/ |
||
520 | |||
521 | /** |
||
522 | * @return AssociationQuery |
||
523 | */ |
||
524 | protected function associationQuery(): AssociationQuery |
||
525 | { |
||
526 | return Association::find() |
||
527 | ->setSource($this->element->getId() ?: false) |
||
528 | ->setField($this->field) |
||
529 | ->orderBy([ |
||
530 | 'sortOrder' => SORT_ASC |
||
531 | ]) |
||
532 | ->limit(null); |
||
533 | } |
||
534 | |||
535 | |||
536 | /******************************************* |
||
537 | * CREATE |
||
538 | *******************************************/ |
||
539 | |||
540 | /** |
||
541 | * Create a new relationship object |
||
542 | * |
||
543 | * @param $object |
||
544 | * @return Association |
||
545 | */ |
||
546 | protected function create($object): Association |
||
547 | { |
||
548 | if ($object instanceof Association) { |
||
549 | return $object; |
||
550 | } |
||
551 | |||
552 | $element = $this->resolveElement($object); |
||
553 | |||
554 | return new Association([ |
||
555 | 'fieldId' => $this->field->id, |
||
556 | 'sourceId' => $this->element ? $this->element->getId() : null, |
||
557 | 'targetId' => $element->getId() |
||
558 | ]); |
||
559 | } |
||
560 | |||
561 | |||
562 | /******************************************* |
||
563 | * UTILS |
||
564 | *******************************************/ |
||
565 | |||
566 | /** |
||
567 | * @param UserAssociation|int|array|null $object |
||
568 | * @return int|null |
||
569 | */ |
||
570 | protected function findKey($object = null) |
||
571 | { |
||
572 | if ($object instanceof Association) { |
||
573 | return $this->findRelationshipKey($object->targetId); |
||
574 | } |
||
575 | |||
576 | if (null === ($element = $this->resolveElement($object))) { |
||
577 | return null; |
||
578 | } |
||
579 | |||
580 | return $this->findRelationshipKey($element->getId()); |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * @param $identifier |
||
585 | * @return int|string|null |
||
586 | */ |
||
587 | private function findRelationshipKey($identifier) |
||
588 | { |
||
589 | if (null === $identifier) { |
||
590 | return null; |
||
591 | } |
||
592 | |||
593 | /** @var Association $association */ |
||
594 | foreach ($this->getRelationships()->all() as $key => $association) { |
||
595 | if ($association->targetId == $identifier) { |
||
596 | return $key; |
||
597 | } |
||
598 | } |
||
599 | |||
600 | return null; |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * @param ElementInterface|Association|int|array|null $element |
||
605 | * @return ElementInterface|null |
||
606 | */ |
||
607 | protected function resolveElement($element = null) |
||
629 | |||
630 | |||
631 | /******************************************* |
||
632 | * MAGIC (pass calls onto query) |
||
633 | *******************************************/ |
||
634 | |||
635 | /** |
||
636 | * @param string $name |
||
637 | * @return mixed |
||
638 | */ |
||
639 | public function __get($name) |
||
647 | |||
648 | /** |
||
649 | * @param string $name |
||
650 | * @param mixed $value |
||
651 | */ |
||
652 | public function __set($name, $value) |
||
660 | |||
661 | /** |
||
662 | * @param string $name |
||
663 | * @param array $params |
||
664 | * @return mixed |
||
665 | */ |
||
666 | public function __call($name, $params) |
||
676 | } |
||
677 |
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: