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 |
||
31 | abstract class User extends ActiveRecord implements IdentityInterface, RateLimitInterface |
||
32 | { |
||
33 | /** |
||
34 | * password_hash Algorithm |
||
35 | * @var integer |
||
36 | */ |
||
37 | private $passwordHashAlgorithm = PASSWORD_BCRYPT; |
||
38 | |||
39 | /** |
||
40 | * The rate limit |
||
41 | * @var integer |
||
42 | */ |
||
43 | private $rateLimit = 150; |
||
44 | |||
45 | /** |
||
46 | * The rate limit window |
||
47 | * @var integer |
||
48 | */ |
||
49 | private $rateLimitWindow = 900; |
||
50 | |||
51 | /** |
||
52 | * password_hash options |
||
53 | * @var array |
||
54 | */ |
||
55 | private $passwordHashOptions = [ |
||
56 | 'cost' => 13, |
||
57 | 'memory_cost' => 1<<12, |
||
58 | 'time_cost' => 3, |
||
59 | 'threads' => 1 |
||
60 | ]; |
||
61 | |||
62 | /** |
||
63 | * Overrides init |
||
64 | */ |
||
65 | public function init() |
||
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | */ |
||
84 | public function behaviors() |
||
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | public function getRateLimit($request, $action) |
||
101 | |||
102 | /** |
||
103 | * @inheritdoc |
||
104 | */ |
||
105 | public function loadAllowance($request, $action) |
||
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | public function saveAllowance($request, $action, $allowance, $timestamp) |
||
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | public static function tableName() |
||
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | public function rules() |
||
158 | |||
159 | /** |
||
160 | * @inheritdoc |
||
161 | */ |
||
162 | public function attributeLabels() |
||
177 | |||
178 | public function beforeValidate() |
||
187 | |||
188 | /** |
||
189 | * Before save occurs |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function beforeSave($insert) |
||
204 | |||
205 | /** |
||
206 | * Validates the user's password |
||
207 | * @param string $password |
||
208 | * return bool |
||
209 | */ |
||
210 | public function validatePassword($password) |
||
227 | |||
228 | /** |
||
229 | * Returns true of OTP is enabled |
||
230 | * @return boolean |
||
231 | */ |
||
232 | public function isOTPEnabled() |
||
236 | |||
237 | /** |
||
238 | * Provisions TOTP for the account |
||
239 | * @return boolean|string |
||
240 | */ |
||
241 | public function provisionOTP() |
||
265 | |||
266 | /** |
||
267 | * Enables OTP |
||
268 | * @return boolean |
||
269 | */ |
||
270 | public function enableOTP() |
||
284 | |||
285 | /** |
||
286 | * Disables OTP |
||
287 | * @return boolean |
||
288 | */ |
||
289 | public function disableOTP() |
||
296 | |||
297 | /** |
||
298 | * Verifies the OTP code |
||
299 | * @param integer $code |
||
300 | * @return boolean |
||
301 | */ |
||
302 | public function verifyOTP($code) |
||
314 | |||
315 | /** |
||
316 | * Activates the user |
||
317 | * @return boolean |
||
318 | */ |
||
319 | public function activate() |
||
324 | |||
325 | /** |
||
326 | * Whether or not a user is activated or not |
||
327 | * @return boolean |
||
328 | */ |
||
329 | public function isActivated() |
||
333 | |||
334 | /** |
||
335 | * @inheritdoc |
||
336 | */ |
||
337 | public static function findIdentity($id) |
||
341 | |||
342 | /** |
||
343 | * @inheritdoc |
||
344 | */ |
||
345 | public static function findIdentityByAccessToken($token, $type = null) |
||
354 | |||
355 | /** |
||
356 | * @inheritdoc |
||
357 | */ |
||
358 | public function getAuthKey() {} |
||
359 | |||
360 | /** |
||
361 | * @inheritdoc |
||
362 | */ |
||
363 | public function validateAuthKey($authKey) |
||
367 | |||
368 | /** |
||
369 | * @inheritdoc |
||
370 | */ |
||
371 | public function getId() |
||
375 | |||
376 | /** |
||
377 | * @todo |
||
378 | */ |
||
379 | public static function sendActivationEmail($email, $token) |
||
383 | |||
384 | /** |
||
385 | * @todo |
||
386 | */ |
||
387 | public static function sendPasswordResetEmail($email, $token) |
||
391 | |||
392 | /** |
||
393 | * @todo |
||
394 | */ |
||
395 | public static function sendPasswordChangedEmail($email) |
||
399 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: