Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
1 | <?php namespace BB\Entities; |
||
45 | class User extends Model implements AuthenticatableContract, CanResetPasswordContract { |
||
46 | |||
47 | use UserRoleTrait, PresentableTrait, Authenticatable, CanResetPassword; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * The database table used by the model. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $table = 'users'; |
||
56 | |||
57 | /** |
||
58 | * The attributes excluded from the model's JSON form. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $hidden = array('password', 'remember_token'); |
||
63 | |||
64 | protected $auditFields = array('induction_completed', 'trusted', 'key_holder'); |
||
65 | |||
66 | protected $presenter = 'BB\Presenters\UserPresenter'; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Fillable fields |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $fillable = [ |
||
75 | 'given_name', 'family_name', 'email', 'secondary_email', 'password', 'emergency_contact', 'phone', |
||
76 | 'monthly_subscription', 'profile_private', 'hash', 'rules_agreed', |
||
77 | 'key_holder', 'key_deposit_payment_id', 'trusted', 'induction_completed', 'payment_method', 'active', 'status' |
||
78 | ]; |
||
79 | |||
80 | |||
81 | protected $attributes = [ |
||
82 | 'status' => 'setting-up', |
||
83 | 'active' => 0, |
||
84 | 'key_holder' => 0, |
||
85 | 'trusted' => 0, |
||
86 | 'email_verified' => 0, |
||
87 | 'founder' => 0, |
||
88 | 'induction_completed' => 0, |
||
89 | 'payment_day' => 0, |
||
90 | 'profile_private' => 0, |
||
91 | 'cash_balance' => 0, |
||
92 | ]; |
||
93 | |||
94 | |||
95 | public function getDates() |
||
99 | |||
100 | |||
101 | public static function statuses() |
||
114 | |||
115 | |||
116 | |||
117 | /* |
||
118 | |-------------------------------------------------------------------------- |
||
119 | | Relationships |
||
120 | |-------------------------------------------------------------------------- |
||
121 | | |
||
122 | | The connections between this model and others |
||
123 | | |
||
124 | */ |
||
125 | |||
126 | public function payments() |
||
130 | |||
131 | public function inductions() |
||
135 | |||
136 | public function keyFob() |
||
140 | |||
141 | public function keyFobs() |
||
145 | |||
146 | public function profile() |
||
150 | |||
151 | public function address() |
||
155 | |||
156 | public function notifications() |
||
160 | |||
161 | |||
162 | |||
163 | /* |
||
164 | |-------------------------------------------------------------------------- |
||
165 | | Attribute Getters and Setters and Model Extensions |
||
166 | |-------------------------------------------------------------------------- |
||
167 | | |
||
168 | | Useful properties and methods to have on a user model |
||
169 | | |
||
170 | */ |
||
171 | |||
172 | public function getNameAttribute() |
||
176 | |||
177 | public function setPasswordAttribute($password) |
||
181 | |||
182 | public function setPaymentDayAttribute($value) |
||
190 | |||
191 | /** |
||
192 | * Can the user see protected member photos? |
||
193 | * Only available to active members |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function shouldMemberSeeProtectedPhoto() |
||
208 | |||
209 | /** |
||
210 | * Is the user on a payment method that allows their subscription amount to be changed |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function canMemberChangeSubAmount() |
||
218 | |||
219 | /** |
||
220 | * Is this user considered a keyholder - can they use the space on their own |
||
221 | * This may have been replaced by a repo method which checks the photo as well |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function keyholderStatus() |
||
229 | |||
230 | /** |
||
231 | * Is the user part of the admin group |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function isAdmin() |
||
239 | |||
240 | /** |
||
241 | * Should GoCardless be promoted to the user |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | public function promoteGoCardless() |
||
249 | |||
250 | /** |
||
251 | * Should we be promoting the new variable gocardless to users? |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function promoteVariableGoCardless() |
||
259 | |||
260 | /** |
||
261 | * Is the user eligible for a key? |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | public function promoteGetAKey() |
||
269 | |||
270 | /** |
||
271 | * Get an array of alerts for the user |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | public function getAlerts() |
||
286 | |||
287 | /** |
||
288 | * @return bool |
||
289 | */ |
||
290 | public function isBanned() |
||
294 | |||
295 | /** |
||
296 | * @return bool |
||
297 | */ |
||
298 | public function isSuspended() |
||
302 | |||
303 | /** |
||
304 | * @return bool |
||
305 | */ |
||
306 | public function isInducted() |
||
310 | |||
311 | public function inductedBy() |
||
315 | |||
316 | |||
317 | /* |
||
318 | |-------------------------------------------------------------------------- |
||
319 | | Scopes |
||
320 | |-------------------------------------------------------------------------- |
||
321 | */ |
||
322 | |||
323 | public function scopeActive($query) |
||
327 | |||
328 | public function scopeNotSpecialCase($query) |
||
332 | |||
333 | public function scopeLeaving($query) |
||
337 | |||
338 | public function scopePaymentWarning($query) |
||
342 | |||
343 | public function scopeSuspended($query) |
||
347 | |||
348 | |||
349 | /* |
||
350 | |-------------------------------------------------------------------------- |
||
351 | | Methods |
||
352 | |-------------------------------------------------------------------------- |
||
353 | */ |
||
354 | |||
355 | /** |
||
356 | * @param $paymentMethod |
||
357 | * @param $paymentDay |
||
358 | * @deprecated |
||
359 | */ |
||
360 | public function updateSubscription($paymentMethod, $paymentDay) |
||
371 | |||
372 | /** |
||
373 | * @param $amount |
||
374 | * @deprecated |
||
375 | */ |
||
376 | public function updateSubAmount($amount) |
||
381 | |||
382 | /** |
||
383 | * @deprecated |
||
384 | */ |
||
385 | public function cancelSubscription() |
||
393 | |||
394 | /** |
||
395 | * @deprecated |
||
396 | */ |
||
397 | public function setLeaving() |
||
402 | |||
403 | /** |
||
404 | * @deprecated |
||
405 | */ |
||
406 | public function setSuspended() |
||
412 | |||
413 | /** |
||
414 | * @deprecated |
||
415 | */ |
||
416 | public function rejoin() |
||
421 | |||
422 | public function emailConfirmed() |
||
427 | |||
428 | /** |
||
429 | * Fetch a user record, performs a permission check |
||
430 | * |
||
431 | * @param integer|null $id |
||
432 | * @param string $role |
||
433 | * |
||
434 | * @return User |
||
435 | * @throws AuthenticationException |
||
436 | */ |
||
437 | public static function findWithPermission($id = null, $role = 'admin') |
||
458 | |||
459 | public function extendMembership($paymentMethod = null, DateTime $expiry = null) |
||
472 | |||
473 | /** |
||
474 | * @return array |
||
475 | */ |
||
476 | public function getAuditFields() |
||
480 | |||
481 | |||
482 | } |
||
483 |