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 |
||
36 | class Organization extends Element |
||
37 | { |
||
38 | use ExplicitElementTrait, |
||
39 | DateJoinedAttributeTrait, |
||
40 | TypesAttributeTrait, |
||
41 | UsersAttributeTrait; |
||
42 | |||
43 | /** |
||
44 | * @inheritdoc |
||
45 | */ |
||
46 | public static function displayName(): string |
||
47 | { |
||
48 | return Craft::t('organizations', 'Organization'); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | public function getIsEditable(): bool |
||
55 | { |
||
56 | return true; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | public static function hasTitles(): bool |
||
63 | { |
||
64 | return true; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @inheritdoc |
||
69 | */ |
||
70 | public static function isLocalized(): bool |
||
71 | { |
||
72 | return true; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public static function hasContent(): bool |
||
79 | { |
||
80 | return true; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | */ |
||
86 | public static function hasUris(): bool |
||
87 | { |
||
88 | return true; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Returns whether this element type can have statuses. |
||
93 | * |
||
94 | * @return boolean |
||
95 | */ |
||
96 | public static function hasStatuses(): bool |
||
97 | { |
||
98 | return true; |
||
99 | } |
||
100 | |||
101 | |||
102 | /************************************************************ |
||
103 | * QUERY |
||
104 | ************************************************************/ |
||
105 | |||
106 | /** |
||
107 | * @inheritdoc |
||
108 | * |
||
109 | * @return OrganizationQuery |
||
110 | */ |
||
111 | public static function find(): ElementQueryInterface |
||
112 | { |
||
113 | return new OrganizationQuery(static::class); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | * @return static|null |
||
119 | */ |
||
120 | public static function findOne($criteria = null) |
||
121 | { |
||
122 | if ($criteria instanceof self) { |
||
123 | return $criteria; |
||
124 | } |
||
125 | |||
126 | return parent::findOne($criteria); |
||
|
|||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param mixed $criteria |
||
131 | * @param bool $one |
||
132 | * @return Element|Element[]|null |
||
133 | */ |
||
134 | protected static function findByCondition($criteria, bool $one) |
||
135 | { |
||
136 | if (is_numeric($criteria)) { |
||
137 | $criteria = ['id' => $criteria]; |
||
138 | } |
||
139 | |||
140 | if (is_string($criteria)) { |
||
141 | $criteria = ['slug' => $criteria]; |
||
142 | } |
||
143 | |||
144 | return parent::findByCondition($criteria, $one); |
||
145 | } |
||
146 | |||
147 | |||
148 | /************************************************************ |
||
149 | * PROPERTIES / ATTRIBUTES |
||
150 | ************************************************************/ |
||
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | public function attributes() |
||
156 | { |
||
157 | return array_merge( |
||
158 | parent::attributes(), |
||
159 | $this->dateJoinedAttributes() |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @inheritdoc |
||
165 | * @throws \yii\base\InvalidConfigException |
||
166 | */ |
||
167 | public function rules() |
||
168 | { |
||
169 | return array_merge( |
||
170 | parent::rules(), |
||
171 | $this->dateJoinedRules(), |
||
172 | [ |
||
173 | [ |
||
174 | [ |
||
175 | 'types', |
||
176 | 'activeType', |
||
177 | 'primaryType', |
||
178 | 'users', |
||
179 | ], |
||
180 | 'safe', |
||
181 | 'on' => [ |
||
182 | ModelHelper::SCENARIO_DEFAULT |
||
183 | ] |
||
184 | |||
185 | ], |
||
186 | ] |
||
187 | ); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return array |
||
192 | */ |
||
193 | public function attributeLabels() |
||
194 | { |
||
195 | return array_merge( |
||
196 | parent::attributeLabels(), |
||
197 | $this->dateJoinedAttributeLabels() |
||
198 | ); |
||
199 | } |
||
200 | |||
201 | |||
202 | /************************************************************ |
||
203 | * FIELD LAYOUT |
||
204 | ************************************************************/ |
||
205 | |||
206 | /** |
||
207 | * @inheritdoc |
||
208 | */ |
||
209 | public function getFieldLayout() |
||
210 | { |
||
211 | if (null === ($type = $this->getActiveType())) { |
||
212 | return OrganizationPlugin::getInstance()->getSettings()->getFieldLayout(); |
||
213 | } |
||
214 | |||
215 | return $type->getFieldLayout(); |
||
216 | } |
||
217 | |||
218 | |||
219 | /************************************************************ |
||
220 | * ELEMENT ADMIN |
||
221 | ************************************************************/ |
||
222 | |||
223 | /** |
||
224 | * @inheritdoc |
||
225 | */ |
||
226 | public function getRef() |
||
234 | |||
235 | /** |
||
236 | * @inheritdoc |
||
237 | */ |
||
238 | public function getCpEditUrl() |
||
239 | { |
||
240 | return UrlHelper::cpUrl('organizations/' . $this->id); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @inheritdoc |
||
245 | */ |
||
246 | protected static function defineSources(string $context = null): array |
||
247 | { |
||
248 | switch ($context) { |
||
249 | case 'user': |
||
256 | |||
257 | /** |
||
258 | * @return array |
||
259 | */ |
||
260 | private static function defineDefaultSources(): array |
||
271 | |||
272 | /** |
||
273 | * @return array |
||
274 | */ |
||
275 | private static function defineTypeSources(): array |
||
296 | |||
297 | /** |
||
298 | * @return array |
||
299 | */ |
||
300 | private static function defineUserSources(): array |
||
324 | |||
325 | /** |
||
326 | * @inheritdoc |
||
327 | */ |
||
328 | protected static function defineActions(string $source = null): array |
||
345 | |||
346 | /** |
||
347 | * @inheritdoc |
||
348 | */ |
||
349 | protected static function defineSearchableAttributes(): array |
||
355 | |||
356 | /** |
||
357 | * @inheritdoc |
||
358 | */ |
||
359 | protected static function defineSortOptions(): array |
||
366 | |||
367 | /** |
||
368 | * @inheritdoc |
||
369 | */ |
||
370 | public static function eagerLoadingMap(array $sourceElements, string $handle) |
||
379 | |||
380 | /** |
||
381 | * @inheritdoc |
||
382 | */ |
||
383 | public function setEagerLoadedElements(string $handle, array $elements) |
||
395 | |||
396 | /** |
||
397 | * @inheritdoc |
||
398 | */ |
||
399 | public static function defineTableAttributes(): array |
||
410 | |||
411 | |||
412 | |||
413 | // Indexes, etc. |
||
414 | // ------------------------------------------------------------------------- |
||
415 | |||
416 | /** |
||
417 | * @inheritdoc |
||
418 | */ |
||
419 | public function tableAttributeHtml(string $attribute): string |
||
438 | |||
439 | /** |
||
440 | * @inheritdoc |
||
441 | */ |
||
442 | public function getUriFormat() |
||
454 | |||
455 | /** |
||
456 | * @inheritdoc |
||
457 | */ |
||
458 | public function route() |
||
486 | |||
487 | /** |
||
488 | * @return \flipbox\organizations\records\OrganizationTypeSiteSettings|null |
||
489 | */ |
||
490 | protected function getSiteSettings() |
||
512 | |||
513 | /************************************************************ |
||
514 | * EVENTS |
||
515 | ************************************************************/ |
||
516 | |||
517 | /** |
||
518 | * @inheritdoc |
||
519 | */ |
||
520 | public function beforeSave(bool $isNew): bool |
||
528 | |||
529 | /** |
||
530 | * @inheritdoc |
||
531 | * @throws /Exception |
||
532 | */ |
||
533 | public function afterSave(bool $isNew) |
||
551 | |||
552 | /******************************************* |
||
553 | * RECORD |
||
554 | *******************************************/ |
||
555 | |||
556 | /** |
||
557 | * @param bool $isNew |
||
558 | * @return bool |
||
559 | */ |
||
560 | protected function saveRecord(bool $isNew): bool |
||
590 | |||
591 | /** |
||
592 | * @inheritdoc |
||
593 | * @return OrganizationRecord |
||
594 | */ |
||
595 | protected function elementToRecord(): OrganizationRecord |
||
609 | } |
||
610 |