Complex classes like IdentityTrait 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 IdentityTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | trait IdentityTrait |
||
| 31 | { |
||
| 32 | |||
| 33 | public static $statusActive = 1; |
||
| 34 | public static $statusInactive = 0; |
||
| 35 | public $statusAttribute = 'status'; |
||
| 36 | private $statusRules = []; |
||
| 37 | public $authKeyAttribute = 'auth_key'; |
||
| 38 | private $authKeyRules = []; |
||
| 39 | public $accessTokenAttribute = 'access_token'; |
||
| 40 | private $accessTokenRules = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Finds an identity by the given ID. |
||
| 44 | * @param string|integer $identity |
||
| 45 | * @return static |
||
| 46 | */ |
||
| 47 | 3 | public static function findIdentity($identity) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Finds an identity by the given GUID. |
||
| 55 | * @param string $guid |
||
| 56 | * @return static |
||
| 57 | */ |
||
| 58 | 3 | public static function findIdentityByGuid($guid) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Finds an identity by the given token. |
||
| 65 | * @param string $token |
||
| 66 | * @param mixed $type |
||
| 67 | * @return static |
||
| 68 | */ |
||
| 69 | 3 | public static function findIdentityByAccessToken($token, $type = null) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get auth key. |
||
| 77 | * @return string|null |
||
| 78 | */ |
||
| 79 | 5 | public function getAuthKey() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Set auth key. |
||
| 87 | * @param string $key |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 5 | public function setAuthKey($key) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Validate the auth key. |
||
| 98 | * @param string $authKey |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | 3 | public function validateAuthKey($authKey) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Get the rules associated with auth key attribute. |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | 288 | public function getAuthKeyRules() |
|
| 111 | { |
||
| 112 | 288 | if (empty($this->authKeyRules)) { |
|
| 113 | 286 | $this->authKeyRules = [ |
|
| 114 | 286 | [[$this->authKeyAttribute], 'required'], |
|
| 115 | 286 | [[$this->authKeyAttribute], 'string', 'max' => 40], |
|
| 116 | ]; |
||
| 117 | } |
||
| 118 | 288 | return $this->authKeyRules; |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set the rules associated with auth key attribute. |
||
| 123 | * @param array $rules |
||
| 124 | */ |
||
| 125 | 2 | public function setAuthKeyRules($rules) |
|
| 126 | { |
||
| 127 | 2 | if (!empty($rules) && is_array($rules)) { |
|
| 128 | 2 | $this->authKeyRules = $rules; |
|
| 129 | } |
||
| 130 | 2 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Initialize the auth key attribute. |
||
| 134 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 135 | * override or modify it directly, unless you know the consequences. |
||
| 136 | * @param ModelEvent $event |
||
| 137 | */ |
||
| 138 | 300 | public function onInitAuthKey($event) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get access token. |
||
| 147 | * @return string|null |
||
| 148 | */ |
||
| 149 | 8 | public function getAccessToken() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Set access token. |
||
| 157 | * @param string $token |
||
| 158 | * @return string|null |
||
| 159 | */ |
||
| 160 | 5 | public function setAccessToken($token) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Get the rules associated with access token attribute. |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 288 | public function getAccessTokenRules() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Set the rules associated with access token attribute. |
||
| 186 | * @param array $rules |
||
| 187 | */ |
||
| 188 | 2 | public function setAccessTokenRules($rules) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Initialize the access token attribute. |
||
| 197 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 198 | * override or modify it directly, unless you know the consequences. |
||
| 199 | * @param ModelEvent $event |
||
| 200 | */ |
||
| 201 | 300 | public function onInitAccessToken($event) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Get status. |
||
| 210 | * @return integer |
||
| 211 | */ |
||
| 212 | 3 | public function getStatus() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Set status. |
||
| 220 | * @param integer $status |
||
| 221 | * @return integer|null |
||
| 222 | */ |
||
| 223 | 1 | public function setStatus($status) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Get the rules associated with status attribute. |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | 288 | public function getStatusRules() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Set the rules associated with status attribute. |
||
| 249 | * @param array $rules |
||
| 250 | */ |
||
| 251 | 1 | public function setStatusRules($rules) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Initialize the status attribute. |
||
| 260 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 261 | * override or modify it directly, unless you know the consequences. |
||
| 262 | * @param ModelEvent $event |
||
| 263 | */ |
||
| 264 | 300 | public function onInitStatusAttribute($event) |
|
| 272 | } |
||
| 273 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.