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 |
||
47 | class Member extends BaseBlameableModel |
||
48 | { |
||
49 | public $createdByAttribute = 'organization_guid'; |
||
50 | public $updatedByAttribute = false; |
||
51 | public $hostClass = Organization::class; |
||
52 | |||
53 | public $memberAttribute = 'user_guid'; |
||
54 | public $memberUserClass = User::class; |
||
55 | public $contentAttribute = 'nickname'; |
||
56 | private $noInitMemberUser; |
||
57 | /** |
||
58 | * @return User |
||
59 | */ |
||
60 | 36 | protected function getNoInitMemberUser() |
|
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | 37 | public function init() |
|
82 | |||
83 | public $descriptionAttribute = 'description'; |
||
84 | |||
85 | 36 | public function getMemberUserRules() |
|
92 | |||
93 | 36 | public function getMemberRoleRules() |
|
99 | |||
100 | 36 | public function getMemberPositionRules() |
|
107 | |||
108 | /** |
||
109 | * Set member user. |
||
110 | * @param User|string|integer $user |
||
111 | */ |
||
112 | 36 | public function setMemberUser($user) |
|
123 | |||
124 | 36 | public function getMemberUser() |
|
128 | |||
129 | /** |
||
130 | * Get Organization Query. |
||
131 | * Alias of `getHost` method. |
||
132 | * @return OrganizationQuery |
||
133 | */ |
||
134 | 28 | public function getOrganization() |
|
138 | |||
139 | /** |
||
140 | * Set Organization. |
||
141 | * @param BaseOrganization $organization |
||
142 | * @return boolean |
||
143 | */ |
||
144 | 36 | public function setOrganization($organization) |
|
148 | |||
149 | /** |
||
150 | * Assign role. |
||
151 | * The setting role operation will not take effect immediately. You should |
||
152 | * wrap this method and the subsequent save operations together into a |
||
153 | * transaction, in order to ensure data cosistency. |
||
154 | * @param Role $role |
||
155 | * @return boolean |
||
156 | */ |
||
157 | 36 | public function assignRole($role) |
|
172 | |||
173 | /** |
||
174 | * Assign administrator. |
||
175 | * @return boolean |
||
176 | * @throws \Exception |
||
177 | * @throws IntegrityException |
||
178 | */ |
||
179 | 7 | public function assignAdministrator() |
|
203 | |||
204 | /** |
||
205 | * Set role. |
||
206 | * @param Role $role |
||
207 | * @return boolean |
||
208 | */ |
||
209 | 36 | public function setRole($role = null) |
|
220 | |||
221 | /** |
||
222 | * Revoke role. |
||
223 | * @param Role $role |
||
224 | * @throws InvalidParamException |
||
225 | * @throws IntegrityException |
||
226 | * @throws \Exception |
||
227 | * @return boolean |
||
228 | */ |
||
229 | 17 | public function revokeRole($role) |
|
259 | |||
260 | /** |
||
261 | * Revoke administrator. |
||
262 | * @return boolean |
||
263 | * @throws IntegrityException |
||
264 | * @throws \Exception |
||
265 | */ |
||
266 | 1 | public function revokeAdministrator() |
|
290 | |||
291 | 36 | public function rules() |
|
295 | |||
296 | /** |
||
297 | * @inheritdoc |
||
298 | */ |
||
299 | 37 | public static function tableName() |
|
303 | |||
304 | /** |
||
305 | * @inheritdoc |
||
306 | */ |
||
307 | 1 | public function attributeLabels() |
|
324 | |||
325 | 6 | public function isDepartmentAdministrator() |
|
329 | |||
330 | 16 | public function isDepartmentCreator() |
|
334 | |||
335 | 6 | public function isOrganizationAdministrator() |
|
339 | |||
340 | 16 | public function isOrganizationCreator() |
|
344 | |||
345 | /** |
||
346 | * Check whether current member is administrator. |
||
347 | * @return boolean |
||
348 | */ |
||
349 | 6 | public function isAdministrator() |
|
353 | |||
354 | /** |
||
355 | * Check whether current member is creator. |
||
356 | * @return boolean |
||
357 | */ |
||
358 | 16 | public function isCreator() |
|
362 | |||
363 | /** |
||
364 | * We think it a `member` if `role` property is empty. |
||
365 | * @return boolean |
||
366 | */ |
||
367 | 2 | public function isOnlyMember() |
|
371 | } |
||
372 |
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.