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 |
||
49 | class Member extends BaseBlameableModel |
||
50 | { |
||
51 | public $createdByAttribute = 'organization_guid'; |
||
52 | public $updatedByAttribute = false; |
||
53 | public $hostClass = Organization::class; |
||
54 | |||
55 | public $memberAttribute = 'user_guid'; |
||
56 | public $memberUserClass = User::class; |
||
57 | public $contentAttribute = 'nickname'; |
||
58 | public $searchClass = MemberSearch::class; |
||
59 | private $noInitMemberUser; |
||
60 | /** |
||
61 | * @return User |
||
62 | */ |
||
63 | 50 | public function getNoInitMemberUser() |
|
71 | |||
72 | /** |
||
73 | * @return null|MemberSearch |
||
74 | */ |
||
75 | public function getSearchModel() |
||
83 | |||
84 | /** |
||
85 | * @inheritdoc |
||
86 | */ |
||
87 | 51 | public function init() |
|
97 | |||
98 | public $descriptionAttribute = 'description'; |
||
99 | |||
100 | 50 | public function getMemberUserRules() |
|
107 | |||
108 | 50 | public function getMemberRoleRules() |
|
114 | |||
115 | 50 | public function getMemberPositionRules() |
|
122 | |||
123 | /** |
||
124 | * Set member user. |
||
125 | * @param User|string|integer $user |
||
126 | */ |
||
127 | 50 | public function setMemberUser($user) |
|
138 | |||
139 | 50 | public function getMemberUser() |
|
143 | |||
144 | /** |
||
145 | * Get Organization Query. |
||
146 | * Alias of `getHost` method. |
||
147 | * @return OrganizationQuery |
||
148 | */ |
||
149 | 43 | public function getOrganization() |
|
153 | |||
154 | /** |
||
155 | * Set Organization. |
||
156 | * @param BaseOrganization $organization |
||
157 | * @return boolean |
||
158 | */ |
||
159 | 50 | public function setOrganization($organization) |
|
163 | |||
164 | /** |
||
165 | * Assign role. |
||
166 | * The setting role operation will not take effect immediately. You should |
||
167 | * wrap this method and the subsequent save operations together into a |
||
168 | * transaction, in order to ensure data cosistency. |
||
169 | * @param Role $role |
||
170 | * @return boolean |
||
171 | */ |
||
172 | 50 | public function assignRole($role) |
|
187 | |||
188 | /** |
||
189 | * Assign administrator. |
||
190 | * @return boolean |
||
191 | * @throws \Exception |
||
192 | * @throws IntegrityException |
||
193 | */ |
||
194 | 17 | public function assignAdministrator() |
|
224 | |||
225 | /** |
||
226 | * Set role. |
||
227 | * @param Role $role |
||
228 | * @return boolean |
||
229 | */ |
||
230 | 50 | public function setRole($role = null) |
|
241 | |||
242 | /** |
||
243 | * Revoke role. |
||
244 | * @param Role $role |
||
245 | * @throws InvalidParamException |
||
246 | * @throws IntegrityException |
||
247 | * @throws \Exception |
||
248 | * @return boolean |
||
249 | */ |
||
250 | 20 | public function revokeRole($role) |
|
280 | |||
281 | /** |
||
282 | * Revoke administrator. |
||
283 | * @return boolean |
||
284 | * @throws IntegrityException |
||
285 | * @throws \Exception |
||
286 | */ |
||
287 | 1 | public function revokeAdministrator() |
|
317 | |||
318 | 50 | public function rules() |
|
322 | |||
323 | /** |
||
324 | * @inheritdoc |
||
325 | */ |
||
326 | 51 | public static function tableName() |
|
330 | |||
331 | /** |
||
332 | * Find. |
||
333 | * Friendly to IDE. |
||
334 | * @return MemberQuery |
||
335 | */ |
||
336 | 51 | public static function find() |
|
340 | |||
341 | /** |
||
342 | * @inheritdoc |
||
343 | */ |
||
344 | 1 | public function attributeLabels() |
|
361 | |||
362 | /** |
||
363 | * @return bool |
||
364 | */ |
||
365 | 18 | public function isDepartmentAdministrator() |
|
369 | |||
370 | /** |
||
371 | * @return bool |
||
372 | */ |
||
373 | 34 | public function isDepartmentCreator() |
|
377 | |||
378 | /** |
||
379 | * @return bool |
||
380 | */ |
||
381 | 18 | public function isOrganizationAdministrator() |
|
385 | |||
386 | /** |
||
387 | * @return bool |
||
388 | */ |
||
389 | 34 | public function isOrganizationCreator() |
|
393 | |||
394 | /** |
||
395 | * Check whether current member is administrator. |
||
396 | * @return boolean |
||
397 | */ |
||
398 | 18 | public function isAdministrator() |
|
402 | |||
403 | /** |
||
404 | * Check whether current member is creator. |
||
405 | * @return boolean |
||
406 | */ |
||
407 | 34 | public function isCreator() |
|
411 | |||
412 | /** |
||
413 | * We think it a `member` if `role` property is empty. |
||
414 | * @return boolean |
||
415 | */ |
||
416 | 2 | public function isOnlyMember() |
|
420 | |||
421 | const SCENARIO_UPDATE = 'update'; |
||
422 | const SCENARIO_ADMIN_UPDATE = 'admin_update'; |
||
423 | |||
424 | 50 | public function scenarios() |
|
432 | } |
||
433 |
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.