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 |
||
| 41 | class Organization extends Element |
||
| 42 | { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $status; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var DateTime|null |
||
| 51 | */ |
||
| 52 | public $dateJoined; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | public $ownerId; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var User |
||
| 61 | */ |
||
| 62 | private $owner; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var TypeModel[] |
||
| 66 | */ |
||
| 67 | private $types; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var TypeModel |
||
| 71 | */ |
||
| 72 | private $activeType; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var TypeModel |
||
| 76 | */ |
||
| 77 | private $primaryType; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var UserQuery |
||
| 81 | */ |
||
| 82 | private $users; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var UserQuery |
||
| 86 | */ |
||
| 87 | private $members; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @inheritdoc |
||
| 91 | */ |
||
| 92 | public static function displayName(): string |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @inheritdoc |
||
| 99 | */ |
||
| 100 | public static function hasTitles(): bool |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @inheritdoc |
||
| 107 | */ |
||
| 108 | public static function hasContent(): bool |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function attributes() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @inheritdoc |
||
| 131 | */ |
||
| 132 | public function rules() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Returns the names of any attributes that should be converted to DateTime objects from [[populate()]]. |
||
| 181 | * |
||
| 182 | * @return string[] |
||
| 183 | */ |
||
| 184 | public function datetimeAttributes(): array |
||
| 194 | |||
| 195 | |||
| 196 | /************************************************************ |
||
| 197 | * FIELD LAYOUT |
||
| 198 | ************************************************************/ |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | public function getFieldLayout(int $siteId = null) |
||
| 213 | |||
| 214 | |||
| 215 | /************************************************************ |
||
| 216 | * FIND / GET |
||
| 217 | ************************************************************/ |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @inheritdoc |
||
| 221 | * |
||
| 222 | * @return OrganizationQuery |
||
| 223 | */ |
||
| 224 | public static function find(): ElementQueryInterface |
||
| 228 | |||
| 229 | |||
| 230 | /************************************************************ |
||
| 231 | * STATUS |
||
| 232 | ************************************************************/ |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns whether this element type can have statuses. |
||
| 236 | * |
||
| 237 | * @return boolean |
||
| 238 | */ |
||
| 239 | public static function hasStatuses(): bool |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @inheritdoc |
||
| 246 | */ |
||
| 247 | public static function statuses(): array |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param $status |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | public function setStatus($status) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @inheritdoc |
||
| 290 | */ |
||
| 291 | public function getStatus() |
||
| 300 | |||
| 301 | /************************************************************ |
||
| 302 | * ACTIVE TYPE |
||
| 303 | ************************************************************/ |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param TypeModel|null $type |
||
| 307 | * @return $this |
||
| 308 | */ |
||
| 309 | public function setActiveType(TypeModel $type = null) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return TypeModel|null |
||
| 322 | */ |
||
| 323 | public function getActiveType() |
||
| 338 | |||
| 339 | |||
| 340 | /************************************************************ |
||
| 341 | * PRIMARY TYPE |
||
| 342 | ************************************************************/ |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Populate the primary type |
||
| 346 | */ |
||
| 347 | protected function ensurePrimaryType() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Identify whether a primary type is set |
||
| 363 | * |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | public function hasPrimaryType() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Identify whether the type is primary |
||
| 376 | * |
||
| 377 | * @param $type |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function isPrimaryType(TypeModel $type) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param TypeModel $type |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | public function setPrimaryType(TypeModel $type) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get the primary type |
||
| 409 | * |
||
| 410 | * @return TypeModel|null |
||
| 411 | */ |
||
| 412 | public function getPrimaryType() |
||
| 421 | |||
| 422 | /************************************************************ |
||
| 423 | * TYPES |
||
| 424 | ************************************************************/ |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Associate a type to the element |
||
| 428 | * |
||
| 429 | * @param TypeModel $type |
||
| 430 | * @return $this |
||
| 431 | */ |
||
| 432 | public function addType(TypeModel $type) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Set the types associated to the element |
||
| 447 | * |
||
| 448 | * @param null $types |
||
| 449 | * @return $this |
||
| 450 | */ |
||
| 451 | public function setTypes($types = null) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get all associated types associated to the element |
||
| 472 | * |
||
| 473 | * @return TypeModel[] |
||
| 474 | */ |
||
| 475 | public function getTypes(): array |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Ensure all types are associated to the element |
||
| 485 | * |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | private function ensureTypes() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Get an associated type by identifier (id/handle) |
||
| 503 | * |
||
| 504 | * @param $identifier |
||
| 505 | * @return null|TypeModel |
||
| 506 | */ |
||
| 507 | public function getType($identifier) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Identify whether a type is associated to the element |
||
| 524 | * |
||
| 525 | * @param TypeModel|null $type |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | public function hasType(TypeModel $type = null): bool |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @param TypeModel|null $type |
||
| 541 | * @return bool |
||
| 542 | * @deprecated |
||
| 543 | */ |
||
| 544 | public function getHasType(TypeModel $type = null): bool |
||
| 554 | |||
| 555 | |||
| 556 | /************************************************************ |
||
| 557 | * MEMBERS |
||
| 558 | ************************************************************/ |
||
| 559 | /** |
||
| 560 | * Get an array of users associated to an organization |
||
| 561 | * |
||
| 562 | * @param array $criteria |
||
| 563 | * @return UserQuery |
||
| 564 | */ |
||
| 565 | public function getMembers($criteria = []) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Associate users to an organization |
||
| 584 | * |
||
| 585 | * @param $members |
||
| 586 | * @return $this |
||
| 587 | */ |
||
| 588 | protected function setMembers($members) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Associate an array of users to an organization |
||
| 604 | * |
||
| 605 | * @param $members |
||
| 606 | * @return $this |
||
| 607 | */ |
||
| 608 | protected function addMembers(array $members) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Associate a user to an organization |
||
| 630 | * |
||
| 631 | * @param User $user |
||
| 632 | * @param bool $addAsUser |
||
| 633 | * @return $this |
||
| 634 | */ |
||
| 635 | protected function addMember(User $user, bool $addAsUser = true) |
||
| 658 | |||
| 659 | |||
| 660 | /************************************************************ |
||
| 661 | * USERS |
||
| 662 | ************************************************************/ |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Get an array of users associated to an organization |
||
| 666 | * |
||
| 667 | * @param array $criteria |
||
| 668 | * @return UserQuery |
||
| 669 | */ |
||
| 670 | public function getUsers($criteria = []) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @param User $user |
||
| 689 | * @param array $criteria |
||
| 690 | * @return bool |
||
| 691 | */ |
||
| 692 | public function isUser(User $user, $criteria = []) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Associate users to an organization |
||
| 705 | * |
||
| 706 | * @param $users |
||
| 707 | * @return $this |
||
| 708 | */ |
||
| 709 | public function setUsers($users) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Associate an array of users to an organization |
||
| 725 | * |
||
| 726 | * @param $users |
||
| 727 | * @return $this |
||
| 728 | */ |
||
| 729 | public function addUsers(array $users) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Associate a user to an organization |
||
| 751 | * |
||
| 752 | * @param User $user |
||
| 753 | * @param bool $addAsMember |
||
| 754 | * @return $this |
||
| 755 | */ |
||
| 756 | public function addUser(User $user, bool $addAsMember = true) |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Dissociate a user from an organization |
||
| 781 | * |
||
| 782 | * @param array $users |
||
| 783 | * @return $this |
||
| 784 | */ |
||
| 785 | public function removeUsers(array $users) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Dissociate a user from an organization |
||
| 807 | * |
||
| 808 | * @param User $user |
||
| 809 | * @return $this |
||
| 810 | */ |
||
| 811 | public function removeUser(User $user) |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Associate an array of types from request input |
||
| 833 | * |
||
| 834 | * @param string $identifier |
||
| 835 | * @return $this |
||
| 836 | */ |
||
| 837 | public function setTypesFromRequest(string $identifier = 'types') |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Associate an array of users from request input |
||
| 850 | * |
||
| 851 | * @param string $identifier |
||
| 852 | * @return $this |
||
| 853 | */ |
||
| 854 | public function setUsersFromRequest(string $identifier = 'users') |
||
| 864 | |||
| 865 | |||
| 866 | /************************************************************ |
||
| 867 | * ELEMENT ADMIN |
||
| 868 | ************************************************************/ |
||
| 869 | |||
| 870 | /** |
||
| 871 | * @inheritdoc |
||
| 872 | */ |
||
| 873 | public function getCpEditUrl() |
||
| 877 | |||
| 878 | /** |
||
| 879 | * @inheritdoc |
||
| 880 | */ |
||
| 881 | protected static function defineSources(string $context = null): array |
||
| 895 | |||
| 896 | /** |
||
| 897 | * @return array |
||
| 898 | */ |
||
| 899 | private static function defineDefaultSources(): array |
||
| 911 | |||
| 912 | /** |
||
| 913 | * @return array |
||
| 914 | */ |
||
| 915 | private static function defineTypeSources(): array |
||
| 937 | |||
| 938 | /** |
||
| 939 | * @return array |
||
| 940 | */ |
||
| 941 | private static function defineUserSources(): array |
||
| 966 | |||
| 967 | /** |
||
| 968 | * @return array |
||
| 969 | */ |
||
| 970 | private static function defineOwnerSources(): array |
||
| 995 | |||
| 996 | /** |
||
| 997 | * @inheritdoc |
||
| 998 | */ |
||
| 999 | protected static function defineActions(string $source = null): array |
||
| 1022 | |||
| 1023 | /** |
||
| 1024 | * @inheritdoc |
||
| 1025 | */ |
||
| 1026 | protected static function defineSearchableAttributes(): array |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * @inheritdoc |
||
| 1036 | */ |
||
| 1037 | protected static function defineSortOptions(): array |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * @inheritdoc |
||
| 1051 | */ |
||
| 1052 | public static function eagerLoadingMap(array $sourceElements, string $handle) |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * @param array $sourceElements |
||
| 1074 | * @return array |
||
| 1075 | */ |
||
| 1076 | private static function eagerLoadingOwnerMap(array $sourceElements) |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * @param array $sourceElements |
||
| 1096 | * @return array |
||
| 1097 | */ |
||
| 1098 | private static function eagerLoadingUsersMap(array $sourceElements) |
||
| 1115 | |||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * @inheritdoc |
||
| 1119 | */ |
||
| 1120 | public function setEagerLoadedElements(string $handle, array $elements) |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * @inheritdoc |
||
| 1146 | */ |
||
| 1147 | public static function defineTableAttributes(): array |
||
| 1161 | |||
| 1162 | |||
| 1163 | |||
| 1164 | // Indexes, etc. |
||
| 1165 | // ------------------------------------------------------------------------- |
||
| 1166 | |||
| 1167 | /** |
||
| 1168 | * @inheritdoc |
||
| 1169 | */ |
||
| 1170 | public function tableAttributeHtml(string $attribute): string |
||
| 1207 | |||
| 1208 | |||
| 1209 | /** |
||
| 1210 | * @inheritdoc |
||
| 1211 | */ |
||
| 1212 | protected function route() |
||
| 1242 | |||
| 1243 | // Events |
||
| 1244 | // ------------------------------------------------------------------------- |
||
| 1245 | |||
| 1246 | /** |
||
| 1247 | * @inheritdoc |
||
| 1248 | * @throws Exception |
||
| 1249 | */ |
||
| 1250 | public function beforeSave(bool $isNew): bool |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * @inheritdoc |
||
| 1260 | * @throws Exception |
||
| 1261 | */ |
||
| 1262 | public function afterSave(bool $isNew) |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * Identify whether element has an owner |
||
| 1272 | * |
||
| 1273 | * @return bool |
||
| 1274 | */ |
||
| 1275 | public function hasOwner() |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * @return bool|User|null |
||
| 1287 | */ |
||
| 1288 | public function getOwner() |
||
| 1323 | |||
| 1324 | /** |
||
| 1325 | * Associate an owner to the element |
||
| 1326 | * |
||
| 1327 | * @param $owner |
||
| 1328 | * @return $this |
||
| 1329 | */ |
||
| 1330 | public function setOwner($owner) |
||
| 1350 | |||
| 1351 | /** |
||
| 1352 | * @param string $user |
||
| 1353 | * @return bool |
||
| 1354 | */ |
||
| 1355 | public function getIsOwner($user = 'CURRENT_USER') |
||
| 1368 | |||
| 1369 | /** |
||
| 1370 | * @param string|User $user |
||
| 1371 | * @return bool |
||
| 1372 | */ |
||
| 1373 | public function isOwner($user = 'CURRENT_USER') |
||
| 1377 | |||
| 1378 | /** |
||
| 1379 | * @param $user |
||
| 1380 | * @return User|null |
||
| 1381 | */ |
||
| 1382 | private function findUserElement($user) |
||
| 1400 | } |
||
| 1401 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: