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 |
||
| 48 | class Member extends BaseBlameableModel |
||
| 49 | { |
||
| 50 | public $createdByAttribute = 'organization_guid'; |
||
| 51 | public $updatedByAttribute = false; |
||
| 52 | public $hostClass = Organization::class; |
||
| 53 | |||
| 54 | public $memberAttribute = 'user_guid'; |
||
| 55 | public $memberUserClass = User::class; |
||
| 56 | public $contentAttribute = 'nickname'; |
||
| 57 | private $noInitMemberUser; |
||
| 58 | /** |
||
| 59 | * @return User |
||
| 60 | */ |
||
| 61 | 36 | protected function getNoInitMemberUser() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritdoc |
||
| 72 | */ |
||
| 73 | 37 | public function init() |
|
| 83 | |||
| 84 | public $descriptionAttribute = 'description'; |
||
| 85 | |||
| 86 | 36 | public function getMemberUserRules() |
|
| 93 | |||
| 94 | 36 | public function getMemberRoleRules() |
|
| 100 | |||
| 101 | 36 | public function getMemberPositionRules() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Set member user. |
||
| 111 | * @param User|string|integer $user |
||
| 112 | */ |
||
| 113 | 36 | public function setMemberUser($user) |
|
| 124 | |||
| 125 | 36 | public function getMemberUser() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get Organization Query. |
||
| 132 | * Alias of `getHost` method. |
||
| 133 | * @return OrganizationQuery |
||
| 134 | */ |
||
| 135 | 28 | public function getOrganization() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Set Organization. |
||
| 142 | * @param BaseOrganization $organization |
||
| 143 | * @return boolean |
||
| 144 | */ |
||
| 145 | 36 | public function setOrganization($organization) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Assign role. |
||
| 152 | * The setting role operation will not take effect immediately. You should |
||
| 153 | * wrap this method and the subsequent save operations together into a |
||
| 154 | * transaction, in order to ensure data cosistency. |
||
| 155 | * @param Role $role |
||
| 156 | * @return boolean |
||
| 157 | */ |
||
| 158 | 36 | public function assignRole($role) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Assign administrator. |
||
| 176 | * @return boolean |
||
| 177 | * @throws \Exception |
||
| 178 | * @throws IntegrityException |
||
| 179 | */ |
||
| 180 | 7 | public function assignAdministrator() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Set role. |
||
| 207 | * @param Role $role |
||
| 208 | * @return boolean |
||
| 209 | */ |
||
| 210 | 36 | public function setRole($role = null) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Revoke role. |
||
| 224 | * @param Role $role |
||
| 225 | * @throws InvalidParamException |
||
| 226 | * @throws IntegrityException |
||
| 227 | * @throws \Exception |
||
| 228 | * @return boolean |
||
| 229 | */ |
||
| 230 | 17 | public function revokeRole($role) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Revoke administrator. |
||
| 263 | * @return boolean |
||
| 264 | * @throws IntegrityException |
||
| 265 | * @throws \Exception |
||
| 266 | */ |
||
| 267 | 1 | public function revokeAdministrator() |
|
| 291 | |||
| 292 | 36 | public function rules() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @inheritdoc |
||
| 299 | */ |
||
| 300 | 37 | public static function tableName() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Find. |
||
| 307 | * Friendly to IDE. |
||
| 308 | * @return MemberQuery |
||
| 309 | */ |
||
| 310 | 37 | public static function find() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @inheritdoc |
||
| 317 | */ |
||
| 318 | 1 | public function attributeLabels() |
|
| 335 | |||
| 336 | 6 | public function isDepartmentAdministrator() |
|
| 340 | |||
| 341 | 17 | public function isDepartmentCreator() |
|
| 345 | |||
| 346 | 6 | public function isOrganizationAdministrator() |
|
| 350 | |||
| 351 | 17 | public function isOrganizationCreator() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Check whether current member is administrator. |
||
| 358 | * @return boolean |
||
| 359 | */ |
||
| 360 | 6 | public function isAdministrator() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Check whether current member is creator. |
||
| 367 | * @return boolean |
||
| 368 | */ |
||
| 369 | 17 | public function isCreator() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * We think it a `member` if `role` property is empty. |
||
| 376 | * @return boolean |
||
| 377 | */ |
||
| 378 | 2 | public function isOnlyMember() |
|
| 382 | } |
||
| 383 |
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.