Complex classes like DomainAssociations 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 DomainAssociations, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class DomainAssociations extends Component |
||
28 | { |
||
29 | /** |
||
30 | * @event DomainAssociationEvent The event that is triggered before a domain association. |
||
31 | * |
||
32 | * You may set [[DomainAssociationEvent::isValid]] to `false` to prevent the associate action. |
||
33 | */ |
||
34 | const EVENT_BEFORE_ASSOCIATE = 'beforeAssociate'; |
||
35 | |||
36 | /** |
||
37 | * @event DomainAssociationEvent The event that is triggered after a domain association. |
||
38 | */ |
||
39 | const EVENT_AFTER_ASSOCIATE = 'afterAssociate'; |
||
40 | |||
41 | /** |
||
42 | * @event DomainAssociationEvent The event that is triggered before a domain dissociation. |
||
43 | * |
||
44 | * You may set [[DomainAssociationEvent::isValid]] to `false` to prevent the dissociate action. |
||
45 | */ |
||
46 | const EVENT_BEFORE_DISSOCIATE = 'beforeDissociate'; |
||
47 | |||
48 | /** |
||
49 | * @event DomainAssociationEvent The event that is triggered after a domain dissociation. |
||
50 | */ |
||
51 | const EVENT_AFTER_DISSOCIATE = 'afterDissociate'; |
||
52 | |||
53 | /** |
||
54 | * @param Domains $field |
||
55 | * @param DomainsQuery $query |
||
56 | * @param ElementInterface|Element $element |
||
57 | * @return bool |
||
58 | * @throws \Exception |
||
59 | */ |
||
60 | public function save( |
||
101 | |||
102 | /******************************************* |
||
103 | * ASSOCIATE |
||
104 | *******************************************/ |
||
105 | |||
106 | /** |
||
107 | * @param Domain $model |
||
108 | * @return bool |
||
109 | * @throws \Exception |
||
110 | */ |
||
111 | public function associate(Domain $model): bool |
||
167 | |||
168 | /******************************************* |
||
169 | * DISSOCIATE |
||
170 | *******************************************/ |
||
171 | |||
172 | /** |
||
173 | * @param Domain $model |
||
174 | * @param bool $autoReorder |
||
175 | * @return bool |
||
176 | * @throws \Exception |
||
177 | */ |
||
178 | public function dissociate( |
||
236 | |||
237 | /******************************************* |
||
238 | * ORDER |
||
239 | *******************************************/ |
||
240 | |||
241 | /** |
||
242 | * @param string $tableName |
||
243 | * @param int $elementId |
||
244 | * @param int $siteId |
||
245 | * @param array $sortOrder |
||
246 | * @return bool |
||
247 | * @throws \yii\db\Exception |
||
248 | */ |
||
249 | public function updateOrder( |
||
273 | |||
274 | /******************************************* |
||
275 | * INSERT / DELETE |
||
276 | *******************************************/ |
||
277 | |||
278 | /** |
||
279 | * @param string $tableName |
||
280 | * @param int $elementId |
||
281 | * @param string $domain |
||
282 | * @param string $status |
||
283 | * @param int $sortOrder |
||
284 | * @param int $siteId |
||
285 | * @return bool |
||
286 | * @throws \yii\db\Exception |
||
287 | */ |
||
288 | private function insertRecord( |
||
330 | |||
331 | /** |
||
332 | * @param string $tableName |
||
333 | * @param int $elementId |
||
334 | * @param string $domain |
||
335 | * @param int $siteId |
||
336 | * @return bool |
||
337 | * @throws \yii\db\Exception |
||
338 | */ |
||
339 | private function deleteRecord( |
||
377 | |||
378 | |||
379 | /******************************************* |
||
380 | * ASSOCIATE / DISSOCIATE MANY |
||
381 | *******************************************/ |
||
382 | |||
383 | /** |
||
384 | * @param ElementInterface|Element $element |
||
385 | * @param array $models |
||
386 | * @param array $currentModels |
||
387 | * @param array $newOrder |
||
388 | * @return bool |
||
389 | * @throws \Exception |
||
390 | */ |
||
391 | private function associateAll( |
||
416 | |||
417 | /** |
||
418 | * @param ElementInterface|Element $element |
||
419 | * @param array $models |
||
420 | * @return bool |
||
421 | * @throws \Exception |
||
422 | */ |
||
423 | private function dissociateAll( |
||
438 | |||
439 | /******************************************* |
||
440 | * SORT ORDER |
||
441 | *******************************************/ |
||
442 | |||
443 | /** |
||
444 | * @param Domain $model |
||
445 | * @return bool |
||
446 | * @throws \Exception |
||
447 | * @throws \yii\db\Exception |
||
448 | */ |
||
449 | private function applySortOrder( |
||
477 | |||
478 | /** |
||
479 | * @param Domain $model |
||
480 | * @return int |
||
481 | */ |
||
482 | private function nextSortOrder( |
||
495 | |||
496 | /** |
||
497 | * Re-orders so they are in sequential order |
||
498 | * |
||
499 | * @param string $tableName |
||
500 | * @param int $elementId |
||
501 | * @param int $siteId |
||
502 | * @return bool |
||
503 | * @throws \yii\db\Exception |
||
504 | */ |
||
505 | protected function autoReorder(string $tableName, int $elementId, int $siteId): bool |
||
523 | |||
524 | /** |
||
525 | * @param Domains $field |
||
526 | * @param ElementInterface|Element $element |
||
527 | * @return array|null |
||
528 | */ |
||
529 | protected function getCurrentDomainAssociations(Domains $field, ElementInterface $element) |
||
537 | |||
538 | /** |
||
539 | * @param string $tableName |
||
540 | * @param int $elementId |
||
541 | * @param int $siteId |
||
542 | * @return array |
||
543 | */ |
||
544 | private function currentSortOrder(string $tableName, int $elementId, int $siteId): array |
||
551 | |||
552 | /** |
||
553 | * @param string $tableName |
||
554 | * @param int $elementId |
||
555 | * @param int $siteId |
||
556 | * @return Query |
||
557 | */ |
||
558 | private function baseAssociationQuery(string $tableName, int $elementId, int $siteId): Query |
||
568 | |||
569 | /******************************************* |
||
570 | * UTILITIES |
||
571 | *******************************************/ |
||
572 | |||
573 | /** |
||
574 | * @param Domain $model |
||
575 | * @return bool |
||
576 | */ |
||
577 | private function associationExists( |
||
594 | |||
595 | /** |
||
596 | * @param string $tableName |
||
597 | * @param array $newOrder |
||
598 | * @param ElementInterface|Element $element |
||
599 | * @return bool |
||
600 | * @throws \yii\db\Exception |
||
601 | */ |
||
602 | private function reOrderIfChanged(string $tableName, array $newOrder, ElementInterface $element) |
||
616 | } |
||
617 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: