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 |
||
33 | class User extends \yii\db\ActiveRecord implements RateLimitInterface, IdentityInterface |
||
34 | { |
||
35 | const STATUS_DELETED = 0; |
||
36 | const STATUS_ACTIVE = 1; |
||
37 | const STATUS_BLOCKED = 2; |
||
38 | |||
39 | const ROLE_SUPERUSER = 'SuperUser'; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | public $passwordNew; |
||
45 | |||
46 | /** |
||
47 | * @inheritdoc |
||
48 | */ |
||
49 | 111 | public static function tableName() |
|
53 | |||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | 43 | public function attributeLabels() |
|
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | public function behaviors() |
||
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | */ |
||
92 | public function events() |
||
98 | |||
99 | public function transactions() |
||
107 | |||
108 | public function getRateLimit($request, $action) |
||
112 | |||
113 | 9 | public function loadAllowance($request, $action) |
|
117 | |||
118 | public function saveAllowance($request, $action, $allowance, $timestamp) |
||
124 | |||
125 | /** |
||
126 | * @return \yii\db\ActiveQuery |
||
127 | */ |
||
128 | public function getProfile() |
||
132 | |||
133 | /** |
||
134 | * @param array $attributes |
||
135 | */ |
||
136 | public function setProfile($attributes = []) |
||
140 | |||
141 | /** |
||
142 | * @return \yii\db\ActiveQuery |
||
143 | */ |
||
144 | public function getProviders() |
||
148 | |||
149 | /** |
||
150 | * @param array $attributes |
||
151 | */ |
||
152 | public function setProviders($attributes = []) |
||
156 | 80 | ||
157 | /** |
||
158 | * @return \yii\db\ActiveQuery |
||
159 | */ |
||
160 | public function getRole() |
||
164 | 9 | ||
165 | 9 | /** |
|
166 | 3 | * @inheritdoc |
|
167 | 3 | * @return \query\UserQuery the active query used by this AR class. |
|
168 | 3 | */ |
|
169 | public static function find() |
||
173 | |||
174 | /** |
||
175 | * @inheritdoc |
||
176 | 9 | */ |
|
177 | 1 | public function beforeSave($insert) |
|
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | * @property \entity\UserProfile $profile |
||
204 | * @property \entity\UserProvider $providers |
||
205 | */ |
||
206 | 9 | public function afterSave($insert, $changedAttributes) |
|
222 | |||
223 | /** |
||
224 | * @inheritdoc |
||
225 | * @param boolean $runValidation |
||
226 | 6 | * @param array $attributeNames |
|
227 | * @return boolean |
||
228 | */ |
||
229 | 6 | public function save($runValidation = true, $attributeNames = null) |
|
235 | |||
236 | /** |
||
237 | * Get all statuses |
||
238 | * |
||
239 | * @param string[] |
||
240 | 6 | */ |
|
241 | public static function getStatuses(): array |
||
249 | |||
250 | /** |
||
251 | * Get statuse name |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getStatusName(): string |
||
260 | |||
261 | 6 | /** |
|
262 | * Is it deleted? |
||
263 | 6 | * |
|
264 | * @param bool |
||
265 | */ |
||
266 | public function isDeleted(): bool |
||
270 | |||
271 | 30 | /** |
|
272 | * Is it blocked? |
||
273 | 30 | * |
|
274 | * @param bool |
||
275 | */ |
||
276 | public function isBlocked(): bool |
||
280 | |||
281 | 10 | /** |
|
282 | * Is it active? |
||
283 | 10 | * |
|
284 | * @param bool |
||
285 | */ |
||
286 | public function isActive(): bool |
||
290 | |||
291 | 15 | /** |
|
292 | * Is it confirmed? |
||
293 | 15 | * |
|
294 | * @param bool |
||
295 | */ |
||
296 | public function isConfirmed(): bool |
||
300 | |||
301 | 2 | /** |
|
302 | 2 | * Is SuperUser? |
|
303 | 2 | * |
|
304 | * @return bool |
||
305 | */ |
||
306 | public function isSuperUser(): bool |
||
310 | 8 | ||
311 | /** |
||
312 | 8 | * Set confirmed |
|
313 | 4 | */ |
|
314 | 4 | public function setConfirmed(): void |
|
319 | |||
320 | /** |
||
321 | * Get status description |
||
322 | * |
||
323 | 28 | * @return string |
|
324 | */ |
||
325 | 28 | public function getStatusDescription(): string |
|
334 | |||
335 | /** |
||
336 | * @inheritdoc |
||
337 | */ |
||
338 | public function getId() |
||
342 | |||
343 | /** |
||
344 | * @inheritdoc |
||
345 | */ |
||
346 | public function getAuthKey() |
||
350 | 3 | ||
351 | /** |
||
352 | * @inheritdoc |
||
353 | */ |
||
354 | public function validateAuthKey($authKey) |
||
358 | 22 | ||
359 | /** |
||
360 | 22 | * Generates "remember me" authentication key |
|
361 | */ |
||
362 | public function generateAuthKey() |
||
366 | |||
367 | /** |
||
368 | * Validates password |
||
369 | * |
||
370 | * @param string $password password to validate |
||
371 | * @return boolean if password provided is valid for current user |
||
372 | 6 | */ |
|
373 | public function validatePassword(string $password): bool |
||
381 | |||
382 | 1 | /** |
|
383 | * Set a new password |
||
384 | 1 | * |
|
385 | 1 | * @param string $password |
|
386 | */ |
||
387 | public function setPassword(string $password): void |
||
391 | |||
392 | 2 | /** |
|
393 | 2 | * Set new password reset token |
|
394 | * |
||
395 | * @param string $token |
||
396 | */ |
||
397 | public function setPasswordResetToken(string $token): void |
||
401 | |||
402 | 4 | /** |
|
403 | 4 | * Removes password reset token |
|
404 | 4 | */ |
|
405 | public function removePasswordResetToken(): void |
||
409 | 20 | ||
410 | /** |
||
411 | 20 | * Set new confirm email token |
|
412 | * |
||
413 | * @param string $token |
||
414 | */ |
||
415 | public function setEmailConfirmToken(string $token): void |
||
420 | |||
421 | public function getName() |
||
425 | 12 | ||
426 | /** |
||
427 | 12 | * @inheritdoc |
|
428 | 12 | */ |
|
429 | 12 | public static function findIdentity($id) |
|
433 | |||
434 | /** |
||
435 | * @inheritdoc |
||
436 | */ |
||
437 | public static function findIdentityByAccessToken($token, $type = null) |
||
441 | |||
442 | /** |
||
443 | * Update date login |
||
444 | */ |
||
445 | public function updateDateLogin(): void |
||
452 | |||
453 | /** |
||
454 | * @return bool |
||
455 | */ |
||
456 | public function beforeDelete() |
||
466 | } |
||
467 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.