Complex classes like Member 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 Member, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Member extends BaseBlameableModel |
||
| 47 | { |
||
| 48 | public $createdByAttribute = 'organization_guid'; |
||
| 49 | public $updatedByAttribute = false; |
||
| 50 | public $hostClass = Organization::class; |
||
| 51 | |||
| 52 | public $memberAttribute = 'user_guid'; |
||
| 53 | public $memberUserClass = User::class; |
||
| 54 | public $contentAttribute = 'nickname'; |
||
| 55 | private $noInitMemberUser; |
||
| 56 | /** |
||
| 57 | * @return User |
||
| 58 | */ |
||
| 59 | 32 | protected function getNoInitMemberUser() |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @inheritdoc |
||
| 70 | */ |
||
| 71 | 32 | public function init() |
|
| 81 | |||
| 82 | public $descriptionAttribute = 'description'; |
||
| 83 | |||
| 84 | 32 | public function getMemberUserRules() |
|
| 91 | |||
| 92 | 32 | public function getMemberRoleRules() |
|
| 98 | |||
| 99 | 32 | public function getMemberPositionRules() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Set member user. |
||
| 109 | * @param User|string|integer $user |
||
| 110 | */ |
||
| 111 | 32 | public function setMemberUser($user) |
|
| 122 | |||
| 123 | 32 | public function getMemberUser() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Get Organization Query. |
||
| 130 | * Alias of `getHost` method. |
||
| 131 | * @return OrganizationQuery |
||
| 132 | */ |
||
| 133 | 24 | public function getOrganization() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Set Organization. |
||
| 140 | * @param BaseOrganization $organization |
||
| 141 | * @return boolean |
||
| 142 | */ |
||
| 143 | 32 | public function setOrganization($organization) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Assign role. |
||
| 150 | * The setting role operation will not take effect immediately. You should |
||
| 151 | * wrap this method and the subsequent save operations together into a |
||
| 152 | * transaction, in order to ensure data cosistency. |
||
| 153 | * @param Role $role |
||
| 154 | * @return boolean |
||
| 155 | 32 | */ |
|
| 156 | public function assignRole($role) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Assign administrator. |
||
| 174 | * @return boolean |
||
| 175 | * @throws \Exception |
||
| 176 | 32 | * @throws IntegrityException |
|
| 177 | */ |
||
| 178 | 32 | public function assignAdministrator() |
|
| 202 | 18 | ||
| 203 | 18 | /** |
|
| 204 | 18 | * Set role. |
|
| 205 | 18 | * @param Role $role |
|
| 206 | 16 | * @return boolean |
|
| 207 | */ |
||
| 208 | public function setRole($role = null) |
||
| 219 | 18 | ||
| 220 | /** |
||
| 221 | * Revoke role. |
||
| 222 | 32 | * @param Role $role |
|
| 223 | */ |
||
| 224 | 32 | public function revokeRole($role) |
|
| 254 | |||
| 255 | /** |
||
| 256 | 4 | * Revoke administrator. |
|
| 257 | * @return boolean |
||
| 258 | 4 | * @throws IntegrityException |
|
| 259 | */ |
||
| 260 | public function revokeAdministrator() |
||
| 284 | |||
| 285 | public function rules() |
||
| 289 | 13 | ||
| 290 | /** |
||
| 291 | 13 | * @inheritdoc |
|
| 292 | */ |
||
| 293 | public static function tableName() |
||
| 297 | |||
| 298 | 2 | /** |
|
| 299 | * @inheritdoc |
||
| 300 | 2 | */ |
|
| 301 | public function attributeLabels() |
||
| 318 | |||
| 319 | public function isDepartmentAdministrator() |
||
| 323 | |||
| 324 | public function isDepartmentCreator() |
||
| 328 | |||
| 329 | public function isOrganizationAdministrator() |
||
| 333 | |||
| 334 | public function isOrganizationCreator() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Check whether current member is administrator. |
||
| 341 | * @return boolean |
||
| 342 | */ |
||
| 343 | public function isAdministrator() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Check whether current member is creator. |
||
| 350 | * @return boolean |
||
| 351 | */ |
||
| 352 | public function isCreator() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * We think it a `member` if `role` property is empty. |
||
| 359 | * @return boolean |
||
| 360 | */ |
||
| 361 | public function isOnlyMember() |
||
| 365 | } |
||
| 366 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.