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 | 306 | 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 | 292 | public function getAuthKeyRules() |
|
123 | |||
124 | /** |
||
125 | * Set the rules associated with auth key attribute. |
||
126 | * @param array $rules |
||
127 | */ |
||
128 | 2 | public function setAuthKeyRules($rules) |
|
134 | |||
135 | /** |
||
136 | * Initialize the auth key attribute. |
||
137 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
138 | * override or modify it directly, unless you know the consequences. |
||
139 | * @param ModelEvent $event |
||
140 | */ |
||
141 | 306 | public function onInitAuthKey($event) |
|
142 | { |
||
143 | 306 | $sender = $event->sender; |
|
144 | /* @var $sender static */ |
||
145 | 306 | $sender->setAuthKey(sha1(Yii::$app->security->generateRandomString())); |
|
146 | 306 | } |
|
147 | |||
148 | /** |
||
149 | * Get access token. |
||
150 | * @return string|null |
||
151 | */ |
||
152 | 8 | public function getAccessToken() |
|
157 | |||
158 | /** |
||
159 | * Set access token. |
||
160 | * @param string $token |
||
161 | * @return string|null |
||
162 | */ |
||
163 | 306 | public function setAccessToken($token) |
|
168 | |||
169 | /** |
||
170 | * Get the rules associated with access token attribute. |
||
171 | * @return array |
||
172 | */ |
||
173 | 292 | public function getAccessTokenRules() |
|
186 | |||
187 | /** |
||
188 | * Set the rules associated with access token attribute. |
||
189 | * @param array $rules |
||
190 | */ |
||
191 | 2 | public function setAccessTokenRules($rules) |
|
197 | |||
198 | /** |
||
199 | * Initialize the access token attribute. |
||
200 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
201 | * override or modify it directly, unless you know the consequences. |
||
202 | * @param ModelEvent $event |
||
203 | */ |
||
204 | 306 | public function onInitAccessToken($event) |
|
205 | { |
||
206 | 306 | $sender = $event->sender; |
|
207 | /* @var $sender static */ |
||
208 | 306 | $sender->setAccessToken(sha1(Yii::$app->security->generateRandomString())); |
|
209 | 306 | } |
|
210 | |||
211 | /** |
||
212 | * Get status. |
||
213 | * @return integer |
||
214 | */ |
||
215 | 306 | public function getStatus() |
|
220 | |||
221 | /** |
||
222 | * Set status. |
||
223 | * @param integer $status |
||
224 | * @return integer|null |
||
225 | */ |
||
226 | 306 | public function setStatus($status) |
|
231 | |||
232 | /** |
||
233 | * Get the rules associated with status attribute. |
||
234 | * @return array |
||
235 | */ |
||
236 | 292 | public function getStatusRules() |
|
249 | |||
250 | /** |
||
251 | * Set the rules associated with status attribute. |
||
252 | * @param array $rules |
||
253 | */ |
||
254 | 1 | public function setStatusRules($rules) |
|
260 | |||
261 | /** |
||
262 | * Initialize the status attribute. |
||
263 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
264 | * override or modify it directly, unless you know the consequences. |
||
265 | * @param ModelEvent $event |
||
266 | */ |
||
267 | 306 | public function onInitStatusAttribute($event) |
|
275 | } |
||
276 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.