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) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @inheritdoc |
||
| 423 | */ |
||
| 424 | public static function defineTableAttributes(): array |
||
| 435 | |||
| 436 | |||
| 437 | |||
| 438 | // Indexes, etc. |
||
| 439 | // ------------------------------------------------------------------------- |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @inheritdoc |
||
| 443 | */ |
||
| 444 | public function tableAttributeHtml(string $attribute): string |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @inheritdoc |
||
| 466 | */ |
||
| 467 | public function getUriFormat() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @inheritdoc |
||
| 482 | */ |
||
| 483 | public function route() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @return \flipbox\organizations\records\OrganizationTypeSiteSettings|null |
||
| 514 | */ |
||
| 515 | protected function getSiteSettings() |
||
| 537 | |||
| 538 | |||
| 539 | /************************************************************ |
||
| 540 | * EVENTS |
||
| 541 | ************************************************************/ |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @inheritdoc |
||
| 545 | */ |
||
| 546 | public function save(bool $runValidation = true, bool $propagate = true): bool |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @inheritdoc |
||
| 553 | */ |
||
| 554 | public function beforeSave(bool $isNew): bool |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @inheritdoc |
||
| 565 | * @throws /Exception |
||
| 566 | */ |
||
| 567 | public function afterSave(bool $isNew) |
||
| 585 | |||
| 586 | |||
| 587 | /******************************************* |
||
| 588 | * RELATIONSHIP |
||
| 589 | *******************************************/ |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @return static |
||
| 593 | */ |
||
| 594 | public function withTypes(): self |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @return static |
||
| 602 | */ |
||
| 603 | public function withoutTypes(): self |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @return static |
||
| 611 | */ |
||
| 612 | public function withUsers(): self |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @return static |
||
| 620 | */ |
||
| 621 | public function withoutUsers(): self |
||
| 626 | |||
| 627 | /******************************************* |
||
| 628 | * RECORD |
||
| 629 | *******************************************/ |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @param bool $isNew |
||
| 633 | * @return bool |
||
| 634 | * @throws \Exception |
||
| 635 | */ |
||
| 636 | protected function saveRecord(bool $isNew): bool |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @inheritdoc |
||
| 669 | * @return OrganizationRecord |
||
| 670 | */ |
||
| 671 | protected function elementToRecord(): OrganizationRecord |
||
| 685 | } |
||
| 686 |