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 |
||
60 | class User extends Component |
||
61 | { |
||
62 | const EVENT_BEFORE_LOGIN = 'beforeLogin'; |
||
63 | const EVENT_AFTER_LOGIN = 'afterLogin'; |
||
64 | const EVENT_BEFORE_LOGOUT = 'beforeLogout'; |
||
65 | const EVENT_AFTER_LOGOUT = 'afterLogout'; |
||
66 | |||
67 | /** |
||
68 | * @var string the class name of the [[identity]] object. |
||
69 | */ |
||
70 | public $identityClass; |
||
71 | /** |
||
72 | * @var bool whether to enable cookie-based login. Defaults to `false`. |
||
73 | * Note that this property will be ignored if [[enableSession]] is `false`. |
||
74 | */ |
||
75 | public $enableAutoLogin = false; |
||
76 | /** |
||
77 | * @var bool whether to use session to persist authentication status across multiple requests. |
||
78 | * You set this property to be `false` if your application is stateless, which is often the case |
||
79 | * for RESTful APIs. |
||
80 | */ |
||
81 | public $enableSession = true; |
||
82 | /** |
||
83 | * @var string|array the URL for login when [[loginRequired()]] is called. |
||
84 | * If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL. |
||
85 | * The first element of the array should be the route to the login action, and the rest of |
||
86 | * the name-value pairs are GET parameters used to construct the login URL. For example, |
||
87 | * |
||
88 | * ```php |
||
89 | * ['site/login', 'ref' => 1] |
||
90 | * ``` |
||
91 | * |
||
92 | * If this property is `null`, a 403 HTTP exception will be raised when [[loginRequired()]] is called. |
||
93 | */ |
||
94 | public $loginUrl = ['site/login']; |
||
95 | /** |
||
96 | * @var array the configuration of the identity cookie. This property is used only when [[enableAutoLogin]] is `true`. |
||
97 | * @see Cookie |
||
98 | */ |
||
99 | public $identityCookie = ['name' => '_identity', 'httpOnly' => true]; |
||
100 | /** |
||
101 | * @var int the number of seconds in which the user will be logged out automatically if he |
||
102 | * remains inactive. If this property is not set, the user will be logged out after |
||
103 | * the current session expires (c.f. [[Session::timeout]]). |
||
104 | * Note that this will not work if [[enableAutoLogin]] is `true`. |
||
105 | */ |
||
106 | public $authTimeout; |
||
107 | /** |
||
108 | * @var CheckAccessInterface The access checker to use for checking access. |
||
109 | * If not set the application auth manager will be used. |
||
110 | * @since 2.0.9 |
||
111 | */ |
||
112 | public $accessChecker; |
||
113 | /** |
||
114 | * @var int the number of seconds in which the user will be logged out automatically |
||
115 | * regardless of activity. |
||
116 | * Note that this will not work if [[enableAutoLogin]] is `true`. |
||
117 | */ |
||
118 | public $absoluteAuthTimeout; |
||
119 | /** |
||
120 | * @var bool whether to automatically renew the identity cookie each time a page is requested. |
||
121 | * This property is effective only when [[enableAutoLogin]] is `true`. |
||
122 | * When this is `false`, the identity cookie will expire after the specified duration since the user |
||
123 | * is initially logged in. When this is `true`, the identity cookie will expire after the specified duration |
||
124 | * since the user visits the site the last time. |
||
125 | * @see enableAutoLogin |
||
126 | */ |
||
127 | public $autoRenewCookie = true; |
||
128 | /** |
||
129 | * @var string the session variable name used to store the value of [[id]]. |
||
130 | */ |
||
131 | public $idParam = '__id'; |
||
132 | /** |
||
133 | * @var string the session variable name used to store the value of expiration timestamp of the authenticated state. |
||
134 | * This is used when [[authTimeout]] is set. |
||
135 | */ |
||
136 | public $authTimeoutParam = '__expire'; |
||
137 | /** |
||
138 | * @var string the session variable name used to store the value of absolute expiration timestamp of the authenticated state. |
||
139 | * This is used when [[absoluteAuthTimeout]] is set. |
||
140 | */ |
||
141 | public $absoluteAuthTimeoutParam = '__absoluteExpire'; |
||
142 | /** |
||
143 | * @var string the session variable name used to store the value of [[returnUrl]]. |
||
144 | */ |
||
145 | public $returnUrlParam = '__returnUrl'; |
||
146 | /** |
||
147 | * @var array MIME types for which this component should redirect to the [[loginUrl]]. |
||
148 | * @since 2.0.8 |
||
149 | */ |
||
150 | public $acceptableRedirectTypes = ['text/html', 'application/xhtml+xml']; |
||
151 | |||
152 | private $_access = []; |
||
153 | |||
154 | |||
155 | /** |
||
156 | * Initializes the application component. |
||
157 | */ |
||
158 | 82 | public function init() |
|
172 | |||
173 | private $_identity = false; |
||
174 | |||
175 | /** |
||
176 | * Returns the identity object associated with the currently logged-in user. |
||
177 | * When [[enableSession]] is true, this method may attempt to read the user's authentication data |
||
178 | * stored in session and reconstruct the corresponding identity object, if it has not done so before. |
||
179 | * @param bool $autoRenew whether to automatically renew authentication status if it has not been done so before. |
||
180 | * This is only useful when [[enableSession]] is true. |
||
181 | * @return IdentityInterface|null the identity object associated with the currently logged-in user. |
||
182 | * `null` is returned if the user is not logged in (not authenticated). |
||
183 | * @see login() |
||
184 | * @see logout() |
||
185 | */ |
||
186 | 74 | public function getIdentity($autoRenew = true) |
|
199 | |||
200 | /** |
||
201 | * Sets the user identity object. |
||
202 | * |
||
203 | * Note that this method does not deal with session or cookie. You should usually use [[switchIdentity()]] |
||
204 | * to change the identity of the current user. |
||
205 | * |
||
206 | * @param IdentityInterface|null $identity the identity object associated with the currently logged user. |
||
207 | * If null, it means the current user will be a guest without any associated identity. |
||
208 | * @throws InvalidValueException if `$identity` object does not implement [[IdentityInterface]]. |
||
209 | */ |
||
210 | 74 | public function setIdentity($identity) |
|
221 | |||
222 | /** |
||
223 | * Logs in a user. |
||
224 | * |
||
225 | * After logging in a user: |
||
226 | * - the user's identity information is obtainable from the [[identity]] property |
||
227 | * |
||
228 | * If [[enableSession]] is `true`: |
||
229 | * - the identity information will be stored in session and be available in the next requests |
||
230 | * - in case of `$duration == 0`: as long as the session remains active or till the user closes the browser |
||
231 | * - in case of `$duration > 0`: as long as the session remains active or as long as the cookie |
||
232 | * remains valid by it's `$duration` in seconds when [[enableAutoLogin]] is set `true`. |
||
233 | * |
||
234 | * If [[enableSession]] is `false`: |
||
235 | * - the `$duration` parameter will be ignored |
||
236 | * |
||
237 | * @param IdentityInterface $identity the user identity (which should already be authenticated) |
||
238 | * @param int $duration number of seconds that the user can remain in logged-in status, defaults to `0` |
||
239 | * @return bool whether the user is logged in |
||
240 | */ |
||
241 | 30 | public function login(IdentityInterface $identity, $duration = 0) |
|
258 | |||
259 | /** |
||
260 | * Logs in a user by the given access token. |
||
261 | * This method will first authenticate the user by calling [[IdentityInterface::findIdentityByAccessToken()]] |
||
262 | * with the provided access token. If successful, it will call [[login()]] to log in the authenticated user. |
||
263 | * If authentication fails or [[login()]] is unsuccessful, it will return null. |
||
264 | * @param string $token the access token |
||
265 | * @param mixed $type the type of the token. The value of this parameter depends on the implementation. |
||
266 | * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`. |
||
267 | * @return IdentityInterface|null the identity associated with the given access token. Null is returned if |
||
268 | * the access token is invalid or [[login()]] is unsuccessful. |
||
269 | */ |
||
270 | 40 | public function loginByAccessToken($token, $type = null) |
|
281 | |||
282 | /** |
||
283 | * Logs in a user by cookie. |
||
284 | * |
||
285 | * This method attempts to log in a user using the ID and authKey information |
||
286 | * provided by the [[identityCookie|identity cookie]]. |
||
287 | */ |
||
288 | 2 | protected function loginByCookie() |
|
303 | |||
304 | /** |
||
305 | * Logs out the current user. |
||
306 | * This will remove authentication-related session data. |
||
307 | * If `$destroySession` is true, all session data will be removed. |
||
308 | * @param bool $destroySession whether to destroy the whole session. Defaults to true. |
||
309 | * This parameter is ignored if [[enableSession]] is false. |
||
310 | * @return bool whether the user is logged out |
||
311 | */ |
||
312 | public function logout($destroySession = true) |
||
328 | |||
329 | /** |
||
330 | * Returns a value indicating whether the user is a guest (not authenticated). |
||
331 | * @return bool whether the current user is a guest. |
||
332 | * @see getIdentity() |
||
333 | */ |
||
334 | 30 | public function getIsGuest() |
|
338 | |||
339 | /** |
||
340 | * Returns a value that uniquely represents the user. |
||
341 | * @return string|int the unique identifier for the user. If `null`, it means the user is a guest. |
||
342 | * @see getIdentity() |
||
343 | */ |
||
344 | 71 | public function getId() |
|
350 | |||
351 | /** |
||
352 | * Returns the URL that the browser should be redirected to after successful login. |
||
353 | * |
||
354 | * This method reads the return URL from the session. It is usually used by the login action which |
||
355 | * may call this method to redirect the browser to where it goes after successful authentication. |
||
356 | * |
||
357 | * @param string|array $defaultUrl the default return URL in case it was not set previously. |
||
358 | * If this is null and the return URL was not set previously, [[Application::homeUrl]] will be redirected to. |
||
359 | * Please refer to [[setReturnUrl()]] on accepted format of the URL. |
||
360 | * @return string the URL that the user should be redirected to after login. |
||
361 | * @see loginRequired() |
||
362 | */ |
||
363 | 1 | public function getReturnUrl($defaultUrl = null) |
|
376 | |||
377 | /** |
||
378 | * Remembers the URL in the session so that it can be retrieved back later by [[getReturnUrl()]]. |
||
379 | * @param string|array $url the URL that the user should be redirected to after login. |
||
380 | * If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL. |
||
381 | * The first element of the array should be the route, and the rest of |
||
382 | * the name-value pairs are GET parameters used to construct the URL. For example, |
||
383 | * |
||
384 | * ```php |
||
385 | * ['admin/index', 'ref' => 1] |
||
386 | * ``` |
||
387 | */ |
||
388 | 2 | public function setReturnUrl($url) |
|
392 | |||
393 | /** |
||
394 | * Redirects the user browser to the login page. |
||
395 | * |
||
396 | * Before the redirection, the current URL (if it's not an AJAX url) will be kept as [[returnUrl]] so that |
||
397 | * the user browser may be redirected back to the current page after successful login. |
||
398 | * |
||
399 | * Make sure you set [[loginUrl]] so that the user browser can be redirected to the specified login URL after |
||
400 | * calling this method. |
||
401 | * |
||
402 | * Note that when [[loginUrl]] is set, calling this method will NOT terminate the application execution. |
||
403 | * |
||
404 | * @param bool $checkAjax whether to check if the request is an AJAX request. When this is true and the request |
||
405 | * is an AJAX request, the current URL (for AJAX request) will NOT be set as the return URL. |
||
406 | * @param bool $checkAcceptHeader whether to check if the request accepts HTML responses. Defaults to `true`. When this is true and |
||
407 | * the request does not accept HTML responses the current URL will not be SET as the return URL. Also instead of |
||
408 | * redirecting the user an ForbiddenHttpException is thrown. This parameter is available since version 2.0.8. |
||
409 | * @return Response the redirection response if [[loginUrl]] is set |
||
410 | * @throws ForbiddenHttpException the "Access Denied" HTTP exception if [[loginUrl]] is not set or a redirect is |
||
411 | * not applicable. |
||
412 | */ |
||
413 | 2 | public function loginRequired($checkAjax = true, $checkAcceptHeader = true) |
|
432 | |||
433 | /** |
||
434 | * This method is called before logging in a user. |
||
435 | * The default implementation will trigger the [[EVENT_BEFORE_LOGIN]] event. |
||
436 | * If you override this method, make sure you call the parent implementation |
||
437 | * so that the event is triggered. |
||
438 | * @param IdentityInterface $identity the user identity information |
||
439 | * @param bool $cookieBased whether the login is cookie-based |
||
440 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
441 | * If 0, it means login till the user closes the browser or the session is manually destroyed. |
||
442 | * @return bool whether the user should continue to be logged in |
||
443 | */ |
||
444 | 30 | protected function beforeLogin($identity, $cookieBased, $duration) |
|
455 | |||
456 | /** |
||
457 | * This method is called after the user is successfully logged in. |
||
458 | * The default implementation will trigger the [[EVENT_AFTER_LOGIN]] event. |
||
459 | * If you override this method, make sure you call the parent implementation |
||
460 | * so that the event is triggered. |
||
461 | * @param IdentityInterface $identity the user identity information |
||
462 | * @param bool $cookieBased whether the login is cookie-based |
||
463 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
464 | * If 0, it means login till the user closes the browser or the session is manually destroyed. |
||
465 | */ |
||
466 | 29 | protected function afterLogin($identity, $cookieBased, $duration) |
|
474 | |||
475 | /** |
||
476 | * This method is invoked when calling [[logout()]] to log out a user. |
||
477 | * The default implementation will trigger the [[EVENT_BEFORE_LOGOUT]] event. |
||
478 | * If you override this method, make sure you call the parent implementation |
||
479 | * so that the event is triggered. |
||
480 | * @param IdentityInterface $identity the user identity information |
||
481 | * @return bool whether the user should continue to be logged out |
||
482 | */ |
||
483 | protected function beforeLogout($identity) |
||
492 | |||
493 | /** |
||
494 | * This method is invoked right after a user is logged out via [[logout()]]. |
||
495 | * The default implementation will trigger the [[EVENT_AFTER_LOGOUT]] event. |
||
496 | * If you override this method, make sure you call the parent implementation |
||
497 | * so that the event is triggered. |
||
498 | * @param IdentityInterface $identity the user identity information |
||
499 | */ |
||
500 | protected function afterLogout($identity) |
||
506 | |||
507 | /** |
||
508 | * Renews the identity cookie. |
||
509 | * This method will set the expiration time of the identity cookie to be the current time |
||
510 | * plus the originally specified cookie duration. |
||
511 | */ |
||
512 | protected function renewIdentityCookie() |
||
528 | |||
529 | /** |
||
530 | * Sends an identity cookie. |
||
531 | * This method is used when [[enableAutoLogin]] is true. |
||
532 | * It saves [[id]], [[IdentityInterface::getAuthKey()|auth key]], and the duration of cookie-based login |
||
533 | * information in the cookie. |
||
534 | * @param IdentityInterface $identity |
||
535 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
536 | * @see loginByCookie() |
||
537 | */ |
||
538 | 2 | protected function sendIdentityCookie($identity, $duration) |
|
551 | |||
552 | /** |
||
553 | * Determines if an identity cookie has a valid format and contains a valid auth key. |
||
554 | * This method is used when [[enableAutoLogin]] is true. |
||
555 | * This method attempts to authenticate a user using the information in the identity cookie. |
||
556 | * @return array|null Returns an array of 'identity' and 'duration' if valid, otherwise null. |
||
557 | * @see loginByCookie() |
||
558 | * @since 2.0.9 |
||
559 | */ |
||
560 | 2 | protected function getIdentityAndDurationFromCookie() |
|
585 | |||
586 | /** |
||
587 | * Removes the identity cookie. |
||
588 | * This method is used when [[enableAutoLogin]] is true. |
||
589 | * @since 2.0.9 |
||
590 | */ |
||
591 | 2 | protected function removeIdentityCookie() |
|
597 | |||
598 | /** |
||
599 | * Switches to a new identity for the current user. |
||
600 | * |
||
601 | * When [[enableSession]] is true, this method may use session and/or cookie to store the user identity information, |
||
602 | * according to the value of `$duration`. Please refer to [[login()]] for more details. |
||
603 | * |
||
604 | * This method is mainly called by [[login()]], [[logout()]] and [[loginByCookie()]] |
||
605 | * when the current user needs to be associated with the corresponding identity information. |
||
606 | * |
||
607 | * @param IdentityInterface|null $identity the identity information to be associated with the current user. |
||
608 | * If null, it means switching the current user to be a guest. |
||
609 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
610 | * This parameter is used only when `$identity` is not null. |
||
611 | */ |
||
612 | 33 | public function switchIdentity($identity, $duration = 0) |
|
648 | |||
649 | /** |
||
650 | * Updates the authentication status using the information from session and cookie. |
||
651 | * |
||
652 | * This method will try to determine the user identity using the [[idParam]] session variable. |
||
653 | * |
||
654 | * If [[authTimeout]] is set, this method will refresh the timer. |
||
655 | * |
||
656 | * If the user identity cannot be determined by session, this method will try to [[loginByCookie()|login by cookie]] |
||
657 | * if [[enableAutoLogin]] is true. |
||
658 | */ |
||
659 | 22 | protected function renewAuthStatus() |
|
692 | |||
693 | /** |
||
694 | * Checks if the user can perform the operation as specified by the given permission. |
||
695 | * |
||
696 | * Note that you must configure "authManager" application component in order to use this method. |
||
697 | * Otherwise it will always return false. |
||
698 | * |
||
699 | * @param string $permissionName the name of the permission (e.g. "edit post") that needs access check. |
||
700 | * @param array $params name-value pairs that would be passed to the rules associated |
||
701 | * with the roles and permissions assigned to the user. |
||
702 | * @param bool $allowCaching whether to allow caching the result of access check. |
||
703 | * When this parameter is true (default), if the access check of an operation was performed |
||
704 | * before, its result will be directly returned when calling this method to check the same |
||
705 | * operation. If this parameter is false, this method will always call |
||
706 | * [[\yii\rbac\CheckAccessInterface::checkAccess()]] to obtain the up-to-date access result. Note that this |
||
707 | * caching is effective only within the same request and only works when `$params = []`. |
||
708 | * @return bool whether the user can perform the operation as specified by the given permission. |
||
709 | */ |
||
710 | 20 | public function can($permissionName, $params = [], $allowCaching = true) |
|
725 | |||
726 | /** |
||
727 | * Checks if the `Accept` header contains a content type that allows redirection to the login page. |
||
728 | * The login page is assumed to serve `text/html` or `application/xhtml+xml` by default. You can change acceptable |
||
729 | * content types by modifying [[acceptableRedirectTypes]] property. |
||
730 | * @return bool whether this request may be redirected to the login page. |
||
731 | * @see acceptableRedirectTypes |
||
732 | * @since 2.0.8 |
||
733 | */ |
||
734 | 2 | protected function checkRedirectAcceptable() |
|
749 | |||
750 | /** |
||
751 | * Returns auth manager associated with the user component. |
||
752 | * |
||
753 | * By default this is the `authManager` application component. |
||
754 | * You may override this method to return a different auth manager instance if needed. |
||
755 | * @return \yii\rbac\ManagerInterface |
||
756 | * @since 2.0.6 |
||
757 | * @deprecated since version 2.0.9, to be removed in 2.1. Use [[getAccessChecker()]] instead. |
||
758 | */ |
||
759 | protected function getAuthManager() |
||
763 | |||
764 | /** |
||
765 | * Returns the access checker used for checking access. |
||
766 | * @return CheckAccessInterface |
||
767 | * @since 2.0.9 |
||
768 | */ |
||
769 | 20 | protected function getAccessChecker() |
|
773 | } |
||
774 |
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..