Complex classes like RegistrationTrait 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 RegistrationTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | trait RegistrationTrait |
||
| 31 | { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @event Event an event that is triggered after user is registered successfully. |
||
| 35 | */ |
||
| 36 | public static $eventAfterRegister = "afterRegister"; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @event Event an event that is triggered before registration. |
||
| 40 | */ |
||
| 41 | public static $eventBeforeRegister = "beforeRegister"; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @event Event an event that is triggered when registration failed. |
||
| 45 | */ |
||
| 46 | public static $eventRegisterFailed = "registerFailed"; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @event Event an event that is triggered after user is deregistered successfully. |
||
| 50 | */ |
||
| 51 | public static $eventAfterDeregister = "afterDeregister"; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @event Event an event that is triggered before deregistration. |
||
| 55 | */ |
||
| 56 | public static $eventBeforeDeregister = "beforeDeregister"; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @event Event an event that is triggered when deregistration failed. |
||
| 60 | */ |
||
| 61 | public static $eventDeregisterFailed = "deregisterFailed"; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string name of attribute which store the source. if you don't want to |
||
| 65 | * record source, please assign false. |
||
| 66 | */ |
||
| 67 | public $sourceAttribute = 'source'; |
||
| 68 | private $_sourceRules = []; |
||
| 69 | public static $sourceSelf = '0'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string auth manager component id. |
||
| 73 | */ |
||
| 74 | public $authManagerId = 'authManager'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get auth manager. If auth manager not configured, Yii::$app->authManager |
||
| 78 | * will be given. |
||
| 79 | * @return ManagerInterface |
||
| 80 | */ |
||
| 81 | 277 | public function getAuthManager() |
|
| 82 | { |
||
| 83 | 277 | $authManagerId = $this->authManagerId; |
|
| 84 | 277 | return empty($authManagerId) ? Yii::$app->authManager : Yii::$app->$authManagerId; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Register new user. |
||
| 89 | * It is equivalent to store the current user and its associated models into |
||
| 90 | * database synchronously. The registration will be terminated immediately |
||
| 91 | * if any errors occur in the process, and all the earlier steps succeeded |
||
| 92 | * are rolled back. |
||
| 93 | * If auth manager configured, and auth role(s) provided, it(they) will be |
||
| 94 | * assigned to user after registration. |
||
| 95 | * If current user is not a new one(isNewRecord = false), the registration |
||
| 96 | * will be skipped and return false. |
||
| 97 | * The $eventBeforeRegister will be triggered before registration starts. |
||
| 98 | * If registration finished, the $eventAfterRegister will be triggered. or |
||
| 99 | * $eventRegisterFailed will be triggered when any errors occured. |
||
| 100 | * @param array $associatedModels The models associated with user to be stored synchronously. |
||
| 101 | * @param string|Item[] $authRoles auth name, auth instance, auth name array or auth instance array. |
||
| 102 | * @return boolean Whether the registration succeeds or not. |
||
| 103 | * @throws IntegrityException when inserting user and associated models failed. |
||
| 104 | */ |
||
| 105 | 278 | public function register($associatedModels = [], $authRoles = []) |
|
| 106 | { |
||
| 107 | 278 | if (!$this->getIsNewRecord()) { |
|
|
|
|||
| 108 | 3 | return false; |
|
| 109 | } |
||
| 110 | 278 | $this->trigger(static::$eventBeforeRegister); |
|
| 111 | 278 | $transaction = $this->getDb()->beginTransaction(); |
|
| 112 | try { |
||
| 113 | 278 | if (!$this->save()) { |
|
| 114 | 1 | throw new IntegrityException('Registration Error(s) Occured: User Save Failed.', $this->getErrors()); |
|
| 115 | } |
||
| 116 | 277 | if (($authManager = $this->getAuthManager()) && !empty($authRoles)) { |
|
| 117 | if (is_string($authRoles) || $authRoles instanceof Item || !is_array($authRoles)) { |
||
| 118 | $authRoles = [$authRoles]; |
||
| 119 | } |
||
| 120 | foreach ($authRoles as $role) { |
||
| 121 | if (is_string($role)) { |
||
| 122 | $role = $authManager->getRole($role); |
||
| 123 | } |
||
| 124 | if ($role instanceof Item) { |
||
| 125 | $authManager->assign($role, $this->getGUID()); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | 277 | if (!empty($associatedModels) && is_array($associatedModels)) { |
|
| 130 | 131 | foreach ($associatedModels as $model) { |
|
| 131 | 131 | if (!$model->save()) { |
|
| 132 | throw new IntegrityException |
||
| 133 | ('Registration Error(s) Occured: Associated Models Save Failed.', $model->getErrors()); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | 277 | $transaction->commit(); |
|
| 138 | 1 | } catch (\Exception $ex) { |
|
| 139 | 1 | $transaction->rollBack(); |
|
| 140 | 1 | $this->trigger(static::$eventRegisterFailed); |
|
| 141 | 1 | if (YII_DEBUG || YII_ENV !== YII_ENV_PROD) { |
|
| 142 | 1 | Yii::error($ex->getMessage(), __METHOD__); |
|
| 143 | 1 | return $ex; |
|
| 144 | } |
||
| 145 | Yii::warning($ex->getMessage(), __METHOD__); |
||
| 146 | return false; |
||
| 147 | } |
||
| 148 | 277 | $this->trigger(static::$eventAfterRegister); |
|
| 149 | 277 | return true; |
|
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Deregister current user itself. |
||
| 154 | * It is equivalent to delete current user and its associated models. BUT it |
||
| 155 | * deletes current user ONLY, the associated models will not be deleted |
||
| 156 | * forwardly. So you should set the foreign key of associated models' table |
||
| 157 | * referenced from primary key of user table, and their association mode is |
||
| 158 | * 'on update cascade' and 'on delete cascade'. |
||
| 159 | * the $eventBeforeDeregister will be triggered before deregistration starts. |
||
| 160 | * if deregistration finished, the $eventAfterDeregister will be triggered. or |
||
| 161 | * $eventDeregisterFailed will be triggered when any errors occured. |
||
| 162 | * @return boolean Whether deregistration succeeds or not. |
||
| 163 | * @throws IntegrityException when deleting user failed. |
||
| 164 | */ |
||
| 165 | 302 | public function deregister() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Get source. |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 1 | public function getSource() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Set source. |
||
| 207 | * @param string $source |
||
| 208 | */ |
||
| 209 | 302 | public function setSource($source) |
|
| 210 | { |
||
| 211 | 302 | $sourceAttribute = $this->sourceAttribute; |
|
| 212 | 302 | return (is_string($sourceAttribute) && !empty($sourceAttribute)) ? $this->$sourceAttribute = $source : null; |
|
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the rules associated with source attribute. |
||
| 217 | * @return array rules. |
||
| 218 | */ |
||
| 219 | 288 | public function getSourceRules() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Set the rules associated with source attribute. |
||
| 235 | * @param array $rules |
||
| 236 | */ |
||
| 237 | 1 | public function setSourceRules($rules) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Initialize the source attribute with $sourceSelf. |
||
| 246 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 247 | * override or modify it directly, unless you know the consequences. |
||
| 248 | * @param ModelEvent $event |
||
| 249 | */ |
||
| 250 | 302 | public function onInitSourceAttribute($event) |
|
| 256 | } |
||
| 257 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.