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 | /** |
||
179 | * Before save occurs |
||
180 | * @return bool |
||
181 | */ |
||
182 | public function beforeSave($insert) |
||
183 | { |
||
184 | if (parent::beforeSave($insert)) { |
||
185 | if ($this->isNewRecord || $this->password !== $this->oldAttributes['password']) { |
||
186 | $this->password = password_hash($this->password, $this->passwordHashAlgorithm, $this->passwordHashOptions); |
||
187 | } |
||
188 | |||
189 | return true; |
||
190 | } |
||
191 | |||
192 | return false; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Validates the user's password |
||
197 | * @param string $password |
||
198 | * return bool |
||
199 | */ |
||
200 | public function validatePassword($password) |
||
217 | |||
218 | /** |
||
219 | * Returns true of OTP is enabled |
||
220 | * @return boolean |
||
221 | */ |
||
222 | public function isOTPEnabled() |
||
226 | |||
227 | /** |
||
228 | * Provisions TOTP for the account |
||
229 | * @return boolean|string |
||
230 | */ |
||
231 | public function provisionOTP() |
||
255 | |||
256 | /** |
||
257 | * Enables OTP |
||
258 | * @return boolean |
||
259 | */ |
||
260 | public function enableOTP() |
||
274 | |||
275 | /** |
||
276 | * Disables OTP |
||
277 | * @return boolean |
||
278 | */ |
||
279 | public function disableOTP() |
||
286 | |||
287 | /** |
||
288 | * Verifies the OTP code |
||
289 | * @param integer $code |
||
290 | * @return boolean |
||
291 | */ |
||
292 | public function verifyOTP($code) |
||
304 | |||
305 | /** |
||
306 | * Activates the user |
||
307 | * @return boolean |
||
308 | */ |
||
309 | public function activate() |
||
314 | |||
315 | /** |
||
316 | * Whether or not a user is activated or not |
||
317 | * @return boolean |
||
318 | */ |
||
319 | public function isActivated() |
||
323 | |||
324 | /** |
||
325 | * @inheritdoc |
||
326 | */ |
||
327 | public static function findIdentity($id) |
||
331 | |||
332 | /** |
||
333 | * @inheritdoc |
||
334 | */ |
||
335 | public static function findIdentityByAccessToken($token, $type = null) |
||
344 | |||
345 | /** |
||
346 | * @inheritdoc |
||
347 | */ |
||
348 | public function getAuthKey() {} |
||
349 | |||
350 | /** |
||
351 | * @inheritdoc |
||
352 | */ |
||
353 | public function validateAuthKey($authKey) |
||
357 | |||
358 | /** |
||
359 | * @inheritdoc |
||
360 | */ |
||
361 | public function getId() |
||
365 | |||
366 | /** |
||
367 | * @todo |
||
368 | */ |
||
369 | public static function sendActivationEmail($email, $token) |
||
373 | |||
374 | /** |
||
375 | * @todo |
||
376 | */ |
||
377 | public static function sendPasswordResetEmail($email, $token) |
||
381 | |||
382 | /** |
||
383 | * @todo |
||
384 | */ |
||
385 | public static function sendPasswordChangedEmail($email) |
||
389 | } |
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: