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 |
||
29 | abstract class User extends ActiveRecord implements IdentityInterface, RateLimitInterface |
||
30 | { |
||
31 | /** |
||
32 | * password_hash Algorithm |
||
33 | * @var integer |
||
34 | */ |
||
35 | private $passwordHashAlgorithm = PASSWORD_BCRYPT; |
||
36 | |||
37 | /** |
||
38 | * The rate limit |
||
39 | * @var integer |
||
40 | */ |
||
41 | private $rateLimit = 150; |
||
42 | |||
43 | /** |
||
44 | * The rate limit window |
||
45 | * @var integer |
||
46 | */ |
||
47 | private $rateLimitWindow = 900; |
||
48 | |||
49 | /** |
||
50 | * password_hash options |
||
51 | * @var array |
||
52 | */ |
||
53 | private $passwordHashOptions = [ |
||
54 | 'cost' => 13, |
||
55 | 'memory_cost' => 1<<12, |
||
56 | 'time_cost' => 3, |
||
57 | 'threads' => 1 |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * The token used to authenticate the user |
||
62 | * @var app\models\Token |
||
63 | */ |
||
64 | protected $token; |
||
65 | |||
66 | /** |
||
67 | * Sets the token used to authenticate the user |
||
68 | * @return app\models\Token |
||
69 | */ |
||
70 | public function getToken() |
||
74 | |||
75 | /** |
||
76 | * Sets the token that was used to authenticate the user |
||
77 | */ |
||
78 | public function setToken($token) |
||
86 | |||
87 | /** |
||
88 | * Overrides init |
||
89 | */ |
||
90 | public function init() |
||
105 | |||
106 | /** |
||
107 | * @inheritdoc |
||
108 | */ |
||
109 | public function behaviors() |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | public function getRateLimit($request, $action) |
||
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | public function loadAllowance($request, $action) |
||
144 | |||
145 | /** |
||
146 | * @inheritdoc |
||
147 | */ |
||
148 | public function saveAllowance($request, $action, $allowance, $timestamp) |
||
158 | |||
159 | /** |
||
160 | * @inheritdoc |
||
161 | */ |
||
162 | public static function tableName() |
||
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | public function rules() |
||
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | public function attributeLabels() |
||
202 | |||
203 | public function beforeValidate() |
||
212 | |||
213 | /** |
||
214 | * Before save occurs |
||
215 | * @return bool |
||
216 | */ |
||
217 | public function beforeSave($insert) |
||
229 | |||
230 | /** |
||
231 | * Validates the user's password |
||
232 | * @param string $password |
||
233 | * return bool |
||
234 | */ |
||
235 | public function validatePassword($password) |
||
252 | |||
253 | /** |
||
254 | * Returns true of OTP is enabled |
||
255 | * @return boolean |
||
256 | */ |
||
257 | public function isOTPEnabled() |
||
261 | |||
262 | /** |
||
263 | * Provisions TOTP for the account |
||
264 | * @return boolean|string |
||
265 | */ |
||
266 | public function provisionOTP() |
||
290 | |||
291 | /** |
||
292 | * Enables OTP |
||
293 | * @return boolean |
||
294 | */ |
||
295 | public function enableOTP() |
||
309 | |||
310 | /** |
||
311 | * Disables OTP |
||
312 | * @return boolean |
||
313 | */ |
||
314 | public function disableOTP() |
||
321 | |||
322 | /** |
||
323 | * Verifies the OTP code |
||
324 | * @param integer $code |
||
325 | * @return boolean |
||
326 | */ |
||
327 | public function verifyOTP($code) |
||
340 | |||
341 | /** |
||
342 | * Activates the user |
||
343 | * @return boolean |
||
344 | */ |
||
345 | public function activate() |
||
350 | |||
351 | /** |
||
352 | * Whether or not a user is activated or not |
||
353 | * @return boolean |
||
354 | */ |
||
355 | public function isActivated() |
||
359 | |||
360 | /** |
||
361 | * @inheritdoc |
||
362 | */ |
||
363 | public static function findIdentity($id) |
||
367 | |||
368 | /** |
||
369 | * @inheritdoc |
||
370 | */ |
||
371 | public static function findIdentityByAccessToken($token, $type = null) |
||
380 | |||
381 | /** |
||
382 | * @inheritdoc |
||
383 | */ |
||
384 | public function getAuthKey() |
||
388 | |||
389 | /** |
||
390 | * @inheritdoc |
||
391 | */ |
||
392 | public function validateAuthKey($authKey) |
||
396 | |||
397 | /** |
||
398 | * @inheritdoc |
||
399 | */ |
||
400 | public function getId() |
||
404 | } |
||
405 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.