Complex classes like Organization 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 Organization, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Organization extends Element |
||
38 | { |
||
39 | use ExplicitElementTrait, |
||
40 | DateJoinedAttributeTrait, |
||
41 | TypesAttributeTrait, |
||
42 | UsersAttributeTrait; |
||
43 | |||
44 | /** |
||
45 | * Whether associated types should be saved |
||
46 | * |
||
47 | * @var bool |
||
48 | */ |
||
49 | private $saveTypes = true; |
||
50 | |||
51 | /** |
||
52 | * Whether associated users should be saved |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | private $saveUsers = true; |
||
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | public static function displayName(): string |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | public function getIsEditable(): bool |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | public static function hasTitles(): bool |
||
81 | |||
82 | /** |
||
83 | * @inheritdoc |
||
84 | */ |
||
85 | public static function isLocalized(): bool |
||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | public static function hasContent(): bool |
||
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | public static function hasUris(): bool |
||
105 | |||
106 | /** |
||
107 | * Returns whether this element type can have statuses. |
||
108 | * |
||
109 | * @return boolean |
||
110 | */ |
||
111 | public static function hasStatuses(): bool |
||
115 | |||
116 | |||
117 | /************************************************************ |
||
118 | * QUERY |
||
119 | ************************************************************/ |
||
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | * |
||
124 | * @return OrganizationQuery |
||
125 | */ |
||
126 | public static function find(): ElementQueryInterface |
||
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | * @return static|null |
||
134 | */ |
||
135 | public static function findOne($criteria = null) |
||
151 | |||
152 | /** |
||
153 | * @param mixed $criteria |
||
154 | * @param bool $one |
||
155 | * @return Element|Element[]|null |
||
156 | */ |
||
157 | protected static function findByCondition($criteria, bool $one) |
||
169 | |||
170 | |||
171 | /************************************************************ |
||
172 | * PROPERTIES / ATTRIBUTES |
||
173 | ************************************************************/ |
||
174 | |||
175 | /** |
||
176 | * @return array |
||
177 | */ |
||
178 | public function attributes() |
||
185 | |||
186 | /** |
||
187 | * @inheritdoc |
||
188 | * @throws \yii\base\InvalidConfigException |
||
189 | */ |
||
190 | public function rules() |
||
212 | |||
213 | /** |
||
214 | * @return array |
||
215 | */ |
||
216 | public function attributeLabels() |
||
223 | |||
224 | |||
225 | /************************************************************ |
||
226 | * FIELD LAYOUT |
||
227 | ************************************************************/ |
||
228 | |||
229 | /** |
||
230 | * @inheritdoc |
||
231 | */ |
||
232 | public function getFieldLayout() |
||
240 | |||
241 | |||
242 | /************************************************************ |
||
243 | * ELEMENT ADMIN |
||
244 | ************************************************************/ |
||
245 | |||
246 | /** |
||
247 | * @inheritdoc |
||
248 | */ |
||
249 | public function getRef() |
||
257 | |||
258 | /** |
||
259 | * @inheritdoc |
||
260 | */ |
||
261 | public function getCpEditUrl() |
||
265 | |||
266 | /** |
||
267 | * @inheritdoc |
||
268 | */ |
||
269 | protected static function defineSources(string $context = null): array |
||
279 | |||
280 | /** |
||
281 | * @return array |
||
282 | */ |
||
283 | private static function defineDefaultSources(): array |
||
294 | |||
295 | /** |
||
296 | * @return array |
||
297 | */ |
||
298 | private static function defineTypeSources(): array |
||
319 | |||
320 | /** |
||
321 | * @return array |
||
322 | */ |
||
323 | private static function defineUserSources(): array |
||
347 | |||
348 | /** |
||
349 | * @inheritdoc |
||
350 | */ |
||
351 | protected static function defineActions(string $source = null): array |
||
371 | |||
372 | /** |
||
373 | * @inheritdoc |
||
374 | */ |
||
375 | protected static function defineSearchableAttributes(): array |
||
381 | |||
382 | /** |
||
383 | * @inheritdoc |
||
384 | */ |
||
385 | protected static function defineSortOptions(): array |
||
392 | |||
393 | /** |
||
394 | * @inheritdoc |
||
395 | */ |
||
396 | public static function eagerLoadingMap(array $sourceElements, string $handle) |
||
405 | |||
406 | /** |
||
407 | * @inheritdoc |
||
408 | */ |
||
409 | public function setEagerLoadedElements(string $handle, array $elements) |
||
421 | |||
422 | /** |
||
423 | * @inheritdoc |
||
424 | */ |
||
425 | public static function defineTableAttributes(): array |
||
436 | |||
437 | |||
438 | |||
439 | // Indexes, etc. |
||
440 | // ------------------------------------------------------------------------- |
||
441 | |||
442 | /** |
||
443 | * @inheritdoc |
||
444 | */ |
||
445 | public function tableAttributeHtml(string $attribute): string |
||
464 | |||
465 | /** |
||
466 | * @inheritdoc |
||
467 | */ |
||
468 | public function getUriFormat() |
||
480 | |||
481 | /** |
||
482 | * @inheritdoc |
||
483 | */ |
||
484 | public function route() |
||
512 | |||
513 | /** |
||
514 | * @return \flipbox\organizations\records\OrganizationTypeSiteSettings|null |
||
515 | */ |
||
516 | protected function getSiteSettings() |
||
538 | |||
539 | |||
540 | /************************************************************ |
||
541 | * EVENTS |
||
542 | ************************************************************/ |
||
543 | |||
544 | /** |
||
545 | * @inheritdoc |
||
546 | */ |
||
547 | public function save(bool $runValidation = true, bool $propagate = true): bool |
||
551 | |||
552 | /** |
||
553 | * @inheritdoc |
||
554 | */ |
||
555 | public function beforeSave(bool $isNew): bool |
||
563 | |||
564 | /** |
||
565 | * @inheritdoc |
||
566 | * @throws /Exception |
||
567 | */ |
||
568 | public function afterSave(bool $isNew) |
||
586 | |||
587 | |||
588 | /******************************************* |
||
589 | * RELATIONSHIP |
||
590 | *******************************************/ |
||
591 | |||
592 | /** |
||
593 | * @return static |
||
594 | */ |
||
595 | public function withTypes(): self |
||
600 | |||
601 | /** |
||
602 | * @return static |
||
603 | */ |
||
604 | public function withoutTypes(): self |
||
609 | |||
610 | /** |
||
611 | * @return static |
||
612 | */ |
||
613 | public function withUsers(): self |
||
618 | |||
619 | /** |
||
620 | * @return static |
||
621 | */ |
||
622 | public function withoutUsers(): self |
||
627 | |||
628 | /******************************************* |
||
629 | * RECORD |
||
630 | *******************************************/ |
||
631 | |||
632 | /** |
||
633 | * @param bool $isNew |
||
634 | * @return bool |
||
635 | * @throws \Exception |
||
636 | */ |
||
637 | protected function saveRecord(bool $isNew): bool |
||
667 | |||
668 | /** |
||
669 | * @inheritdoc |
||
670 | * @return OrganizationRecord |
||
671 | */ |
||
672 | protected function elementToRecord(): OrganizationRecord |
||
686 | } |
||
687 |