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 | 36 | protected function getNoInitMemberUser() |
|
67 | |||
68 | /** |
||
69 | * @inheritdoc |
||
70 | */ |
||
71 | 37 | public function init() |
|
81 | |||
82 | public $descriptionAttribute = 'description'; |
||
83 | |||
84 | 36 | public function getMemberUserRules() |
|
91 | |||
92 | 36 | public function getMemberRoleRules() |
|
98 | |||
99 | 36 | public function getMemberPositionRules() |
|
106 | |||
107 | /** |
||
108 | * Set member user. |
||
109 | * @param User|string|integer $user |
||
110 | */ |
||
111 | 36 | public function setMemberUser($user) |
|
122 | |||
123 | 36 | public function getMemberUser() |
|
127 | |||
128 | /** |
||
129 | * Get Organization Query. |
||
130 | * Alias of `getHost` method. |
||
131 | * @return OrganizationQuery |
||
132 | */ |
||
133 | 28 | public function getOrganization() |
|
137 | |||
138 | /** |
||
139 | * Set Organization. |
||
140 | * @param BaseOrganization $organization |
||
141 | * @return boolean |
||
142 | */ |
||
143 | 36 | 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 | */ |
||
156 | 36 | public function assignRole($role) |
|
171 | |||
172 | /** |
||
173 | * Assign administrator. |
||
174 | * @return boolean |
||
175 | * @throws \Exception |
||
176 | * @throws IntegrityException |
||
177 | */ |
||
178 | 7 | public function assignAdministrator() |
|
202 | |||
203 | /** |
||
204 | * Set role. |
||
205 | * @param Role $role |
||
206 | * @return boolean |
||
207 | */ |
||
208 | 36 | public function setRole($role = null) |
|
219 | |||
220 | /** |
||
221 | * Revoke role. |
||
222 | * @param Role $role |
||
223 | */ |
||
224 | 17 | public function revokeRole($role) |
|
254 | |||
255 | /** |
||
256 | * Revoke administrator. |
||
257 | * @return boolean |
||
258 | * @throws IntegrityException |
||
259 | */ |
||
260 | 1 | public function revokeAdministrator() |
|
284 | |||
285 | 36 | public function rules() |
|
289 | |||
290 | /** |
||
291 | * @inheritdoc |
||
292 | */ |
||
293 | 37 | public static function tableName() |
|
297 | |||
298 | /** |
||
299 | * @inheritdoc |
||
300 | */ |
||
301 | 1 | public function attributeLabels() |
|
318 | |||
319 | 6 | public function isDepartmentAdministrator() |
|
323 | |||
324 | 16 | public function isDepartmentCreator() |
|
328 | |||
329 | 6 | public function isOrganizationAdministrator() |
|
333 | |||
334 | 16 | public function isOrganizationCreator() |
|
338 | |||
339 | /** |
||
340 | * Check whether current member is administrator. |
||
341 | * @return boolean |
||
342 | */ |
||
343 | 6 | public function isAdministrator() |
|
347 | |||
348 | /** |
||
349 | * Check whether current member is creator. |
||
350 | * @return boolean |
||
351 | */ |
||
352 | 16 | public function isCreator() |
|
356 | |||
357 | /** |
||
358 | * We think it a `member` if `role` property is empty. |
||
359 | * @return boolean |
||
360 | */ |
||
361 | 2 | 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
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.