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 |
||
34 | class Organization extends Element |
||
35 | { |
||
36 | use DateJoinedAttributeTrait, |
||
37 | TypesAttributeTrait, |
||
38 | UsersAttributeTrait; |
||
39 | |||
40 | /** |
||
41 | * @inheritdoc |
||
42 | */ |
||
43 | public static function displayName(): string |
||
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | public function getIsEditable(): bool |
||
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | public static function hasTitles(): bool |
||
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | */ |
||
67 | public static function isLocalized(): bool |
||
71 | |||
72 | /** |
||
73 | * @inheritdoc |
||
74 | */ |
||
75 | public static function hasContent(): bool |
||
79 | |||
80 | /** |
||
81 | * @inheritdoc |
||
82 | */ |
||
83 | public static function hasUris(): bool |
||
87 | |||
88 | /** |
||
89 | * @inheritdoc |
||
90 | * |
||
91 | * @return OrganizationQuery |
||
92 | */ |
||
93 | public static function find(): ElementQueryInterface |
||
97 | |||
98 | /******************************************* |
||
99 | * GET |
||
100 | *******************************************/ |
||
101 | |||
102 | /** |
||
103 | * Returns a single element instance by a primary key or a set of element criteria parameters. |
||
104 | * |
||
105 | * The method accepts: |
||
106 | * |
||
107 | * - an int: query by a single ID value and return the corresponding element (or null if not found). |
||
108 | * - an array of name-value pairs: query by a set of parameter values and return the first element |
||
109 | * matching all of them (or null if not found). |
||
110 | * |
||
111 | * Note that this method will automatically call the `one()` method and return an |
||
112 | * [[ElementInterface|\craft\base\Element]] instance. For example, |
||
113 | * |
||
114 | * ```php |
||
115 | * // find a single entry whose ID is 10 |
||
116 | * $entry = Entry::findOne(10); |
||
117 | * // the above code is equivalent to: |
||
118 | * $entry = Entry::find->id(10)->one(); |
||
119 | * // find the first user whose email ends in "example.com" |
||
120 | * $user = User::findOne(['email' => '*example.com']); |
||
121 | * // the above code is equivalent to: |
||
122 | * $user = User::find()->email('*example.com')->one(); |
||
123 | * ``` |
||
124 | * |
||
125 | * @param mixed $criteria The element ID or a set of element criteria parameters |
||
126 | * @return static Element instance matching the condition, or null if nothing matches. |
||
127 | * @throws ElementNotFoundException |
||
128 | */ |
||
129 | public static function getOne($criteria) |
||
142 | |||
143 | /** |
||
144 | * Returns a list of elements that match the specified ID(s) or a set of element criteria parameters. |
||
145 | * |
||
146 | * The method accepts: |
||
147 | * |
||
148 | * - an int: query by a single ID value and return an array containing the corresponding element |
||
149 | * (or an empty array if not found). |
||
150 | * - an array of integers: query by a list of ID values and return the corresponding elements (or an |
||
151 | * empty array if none was found). |
||
152 | * Note that an empty array will result in an empty result as it will be interpreted as a search for |
||
153 | * primary keys and not an empty set of element criteria parameters. |
||
154 | * - an array of name-value pairs: query by a set of parameter values and return an array of elements |
||
155 | * matching all of them (or an empty array if none was found). |
||
156 | * |
||
157 | * Note that this method will automatically call the `all()` method and return an array of |
||
158 | * [[ElementInterface|\craft\base\Element]] instances. For example, |
||
159 | * |
||
160 | * ```php |
||
161 | * // find the entries whose ID is 10 |
||
162 | * $entries = Entry::findAll(10); |
||
163 | * // the above code is equivalent to: |
||
164 | * $entries = Entry::find()->id(10)->all(); |
||
165 | * // find the entries whose ID is 10, 11 or 12. |
||
166 | * $entries = Entry::findAll([10, 11, 12]); |
||
167 | * // the above code is equivalent to: |
||
168 | * $entries = Entry::find()->id([10, 11, 12]])->all(); |
||
169 | * // find users whose email ends in "example.com" |
||
170 | * $users = User::findAll(['email' => '*example.com']); |
||
171 | * // the above code is equivalent to: |
||
172 | * $users = User::find()->email('*example.com')->all(); |
||
173 | * ``` |
||
174 | * |
||
175 | * @param mixed $criteria The element ID, an array of IDs, or a set of element criteria parameters |
||
176 | * @return static[] an array of Element instances, or an empty array if nothing matches. |
||
177 | * @throws ElementNotFoundException |
||
178 | */ |
||
179 | public static function getAll($criteria) |
||
194 | |||
195 | /** |
||
196 | * @param mixed $criteria |
||
197 | * @param bool $one |
||
198 | * @return Element|Element[]|null |
||
199 | */ |
||
200 | protected static function findByCondition($criteria, bool $one) |
||
212 | |||
213 | /** |
||
214 | * Returns whether this element type can have statuses. |
||
215 | * |
||
216 | * @return boolean |
||
217 | */ |
||
218 | public static function hasStatuses(): bool |
||
222 | |||
223 | /** |
||
224 | * @inheritdoc |
||
225 | */ |
||
226 | public function getRef() |
||
234 | |||
235 | /** |
||
236 | * @return array |
||
237 | */ |
||
238 | public function attributes() |
||
251 | |||
252 | /** |
||
253 | * @inheritdoc |
||
254 | * @throws \yii\base\InvalidConfigException |
||
255 | */ |
||
256 | public function rules() |
||
278 | |||
279 | /** |
||
280 | * @return array |
||
281 | */ |
||
282 | public function attributeLabels() |
||
295 | |||
296 | /************************************************************ |
||
297 | * FIELD LAYOUT |
||
298 | ************************************************************/ |
||
299 | |||
300 | /** |
||
301 | * @inheritdoc |
||
302 | */ |
||
303 | public function getFieldLayout() |
||
311 | |||
312 | /************************************************************ |
||
313 | * ELEMENT ADMIN |
||
314 | ************************************************************/ |
||
315 | |||
316 | /** |
||
317 | * @inheritdoc |
||
318 | */ |
||
319 | public function getCpEditUrl() |
||
323 | |||
324 | /** |
||
325 | * @inheritdoc |
||
326 | */ |
||
327 | protected static function defineSources(string $context = null): array |
||
337 | |||
338 | /** |
||
339 | * @return array |
||
340 | */ |
||
341 | private static function defineDefaultSources(): array |
||
352 | |||
353 | /** |
||
354 | * @return array |
||
355 | */ |
||
356 | private static function defineTypeSources(): array |
||
377 | |||
378 | /** |
||
379 | * @return array |
||
380 | */ |
||
381 | private static function defineUserSources(): array |
||
405 | |||
406 | /** |
||
407 | * @inheritdoc |
||
408 | */ |
||
409 | protected static function defineActions(string $source = null): array |
||
426 | |||
427 | /** |
||
428 | * @inheritdoc |
||
429 | */ |
||
430 | protected static function defineSearchableAttributes(): array |
||
436 | |||
437 | /** |
||
438 | * @inheritdoc |
||
439 | */ |
||
440 | protected static function defineSortOptions(): array |
||
447 | |||
448 | /** |
||
449 | * @inheritdoc |
||
450 | */ |
||
451 | public static function eagerLoadingMap(array $sourceElements, string $handle) |
||
460 | |||
461 | /** |
||
462 | * @inheritdoc |
||
463 | */ |
||
464 | public function setEagerLoadedElements(string $handle, array $elements) |
||
476 | |||
477 | /** |
||
478 | * @inheritdoc |
||
479 | */ |
||
480 | public static function defineTableAttributes(): array |
||
491 | |||
492 | |||
493 | |||
494 | // Indexes, etc. |
||
495 | // ------------------------------------------------------------------------- |
||
496 | |||
497 | /** |
||
498 | * @inheritdoc |
||
499 | */ |
||
500 | public function tableAttributeHtml(string $attribute): string |
||
519 | |||
520 | /** |
||
521 | * @inheritdoc |
||
522 | */ |
||
523 | public function getUriFormat() |
||
535 | |||
536 | /** |
||
537 | * @inheritdoc |
||
538 | */ |
||
539 | public function route() |
||
567 | |||
568 | /** |
||
569 | * @return \flipbox\organizations\records\OrganizationTypeSiteSettings|null |
||
570 | */ |
||
571 | protected function getSiteSettings() |
||
593 | |||
594 | /************************************************************ |
||
595 | * EVENTS |
||
596 | ************************************************************/ |
||
597 | |||
598 | /** |
||
599 | * @inheritdoc |
||
600 | */ |
||
601 | public function beforeSave(bool $isNew): bool |
||
609 | |||
610 | /** |
||
611 | * @inheritdoc |
||
612 | * @throws /Exception |
||
613 | */ |
||
614 | public function afterSave(bool $isNew) |
||
632 | |||
633 | /******************************************* |
||
634 | * RECORD |
||
635 | *******************************************/ |
||
636 | |||
637 | /** |
||
638 | * @param bool $isNew |
||
639 | * @return bool |
||
640 | */ |
||
641 | protected function saveRecord(bool $isNew): bool |
||
671 | |||
672 | /** |
||
673 | * @inheritdoc |
||
674 | * @return OrganizationRecord |
||
675 | */ |
||
676 | protected function elementToRecord(): OrganizationRecord |
||
690 | } |
||
691 |