Complex classes like Users 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 Users, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Users extends \Baka\Auth\Models\Users |
||
39 | { |
||
40 | use PermissionsTrait; |
||
41 | use Billable; |
||
42 | use SubscriptionPlanLimitTrait; |
||
43 | use FileSystemModelTrait; |
||
44 | use HashTableTrait; |
||
45 | use NotifiableTrait; |
||
46 | use EventManagerAwareTrait; |
||
47 | |||
48 | /** |
||
49 | * Default Company Branch. |
||
50 | * |
||
51 | * @var integer |
||
52 | */ |
||
53 | public $default_company_branch; |
||
|
|||
54 | |||
55 | /** |
||
56 | * Roles id. |
||
57 | * |
||
58 | * @var integer |
||
59 | */ |
||
60 | public $roles_id; |
||
61 | |||
62 | /** |
||
63 | * Stripe id. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | public $stripe_id; |
||
68 | |||
69 | /** |
||
70 | * Card last four numbers. |
||
71 | * |
||
72 | * @var integer |
||
73 | */ |
||
74 | public $card_last_four; |
||
75 | |||
76 | /** |
||
77 | * Card Brand. |
||
78 | * |
||
79 | * @var integer |
||
80 | */ |
||
81 | public $card_brand; |
||
82 | |||
83 | /** |
||
84 | * Trial end date. |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | public $trial_ends_at; |
||
89 | |||
90 | /** |
||
91 | * Provide the app plan id |
||
92 | * if the user is signing up a new company. |
||
93 | * |
||
94 | * @var integer |
||
95 | */ |
||
96 | public $appPlanId = null; |
||
97 | |||
98 | /** |
||
99 | * Active subscription id.Not an actual table field, used temporarily. |
||
100 | * @var string |
||
101 | */ |
||
102 | public $active_subscription_id; |
||
103 | |||
104 | /** |
||
105 | * System Module Id. |
||
106 | * @var integer |
||
107 | */ |
||
108 | public $system_modules_id = 2; |
||
109 | |||
110 | /** |
||
111 | * User email activation code. |
||
112 | * |
||
113 | * @var string |
||
114 | */ |
||
115 | public $user_activation_email; |
||
116 | |||
117 | /** |
||
118 | * Initialize method for model. |
||
119 | */ |
||
120 | public function initialize() |
||
272 | |||
273 | /** |
||
274 | * Validations and business logic. |
||
275 | */ |
||
276 | public function validation() |
||
306 | |||
307 | /** |
||
308 | * Returns table name mapped in the model. |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | public function getSource() : string |
||
316 | |||
317 | /** |
||
318 | * Set hashtable settings table, userConfig ;). |
||
319 | * |
||
320 | * @return void |
||
321 | */ |
||
322 | private function createSettingsModel(): void |
||
326 | |||
327 | /** |
||
328 | * Get the User key for redis. |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getKey() : int |
||
336 | |||
337 | /** |
||
338 | * A company owner is the first person that register this company |
||
339 | * This only ocurres when signing up the first time, after that all users invites |
||
340 | * come with a default_company id attached. |
||
341 | * |
||
342 | * @return boolean |
||
343 | */ |
||
344 | public function isFirstSignup(): bool |
||
348 | |||
349 | /** |
||
350 | * Does the user have a role assign to him? |
||
351 | * |
||
352 | * @return boolean |
||
353 | */ |
||
354 | public function hasRole(): bool |
||
358 | |||
359 | /** |
||
360 | * Get all of the subscriptions for the user. |
||
361 | */ |
||
362 | public function subscriptions() |
||
380 | |||
381 | /** |
||
382 | * Strat a free trial. |
||
383 | * |
||
384 | * @param Users $user |
||
385 | * @return Subscription |
||
386 | */ |
||
387 | public function startFreeTrial() : Subscription |
||
412 | |||
413 | /** |
||
414 | * Before create. |
||
415 | * |
||
416 | * @return void |
||
417 | */ |
||
418 | public function beforeCreate() |
||
436 | |||
437 | /** |
||
438 | * What the current company the users is logged in with |
||
439 | * in this current session? |
||
440 | * |
||
441 | * @return integer |
||
442 | */ |
||
443 | public function currentCompanyId(): int |
||
448 | |||
449 | /** |
||
450 | * Overwrite the user relationship. |
||
451 | * use Phalcon Registry to assure we mantian the same instance accross the request. |
||
452 | */ |
||
453 | public function getDefaultCompany(): Companies |
||
462 | |||
463 | /** |
||
464 | * What the current company brach the users is logged in with |
||
465 | * in this current session? |
||
466 | * |
||
467 | * @return integer |
||
468 | */ |
||
469 | public function currentCompanyBranchId(): int |
||
473 | |||
474 | /** |
||
475 | * What to do after the creation of a new users |
||
476 | * - Assign default role. |
||
477 | * |
||
478 | * @return void |
||
479 | */ |
||
480 | public function afterCreate() |
||
500 | |||
501 | /** |
||
502 | * Upload Files. |
||
503 | * |
||
504 | * @todo move this to the baka class |
||
505 | * |
||
506 | * @return void |
||
507 | */ |
||
508 | public function afterSave() |
||
513 | |||
514 | /** |
||
515 | * update user role for the specific app. |
||
516 | * |
||
517 | * @return void |
||
518 | */ |
||
519 | protected function updatePermissionRoles(): bool |
||
528 | |||
529 | /** |
||
530 | * Overwrite the permission relationship to force the user of company id. |
||
531 | * |
||
532 | * @return UserRoles |
||
533 | */ |
||
534 | public function getPermission() |
||
538 | |||
539 | /** |
||
540 | * Get the list of all the associated apps this users has. |
||
541 | *:w. |
||
542 | * @return array |
||
543 | */ |
||
544 | public function getAssociatedApps(): array |
||
556 | |||
557 | /** |
||
558 | * Get an array of the associates companies Ids. |
||
559 | * |
||
560 | * @return array |
||
561 | */ |
||
562 | public function getAssociatedCompanies(): array |
||
574 | |||
575 | /** |
||
576 | * Get user by key. |
||
577 | * @param string $userActivationEmail |
||
578 | * @return Users |
||
579 | */ |
||
580 | public static function getByUserActivationEmail(string $userActivationEmail): Users |
||
587 | |||
588 | /** |
||
589 | * Overwrite the relationship. |
||
590 | * |
||
591 | * @return void |
||
592 | */ |
||
593 | public function getPhoto() |
||
597 | |||
598 | /** |
||
599 | * Update the user current default company. |
||
600 | * |
||
601 | * @param integer $companyId |
||
602 | * @return void |
||
603 | */ |
||
604 | public function switchDefaultCompanyByBranch(int $branchId): void |
||
617 | |||
618 | /** |
||
619 | * Update the password for a current user. |
||
620 | * |
||
621 | * @param string $newPassword |
||
622 | * @return boolean |
||
623 | */ |
||
624 | public function updatePassword(string $currentPassword, string $newPassword, string $verifyPassword) : bool |
||
654 | |||
655 | /** |
||
656 | * Reset the user passwrod. |
||
657 | * |
||
658 | * @param string $newPassword |
||
659 | * @return bool |
||
660 | */ |
||
661 | public function resetPassword(string $newPassword): bool |
||
674 | |||
675 | /** |
||
676 | * user signup to the service. |
||
677 | * |
||
678 | * did we find the email? |
||
679 | * does it have access to this app? |
||
680 | * no? |
||
681 | * ok lets register / associate to this app |
||
682 | * yes? |
||
683 | * it meas he was invites so get the fuck out? |
||
684 | * |
||
685 | * @return Users |
||
686 | */ |
||
687 | public function signUp() : BakUser |
||
725 | |||
726 | /** |
||
727 | * Generate new forgot password hash. |
||
728 | * |
||
729 | * @return string |
||
730 | */ |
||
731 | public function generateForgotHash(): string |
||
738 | |||
739 | /** |
||
740 | * Given a Role assign it to the user for the current app. |
||
741 | * |
||
742 | * @param Roles $role |
||
743 | * @return boolean |
||
744 | */ |
||
745 | public function assignRole(Roles $role): bool |
||
755 | } |
||
756 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.