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 | * @inheritdoc |
||
| 46 | */ |
||
| 47 | public static function displayName(): string |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritdoc |
||
| 54 | */ |
||
| 55 | public function getIsEditable(): bool |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritdoc |
||
| 62 | */ |
||
| 63 | public static function hasTitles(): bool |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @inheritdoc |
||
| 70 | */ |
||
| 71 | public static function isLocalized(): bool |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @inheritdoc |
||
| 78 | */ |
||
| 79 | public static function hasContent(): bool |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @inheritdoc |
||
| 86 | */ |
||
| 87 | public static function hasUris(): bool |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns whether this element type can have statuses. |
||
| 94 | * |
||
| 95 | * @return boolean |
||
| 96 | */ |
||
| 97 | public static function hasStatuses(): bool |
||
| 101 | |||
| 102 | |||
| 103 | /************************************************************ |
||
| 104 | * QUERY |
||
| 105 | ************************************************************/ |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @inheritdoc |
||
| 109 | * |
||
| 110 | * @return OrganizationQuery |
||
| 111 | */ |
||
| 112 | public static function find(): ElementQueryInterface |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param mixed $criteria |
||
| 119 | * @param bool $one |
||
| 120 | * @return Element|Element[]|null |
||
| 121 | */ |
||
| 122 | protected static function findByCondition($criteria, bool $one) |
||
| 134 | |||
| 135 | |||
| 136 | /************************************************************ |
||
| 137 | * PROPERTIES / ATTRIBUTES |
||
| 138 | ************************************************************/ |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function attributes() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @inheritdoc |
||
| 153 | * @throws \yii\base\InvalidConfigException |
||
| 154 | */ |
||
| 155 | public function rules() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function attributeLabels() |
||
| 188 | |||
| 189 | |||
| 190 | /************************************************************ |
||
| 191 | * FIELD LAYOUT |
||
| 192 | ************************************************************/ |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @inheritdoc |
||
| 196 | */ |
||
| 197 | public function getFieldLayout() |
||
| 205 | |||
| 206 | |||
| 207 | /************************************************************ |
||
| 208 | * ELEMENT ADMIN |
||
| 209 | ************************************************************/ |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritdoc |
||
| 213 | */ |
||
| 214 | public function getRef() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @inheritdoc |
||
| 225 | */ |
||
| 226 | public function getCpEditUrl() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @inheritdoc |
||
| 233 | */ |
||
| 234 | protected static function defineSources(string $context = null): array |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | private static function defineDefaultSources(): array |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | private static function defineTypeSources(): array |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | private static function defineUserSources(): array |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @inheritdoc |
||
| 315 | */ |
||
| 316 | protected static function defineActions(string $source = null): array |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @inheritdoc |
||
| 336 | */ |
||
| 337 | protected static function defineSearchableAttributes(): array |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @inheritdoc |
||
| 346 | */ |
||
| 347 | protected static function defineSortOptions(): array |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @inheritdoc |
||
| 357 | */ |
||
| 358 | public static function eagerLoadingMap(array $sourceElements, string $handle) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @inheritdoc |
||
| 370 | */ |
||
| 371 | public function setEagerLoadedElements(string $handle, array $elements) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @inheritdoc |
||
| 386 | */ |
||
| 387 | public static function defineTableAttributes(): array |
||
| 398 | |||
| 399 | |||
| 400 | |||
| 401 | // Indexes, etc. |
||
| 402 | // ------------------------------------------------------------------------- |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @inheritdoc |
||
| 406 | */ |
||
| 407 | public function tableAttributeHtml(string $attribute): string |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @inheritdoc |
||
| 429 | */ |
||
| 430 | public function getUriFormat() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @inheritdoc |
||
| 445 | */ |
||
| 446 | public function route() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @return \flipbox\organizations\records\OrganizationTypeSiteSettings|null |
||
| 477 | */ |
||
| 478 | protected function getSiteSettings() |
||
| 500 | |||
| 501 | /************************************************************ |
||
| 502 | * EVENTS |
||
| 503 | ************************************************************/ |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @inheritdoc |
||
| 507 | */ |
||
| 508 | public function beforeSave(bool $isNew): bool |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @inheritdoc |
||
| 519 | * @throws /Exception |
||
| 520 | */ |
||
| 521 | public function afterSave(bool $isNew) |
||
| 539 | |||
| 540 | /******************************************* |
||
| 541 | * RECORD |
||
| 542 | *******************************************/ |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @param bool $isNew |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | protected function saveRecord(bool $isNew): bool |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @inheritdoc |
||
| 581 | * @return OrganizationRecord |
||
| 582 | */ |
||
| 583 | protected function elementToRecord(): OrganizationRecord |
||
| 597 | } |
||
| 598 |