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 |
||
61 | class User extends Component |
||
62 | { |
||
63 | const EVENT_BEFORE_LOGIN = 'beforeLogin'; |
||
64 | const EVENT_AFTER_LOGIN = 'afterLogin'; |
||
65 | const EVENT_BEFORE_LOGOUT = 'beforeLogout'; |
||
66 | const EVENT_AFTER_LOGOUT = 'afterLogout'; |
||
67 | |||
68 | /** |
||
69 | * @var string the class name of the [[identity]] object. |
||
70 | */ |
||
71 | public $identityClass; |
||
72 | /** |
||
73 | * @var bool whether to enable cookie-based login. Defaults to `false`. |
||
74 | * Note that this property will be ignored if [[enableSession]] is `false`. |
||
75 | */ |
||
76 | public $enableAutoLogin = false; |
||
77 | /** |
||
78 | * @var bool whether to use session to persist authentication status across multiple requests. |
||
79 | * You set this property to be `false` if your application is stateless, which is often the case |
||
80 | * for RESTful APIs. |
||
81 | */ |
||
82 | public $enableSession = true; |
||
83 | /** |
||
84 | * @var string|array the URL for login when [[loginRequired()]] is called. |
||
85 | * If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL. |
||
86 | * The first element of the array should be the route to the login action, and the rest of |
||
87 | * the name-value pairs are GET parameters used to construct the login URL. For example, |
||
88 | * |
||
89 | * ```php |
||
90 | * ['site/login', 'ref' => 1] |
||
91 | * ``` |
||
92 | * |
||
93 | * If this property is `null`, a 403 HTTP exception will be raised when [[loginRequired()]] is called. |
||
94 | */ |
||
95 | public $loginUrl = ['site/login']; |
||
96 | /** |
||
97 | * @var array the configuration of the identity cookie. This property is used only when [[enableAutoLogin]] is `true`. |
||
98 | * @see Cookie |
||
99 | */ |
||
100 | public $identityCookie = ['name' => '_identity', 'httpOnly' => true]; |
||
101 | /** |
||
102 | * @var int the number of seconds in which the user will be logged out automatically if he |
||
103 | * remains inactive. If this property is not set, the user will be logged out after |
||
104 | * the current session expires (c.f. [[Session::timeout]]). |
||
105 | * Note that this will not work if [[enableAutoLogin]] is `true`. |
||
106 | */ |
||
107 | public $authTimeout; |
||
108 | /** |
||
109 | * @var CheckAccessInterface The access checker to use for checking access. |
||
110 | * If not set the application auth manager will be used. |
||
111 | * @since 2.0.9 |
||
112 | */ |
||
113 | public $accessChecker; |
||
114 | /** |
||
115 | * @var int the number of seconds in which the user will be logged out automatically |
||
116 | * regardless of activity. |
||
117 | * Note that this will not work if [[enableAutoLogin]] is `true`. |
||
118 | */ |
||
119 | public $absoluteAuthTimeout; |
||
120 | /** |
||
121 | * @var bool whether to automatically renew the identity cookie each time a page is requested. |
||
122 | * This property is effective only when [[enableAutoLogin]] is `true`. |
||
123 | * When this is `false`, the identity cookie will expire after the specified duration since the user |
||
124 | * is initially logged in. When this is `true`, the identity cookie will expire after the specified duration |
||
125 | * since the user visits the site the last time. |
||
126 | * @see enableAutoLogin |
||
127 | */ |
||
128 | public $autoRenewCookie = true; |
||
129 | /** |
||
130 | * @var string the session variable name used to store the value of [[id]]. |
||
131 | */ |
||
132 | public $idParam = '__id'; |
||
133 | /** |
||
134 | * @var string the session variable name used to store the value of expiration timestamp of the authenticated state. |
||
135 | * This is used when [[authTimeout]] is set. |
||
136 | */ |
||
137 | public $authTimeoutParam = '__expire'; |
||
138 | /** |
||
139 | * @var string the session variable name used to store the value of absolute expiration timestamp of the authenticated state. |
||
140 | * This is used when [[absoluteAuthTimeout]] is set. |
||
141 | */ |
||
142 | public $absoluteAuthTimeoutParam = '__absoluteExpire'; |
||
143 | /** |
||
144 | * @var string the session variable name used to store the value of [[returnUrl]]. |
||
145 | */ |
||
146 | public $returnUrlParam = '__returnUrl'; |
||
147 | /** |
||
148 | * @var array MIME types for which this component should redirect to the [[loginUrl]]. |
||
149 | * @since 2.0.8 |
||
150 | */ |
||
151 | public $acceptableRedirectTypes = ['text/html', 'application/xhtml+xml']; |
||
152 | |||
153 | private $_access = []; |
||
154 | |||
155 | |||
156 | /** |
||
157 | * Initializes the application component. |
||
158 | */ |
||
159 | 60 | public function init() |
|
173 | |||
174 | private $_identity = false; |
||
175 | |||
176 | /** |
||
177 | * Returns the identity object associated with the currently logged-in user. |
||
178 | * When [[enableSession]] is true, this method may attempt to read the user's authentication data |
||
179 | * stored in session and reconstruct the corresponding identity object, if it has not done so before. |
||
180 | * @param bool $autoRenew whether to automatically renew authentication status if it has not been done so before. |
||
181 | * This is only useful when [[enableSession]] is true. |
||
182 | * @return IdentityInterface|null the identity object associated with the currently logged-in user. |
||
183 | * `null` is returned if the user is not logged in (not authenticated). |
||
184 | * @see login() |
||
185 | * @see logout() |
||
186 | */ |
||
187 | 51 | public function getIdentity($autoRenew = true) |
|
208 | |||
209 | /** |
||
210 | * Sets the user identity object. |
||
211 | * |
||
212 | * Note that this method does not deal with session or cookie. You should usually use [[switchIdentity()]] |
||
213 | * to change the identity of the current user. |
||
214 | * |
||
215 | * @param IdentityInterface|null $identity the identity object associated with the currently logged user. |
||
216 | * If null, it means the current user will be a guest without any associated identity. |
||
217 | * @throws InvalidValueException if `$identity` object does not implement [[IdentityInterface]]. |
||
218 | */ |
||
219 | 49 | public function setIdentity($identity) |
|
230 | |||
231 | /** |
||
232 | * Logs in a user. |
||
233 | * |
||
234 | * After logging in a user: |
||
235 | * - the user's identity information is obtainable from the [[identity]] property |
||
236 | * |
||
237 | * If [[enableSession]] is `true`: |
||
238 | * - the identity information will be stored in session and be available in the next requests |
||
239 | * - in case of `$duration == 0`: as long as the session remains active or till the user closes the browser |
||
240 | * - in case of `$duration > 0`: as long as the session remains active or as long as the cookie |
||
241 | * remains valid by it's `$duration` in seconds when [[enableAutoLogin]] is set `true`. |
||
242 | * |
||
243 | * If [[enableSession]] is `false`: |
||
244 | * - the `$duration` parameter will be ignored |
||
245 | * |
||
246 | * @param IdentityInterface $identity the user identity (which should already be authenticated) |
||
247 | * @param int $duration number of seconds that the user can remain in logged-in status, defaults to `0` |
||
248 | * @return bool whether the user is logged in |
||
249 | */ |
||
250 | 15 | public function login(IdentityInterface $identity, $duration = 0) |
|
270 | |||
271 | /** |
||
272 | * Regenerates CSRF token |
||
273 | * |
||
274 | * @since 2.0.14.2 |
||
275 | */ |
||
276 | 15 | protected function regenerateCsrfToken() |
|
283 | |||
284 | /** |
||
285 | * Logs in a user by the given access token. |
||
286 | * This method will first authenticate the user by calling [[IdentityInterface::findIdentityByAccessToken()]] |
||
287 | * with the provided access token. If successful, it will call [[login()]] to log in the authenticated user. |
||
288 | * If authentication fails or [[login()]] is unsuccessful, it will return null. |
||
289 | * @param string $token the access token |
||
290 | * @param mixed $type the type of the token. The value of this parameter depends on the implementation. |
||
291 | * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`. |
||
292 | * @return IdentityInterface|null the identity associated with the given access token. Null is returned if |
||
293 | * the access token is invalid or [[login()]] is unsuccessful. |
||
294 | */ |
||
295 | 19 | public function loginByAccessToken($token, $type = null) |
|
306 | |||
307 | /** |
||
308 | * Logs in a user by cookie. |
||
309 | * |
||
310 | * This method attempts to log in a user using the ID and authKey information |
||
311 | * provided by the [[identityCookie|identity cookie]]. |
||
312 | */ |
||
313 | 2 | protected function loginByCookie() |
|
328 | |||
329 | /** |
||
330 | * Logs out the current user. |
||
331 | * This will remove authentication-related session data. |
||
332 | * If `$destroySession` is true, all session data will be removed. |
||
333 | * @param bool $destroySession whether to destroy the whole session. Defaults to true. |
||
334 | * This parameter is ignored if [[enableSession]] is false. |
||
335 | * @return bool whether the user is logged out |
||
336 | */ |
||
337 | public function logout($destroySession = true) |
||
353 | |||
354 | /** |
||
355 | * Returns a value indicating whether the user is a guest (not authenticated). |
||
356 | * @return bool whether the current user is a guest. |
||
357 | * @see getIdentity() |
||
358 | */ |
||
359 | 16 | public function getIsGuest() |
|
363 | |||
364 | /** |
||
365 | * Returns a value that uniquely represents the user. |
||
366 | * @return string|int the unique identifier for the user. If `null`, it means the user is a guest. |
||
367 | * @see getIdentity() |
||
368 | */ |
||
369 | 46 | public function getId() |
|
375 | |||
376 | /** |
||
377 | * Returns the URL that the browser should be redirected to after successful login. |
||
378 | * |
||
379 | * This method reads the return URL from the session. It is usually used by the login action which |
||
380 | * may call this method to redirect the browser to where it goes after successful authentication. |
||
381 | * |
||
382 | * @param string|array $defaultUrl the default return URL in case it was not set previously. |
||
383 | * If this is null and the return URL was not set previously, [[Application::homeUrl]] will be redirected to. |
||
384 | * Please refer to [[setReturnUrl()]] on accepted format of the URL. |
||
385 | * @return string the URL that the user should be redirected to after login. |
||
386 | * @see loginRequired() |
||
387 | */ |
||
388 | 2 | public function getReturnUrl($defaultUrl = null) |
|
401 | |||
402 | /** |
||
403 | * Remembers the URL in the session so that it can be retrieved back later by [[getReturnUrl()]]. |
||
404 | * @param string|array $url the URL that the user should be redirected to after login. |
||
405 | * If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL. |
||
406 | * The first element of the array should be the route, and the rest of |
||
407 | * the name-value pairs are GET parameters used to construct the URL. For example, |
||
408 | * |
||
409 | * ```php |
||
410 | * ['admin/index', 'ref' => 1] |
||
411 | * ``` |
||
412 | */ |
||
413 | 2 | public function setReturnUrl($url) |
|
417 | |||
418 | /** |
||
419 | * Redirects the user browser to the login page. |
||
420 | * |
||
421 | * Before the redirection, the current URL (if it's not an AJAX url) will be kept as [[returnUrl]] so that |
||
422 | * the user browser may be redirected back to the current page after successful login. |
||
423 | * |
||
424 | * Make sure you set [[loginUrl]] so that the user browser can be redirected to the specified login URL after |
||
425 | * calling this method. |
||
426 | * |
||
427 | * Note that when [[loginUrl]] is set, calling this method will NOT terminate the application execution. |
||
428 | * |
||
429 | * @param bool $checkAjax whether to check if the request is an AJAX request. When this is true and the request |
||
430 | * is an AJAX request, the current URL (for AJAX request) will NOT be set as the return URL. |
||
431 | * @param bool $checkAcceptHeader whether to check if the request accepts HTML responses. Defaults to `true`. When this is true and |
||
432 | * the request does not accept HTML responses the current URL will not be SET as the return URL. Also instead of |
||
433 | * redirecting the user an ForbiddenHttpException is thrown. This parameter is available since version 2.0.8. |
||
434 | * @return Response the redirection response if [[loginUrl]] is set |
||
435 | * @throws ForbiddenHttpException the "Access Denied" HTTP exception if [[loginUrl]] is not set or a redirect is |
||
436 | * not applicable. |
||
437 | */ |
||
438 | 2 | public function loginRequired($checkAjax = true, $checkAcceptHeader = true) |
|
457 | |||
458 | /** |
||
459 | * This method is called before logging in a user. |
||
460 | * The default implementation will trigger the [[EVENT_BEFORE_LOGIN]] event. |
||
461 | * If you override this method, make sure you call the parent implementation |
||
462 | * so that the event is triggered. |
||
463 | * @param IdentityInterface $identity the user identity information |
||
464 | * @param bool $cookieBased whether the login is cookie-based |
||
465 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
466 | * If 0, it means login till the user closes the browser or the session is manually destroyed. |
||
467 | * @return bool whether the user should continue to be logged in |
||
468 | */ |
||
469 | 15 | protected function beforeLogin($identity, $cookieBased, $duration) |
|
481 | |||
482 | /** |
||
483 | * This method is called after the user is successfully logged in. |
||
484 | * The default implementation will trigger the [[EVENT_AFTER_LOGIN]] event. |
||
485 | * If you override this method, make sure you call the parent implementation |
||
486 | * so that the event is triggered. |
||
487 | * @param IdentityInterface $identity the user identity information |
||
488 | * @param bool $cookieBased whether the login is cookie-based |
||
489 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
490 | * If 0, it means login till the user closes the browser or the session is manually destroyed. |
||
491 | */ |
||
492 | 15 | protected function afterLogin($identity, $cookieBased, $duration) |
|
501 | |||
502 | /** |
||
503 | * This method is invoked when calling [[logout()]] to log out a user. |
||
504 | * The default implementation will trigger the [[EVENT_BEFORE_LOGOUT]] event. |
||
505 | * If you override this method, make sure you call the parent implementation |
||
506 | * so that the event is triggered. |
||
507 | * @param IdentityInterface $identity the user identity information |
||
508 | * @return bool whether the user should continue to be logged out |
||
509 | */ |
||
510 | protected function beforeLogout($identity) |
||
520 | |||
521 | /** |
||
522 | * This method is invoked right after a user is logged out via [[logout()]]. |
||
523 | * The default implementation will trigger the [[EVENT_AFTER_LOGOUT]] event. |
||
524 | * If you override this method, make sure you call the parent implementation |
||
525 | * so that the event is triggered. |
||
526 | * @param IdentityInterface $identity the user identity information |
||
527 | */ |
||
528 | protected function afterLogout($identity) |
||
535 | |||
536 | /** |
||
537 | * Renews the identity cookie. |
||
538 | * This method will set the expiration time of the identity cookie to be the current time |
||
539 | * plus the originally specified cookie duration. |
||
540 | */ |
||
541 | protected function renewIdentityCookie() |
||
557 | |||
558 | /** |
||
559 | * Sends an identity cookie. |
||
560 | * This method is used when [[enableAutoLogin]] is true. |
||
561 | * It saves [[id]], [[IdentityInterface::getAuthKey()|auth key]], and the duration of cookie-based login |
||
562 | * information in the cookie. |
||
563 | * @param IdentityInterface $identity |
||
564 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
565 | * @see loginByCookie() |
||
566 | */ |
||
567 | 2 | protected function sendIdentityCookie($identity, $duration) |
|
580 | |||
581 | /** |
||
582 | * Determines if an identity cookie has a valid format and contains a valid auth key. |
||
583 | * This method is used when [[enableAutoLogin]] is true. |
||
584 | * This method attempts to authenticate a user using the information in the identity cookie. |
||
585 | * @return array|null Returns an array of 'identity' and 'duration' if valid, otherwise null. |
||
586 | * @see loginByCookie() |
||
587 | * @since 2.0.9 |
||
588 | */ |
||
589 | 2 | protected function getIdentityAndDurationFromCookie() |
|
614 | |||
615 | /** |
||
616 | * Removes the identity cookie. |
||
617 | * This method is used when [[enableAutoLogin]] is true. |
||
618 | * @since 2.0.9 |
||
619 | */ |
||
620 | 2 | protected function removeIdentityCookie() |
|
626 | |||
627 | /** |
||
628 | * Switches to a new identity for the current user. |
||
629 | * |
||
630 | * When [[enableSession]] is true, this method may use session and/or cookie to store the user identity information, |
||
631 | * according to the value of `$duration`. Please refer to [[login()]] for more details. |
||
632 | * |
||
633 | * This method is mainly called by [[login()]], [[logout()]] and [[loginByCookie()]] |
||
634 | * when the current user needs to be associated with the corresponding identity information. |
||
635 | * |
||
636 | * @param IdentityInterface|null $identity the identity information to be associated with the current user. |
||
637 | * If null, it means switching the current user to be a guest. |
||
638 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
639 | * This parameter is used only when `$identity` is not null. |
||
640 | */ |
||
641 | 18 | public function switchIdentity($identity, $duration = 0) |
|
674 | |||
675 | /** |
||
676 | * Updates the authentication status using the information from session and cookie. |
||
677 | * |
||
678 | * This method will try to determine the user identity using the [[idParam]] session variable. |
||
679 | * |
||
680 | * If [[authTimeout]] is set, this method will refresh the timer. |
||
681 | * |
||
682 | * If the user identity cannot be determined by session, this method will try to [[loginByCookie()|login by cookie]] |
||
683 | * if [[enableAutoLogin]] is true. |
||
684 | */ |
||
685 | 16 | protected function renewAuthStatus() |
|
718 | |||
719 | /** |
||
720 | * Checks if the user can perform the operation as specified by the given permission. |
||
721 | * |
||
722 | * Note that you must configure "authManager" application component in order to use this method. |
||
723 | * Otherwise it will always return false. |
||
724 | * |
||
725 | * @param string $permissionName the name of the permission (e.g. "edit post") that needs access check. |
||
726 | * @param array $params name-value pairs that would be passed to the rules associated |
||
727 | * with the roles and permissions assigned to the user. |
||
728 | * @param bool $allowCaching whether to allow caching the result of access check. |
||
729 | * When this parameter is true (default), if the access check of an operation was performed |
||
730 | * before, its result will be directly returned when calling this method to check the same |
||
731 | * operation. If this parameter is false, this method will always call |
||
732 | * [[\yii\rbac\CheckAccessInterface::checkAccess()]] to obtain the up-to-date access result. Note that this |
||
733 | * caching is effective only within the same request and only works when `$params = []`. |
||
734 | * @return bool whether the user can perform the operation as specified by the given permission. |
||
735 | */ |
||
736 | 20 | public function can($permissionName, $params = [], $allowCaching = true) |
|
751 | |||
752 | /** |
||
753 | * Checks if the `Accept` header contains a content type that allows redirection to the login page. |
||
754 | * The login page is assumed to serve `text/html` or `application/xhtml+xml` by default. You can change acceptable |
||
755 | * content types by modifying [[acceptableRedirectTypes]] property. |
||
756 | * @return bool whether this request may be redirected to the login page. |
||
757 | * @see acceptableRedirectTypes |
||
758 | * @since 2.0.8 |
||
759 | */ |
||
760 | 2 | protected function checkRedirectAcceptable() |
|
775 | |||
776 | /** |
||
777 | * Returns the access checker used for checking access. |
||
778 | * |
||
779 | * By default this is the `authManager` application component. |
||
780 | * |
||
781 | * @return CheckAccessInterface |
||
782 | * @since 2.0.9 |
||
783 | */ |
||
784 | 20 | protected function getAccessChecker() |
|
788 | } |
||
789 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..