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 | 91 | 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 | 81 | public function getIdentity($autoRenew = true) |
|
187 | { |
||
188 | 81 | if ($this->_identity === false) { |
|
189 | 29 | if ($this->enableSession && $autoRenew) { |
|
190 | try { |
||
191 | 28 | $this->_identity = null; |
|
192 | 28 | $this->renewAuthStatus(); |
|
193 | 1 | } catch (\Exception $e) { |
|
194 | 1 | $this->_identity = false; |
|
195 | 1 | throw $e; |
|
196 | } catch (\Throwable $e) { |
||
|
|||
197 | $this->_identity = false; |
||
198 | 27 | throw $e; |
|
199 | } |
||
200 | } else { |
||
201 | 1 | return null; |
|
202 | } |
||
203 | } |
||
204 | |||
205 | 79 | return $this->_identity; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Sets the user identity object. |
||
210 | * |
||
211 | * Note that this method does not deal with session or cookie. You should usually use [[switchIdentity()]] |
||
212 | * to change the identity of the current user. |
||
213 | * |
||
214 | * @param IdentityInterface|null $identity the identity object associated with the currently logged user. |
||
215 | * If null, it means the current user will be a guest without any associated identity. |
||
216 | * @throws InvalidValueException if `$identity` object does not implement [[IdentityInterface]]. |
||
217 | */ |
||
218 | 79 | public function setIdentity($identity) |
|
229 | |||
230 | /** |
||
231 | * Logs in a user. |
||
232 | * |
||
233 | * After logging in a user: |
||
234 | * - the user's identity information is obtainable from the [[identity]] property |
||
235 | * |
||
236 | * If [[enableSession]] is `true`: |
||
237 | * - the identity information will be stored in session and be available in the next requests |
||
238 | * - in case of `$duration == 0`: as long as the session remains active or till the user closes the browser |
||
239 | * - in case of `$duration > 0`: as long as the session remains active or as long as the cookie |
||
240 | * remains valid by it's `$duration` in seconds when [[enableAutoLogin]] is set `true`. |
||
241 | * |
||
242 | * If [[enableSession]] is `false`: |
||
243 | * - the `$duration` parameter will be ignored |
||
244 | * |
||
245 | * @param IdentityInterface $identity the user identity (which should already be authenticated) |
||
246 | * @param int $duration number of seconds that the user can remain in logged-in status, defaults to `0` |
||
247 | * @return bool whether the user is logged in |
||
248 | */ |
||
249 | 33 | public function login(IdentityInterface $identity, $duration = 0) |
|
269 | |||
270 | /** |
||
271 | * Regenerates CSRF token |
||
272 | * |
||
273 | * @since 2.0.14.2 |
||
274 | */ |
||
275 | 33 | protected function regenerateCsrfToken() |
|
282 | |||
283 | /** |
||
284 | * Logs in a user by the given access token. |
||
285 | * This method will first authenticate the user by calling [[IdentityInterface::findIdentityByAccessToken()]] |
||
286 | * with the provided access token. If successful, it will call [[login()]] to log in the authenticated user. |
||
287 | * If authentication fails or [[login()]] is unsuccessful, it will return null. |
||
288 | * @param string $token the access token |
||
289 | * @param mixed $type the type of the token. The value of this parameter depends on the implementation. |
||
290 | * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`. |
||
291 | * @return IdentityInterface|null the identity associated with the given access token. Null is returned if |
||
292 | * the access token is invalid or [[login()]] is unsuccessful. |
||
293 | */ |
||
294 | 42 | public function loginByAccessToken($token, $type = null) |
|
305 | |||
306 | /** |
||
307 | * Logs in a user by cookie. |
||
308 | * |
||
309 | * This method attempts to log in a user using the ID and authKey information |
||
310 | * provided by the [[identityCookie|identity cookie]]. |
||
311 | */ |
||
312 | 2 | protected function loginByCookie() |
|
327 | |||
328 | /** |
||
329 | * Logs out the current user. |
||
330 | * This will remove authentication-related session data. |
||
331 | * If `$destroySession` is true, all session data will be removed. |
||
332 | * @param bool $destroySession whether to destroy the whole session. Defaults to true. |
||
333 | * This parameter is ignored if [[enableSession]] is false. |
||
334 | * @return bool whether the user is logged out |
||
335 | */ |
||
336 | public function logout($destroySession = true) |
||
352 | |||
353 | /** |
||
354 | * Returns a value indicating whether the user is a guest (not authenticated). |
||
355 | * @return bool whether the current user is a guest. |
||
356 | * @see getIdentity() |
||
357 | */ |
||
358 | 34 | public function getIsGuest() |
|
362 | |||
363 | /** |
||
364 | * Returns a value that uniquely represents the user. |
||
365 | * @return string|int the unique identifier for the user. If `null`, it means the user is a guest. |
||
366 | * @see getIdentity() |
||
367 | */ |
||
368 | 76 | public function getId() |
|
374 | |||
375 | /** |
||
376 | * Returns the URL that the browser should be redirected to after successful login. |
||
377 | * |
||
378 | * This method reads the return URL from the session. It is usually used by the login action which |
||
379 | * may call this method to redirect the browser to where it goes after successful authentication. |
||
380 | * |
||
381 | * @param string|array $defaultUrl the default return URL in case it was not set previously. |
||
382 | * If this is null and the return URL was not set previously, [[Application::homeUrl]] will be redirected to. |
||
383 | * Please refer to [[setReturnUrl()]] on accepted format of the URL. |
||
384 | * @return string the URL that the user should be redirected to after login. |
||
385 | * @see loginRequired() |
||
386 | */ |
||
387 | 2 | public function getReturnUrl($defaultUrl = null) |
|
400 | |||
401 | /** |
||
402 | * Remembers the URL in the session so that it can be retrieved back later by [[getReturnUrl()]]. |
||
403 | * @param string|array $url the URL that the user should be redirected to after login. |
||
404 | * If an array is given, [[UrlManager::createUrl()]] will be called to create the corresponding URL. |
||
405 | * The first element of the array should be the route, and the rest of |
||
406 | * the name-value pairs are GET parameters used to construct the URL. For example, |
||
407 | * |
||
408 | * ```php |
||
409 | * ['admin/index', 'ref' => 1] |
||
410 | * ``` |
||
411 | */ |
||
412 | 2 | public function setReturnUrl($url) |
|
416 | |||
417 | /** |
||
418 | * Redirects the user browser to the login page. |
||
419 | * |
||
420 | * Before the redirection, the current URL (if it's not an AJAX url) will be kept as [[returnUrl]] so that |
||
421 | * the user browser may be redirected back to the current page after successful login. |
||
422 | * |
||
423 | * Make sure you set [[loginUrl]] so that the user browser can be redirected to the specified login URL after |
||
424 | * calling this method. |
||
425 | * |
||
426 | * Note that when [[loginUrl]] is set, calling this method will NOT terminate the application execution. |
||
427 | * |
||
428 | * @param bool $checkAjax whether to check if the request is an AJAX request. When this is true and the request |
||
429 | * is an AJAX request, the current URL (for AJAX request) will NOT be set as the return URL. |
||
430 | * @param bool $checkAcceptHeader whether to check if the request accepts HTML responses. Defaults to `true`. When this is true and |
||
431 | * the request does not accept HTML responses the current URL will not be SET as the return URL. Also instead of |
||
432 | * redirecting the user an ForbiddenHttpException is thrown. This parameter is available since version 2.0.8. |
||
433 | * @return Response the redirection response if [[loginUrl]] is set |
||
434 | * @throws ForbiddenHttpException the "Access Denied" HTTP exception if [[loginUrl]] is not set or a redirect is |
||
435 | * not applicable. |
||
436 | */ |
||
437 | 2 | public function loginRequired($checkAjax = true, $checkAcceptHeader = true) |
|
456 | |||
457 | /** |
||
458 | * This method is called before logging in a user. |
||
459 | * The default implementation will trigger the [[EVENT_BEFORE_LOGIN]] event. |
||
460 | * If you override this method, make sure you call the parent implementation |
||
461 | * so that the event is triggered. |
||
462 | * @param IdentityInterface $identity the user identity information |
||
463 | * @param bool $cookieBased whether the login is cookie-based |
||
464 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
465 | * If 0, it means login till the user closes the browser or the session is manually destroyed. |
||
466 | * @return bool whether the user should continue to be logged in |
||
467 | */ |
||
468 | 33 | protected function beforeLogin($identity, $cookieBased, $duration) |
|
479 | |||
480 | /** |
||
481 | * This method is called after the user is successfully logged in. |
||
482 | * The default implementation will trigger the [[EVENT_AFTER_LOGIN]] event. |
||
483 | * If you override this method, make sure you call the parent implementation |
||
484 | * so that the event is triggered. |
||
485 | * @param IdentityInterface $identity the user identity information |
||
486 | * @param bool $cookieBased whether the login is cookie-based |
||
487 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
488 | * If 0, it means login till the user closes the browser or the session is manually destroyed. |
||
489 | */ |
||
490 | 33 | protected function afterLogin($identity, $cookieBased, $duration) |
|
498 | |||
499 | /** |
||
500 | * This method is invoked when calling [[logout()]] to log out a user. |
||
501 | * The default implementation will trigger the [[EVENT_BEFORE_LOGOUT]] event. |
||
502 | * If you override this method, make sure you call the parent implementation |
||
503 | * so that the event is triggered. |
||
504 | * @param IdentityInterface $identity the user identity information |
||
505 | * @return bool whether the user should continue to be logged out |
||
506 | */ |
||
507 | protected function beforeLogout($identity) |
||
516 | |||
517 | /** |
||
518 | * This method is invoked right after a user is logged out via [[logout()]]. |
||
519 | * The default implementation will trigger the [[EVENT_AFTER_LOGOUT]] event. |
||
520 | * If you override this method, make sure you call the parent implementation |
||
521 | * so that the event is triggered. |
||
522 | * @param IdentityInterface $identity the user identity information |
||
523 | */ |
||
524 | protected function afterLogout($identity) |
||
530 | |||
531 | /** |
||
532 | * Renews the identity cookie. |
||
533 | * This method will set the expiration time of the identity cookie to be the current time |
||
534 | * plus the originally specified cookie duration. |
||
535 | */ |
||
536 | protected function renewIdentityCookie() |
||
552 | |||
553 | /** |
||
554 | * Sends an identity cookie. |
||
555 | * This method is used when [[enableAutoLogin]] is true. |
||
556 | * It saves [[id]], [[IdentityInterface::getAuthKey()|auth key]], and the duration of cookie-based login |
||
557 | * information in the cookie. |
||
558 | * @param IdentityInterface $identity |
||
559 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
560 | * @see loginByCookie() |
||
561 | */ |
||
562 | 2 | protected function sendIdentityCookie($identity, $duration) |
|
575 | |||
576 | /** |
||
577 | * Determines if an identity cookie has a valid format and contains a valid auth key. |
||
578 | * This method is used when [[enableAutoLogin]] is true. |
||
579 | * This method attempts to authenticate a user using the information in the identity cookie. |
||
580 | * @return array|null Returns an array of 'identity' and 'duration' if valid, otherwise null. |
||
581 | * @see loginByCookie() |
||
582 | * @since 2.0.9 |
||
583 | */ |
||
584 | 2 | protected function getIdentityAndDurationFromCookie() |
|
609 | |||
610 | /** |
||
611 | * Removes the identity cookie. |
||
612 | * This method is used when [[enableAutoLogin]] is true. |
||
613 | * @since 2.0.9 |
||
614 | */ |
||
615 | 2 | protected function removeIdentityCookie() |
|
621 | |||
622 | /** |
||
623 | * Switches to a new identity for the current user. |
||
624 | * |
||
625 | * When [[enableSession]] is true, this method may use session and/or cookie to store the user identity information, |
||
626 | * according to the value of `$duration`. Please refer to [[login()]] for more details. |
||
627 | * |
||
628 | * This method is mainly called by [[login()]], [[logout()]] and [[loginByCookie()]] |
||
629 | * when the current user needs to be associated with the corresponding identity information. |
||
630 | * |
||
631 | * @param IdentityInterface|null $identity the identity information to be associated with the current user. |
||
632 | * If null, it means switching the current user to be a guest. |
||
633 | * @param int $duration number of seconds that the user can remain in logged-in status. |
||
634 | * This parameter is used only when `$identity` is not null. |
||
635 | */ |
||
636 | 36 | public function switchIdentity($identity, $duration = 0) |
|
669 | |||
670 | /** |
||
671 | * Updates the authentication status using the information from session and cookie. |
||
672 | * |
||
673 | * This method will try to determine the user identity using the [[idParam]] session variable. |
||
674 | * |
||
675 | * If [[authTimeout]] is set, this method will refresh the timer. |
||
676 | * |
||
677 | * If the user identity cannot be determined by session, this method will try to [[loginByCookie()|login by cookie]] |
||
678 | * if [[enableAutoLogin]] is true. |
||
679 | */ |
||
680 | 28 | protected function renewAuthStatus() |
|
713 | |||
714 | /** |
||
715 | * Checks if the user can perform the operation as specified by the given permission. |
||
716 | * |
||
717 | * Note that you must configure "authManager" application component in order to use this method. |
||
718 | * Otherwise it will always return false. |
||
719 | * |
||
720 | * @param string $permissionName the name of the permission (e.g. "edit post") that needs access check. |
||
721 | * @param array $params name-value pairs that would be passed to the rules associated |
||
722 | * with the roles and permissions assigned to the user. |
||
723 | * @param bool $allowCaching whether to allow caching the result of access check. |
||
724 | * When this parameter is true (default), if the access check of an operation was performed |
||
725 | * before, its result will be directly returned when calling this method to check the same |
||
726 | * operation. If this parameter is false, this method will always call |
||
727 | * [[\yii\rbac\CheckAccessInterface::checkAccess()]] to obtain the up-to-date access result. Note that this |
||
728 | * caching is effective only within the same request and only works when `$params = []`. |
||
729 | * @return bool whether the user can perform the operation as specified by the given permission. |
||
730 | */ |
||
731 | 20 | public function can($permissionName, $params = [], $allowCaching = true) |
|
746 | |||
747 | /** |
||
748 | * Checks if the `Accept` header contains a content type that allows redirection to the login page. |
||
749 | * The login page is assumed to serve `text/html` or `application/xhtml+xml` by default. You can change acceptable |
||
750 | * content types by modifying [[acceptableRedirectTypes]] property. |
||
751 | * @return bool whether this request may be redirected to the login page. |
||
752 | * @see acceptableRedirectTypes |
||
753 | * @since 2.0.8 |
||
754 | */ |
||
755 | 2 | protected function checkRedirectAcceptable() |
|
770 | |||
771 | /** |
||
772 | * Returns auth manager associated with the user component. |
||
773 | * |
||
774 | * By default this is the `authManager` application component. |
||
775 | * You may override this method to return a different auth manager instance if needed. |
||
776 | * @return \yii\rbac\ManagerInterface |
||
777 | * @since 2.0.6 |
||
778 | * @deprecated since version 2.0.9, to be removed in 2.1. Use [[getAccessChecker()]] instead. |
||
779 | */ |
||
780 | protected function getAuthManager() |
||
784 | |||
785 | /** |
||
786 | * Returns the access checker used for checking access. |
||
787 | * @return CheckAccessInterface |
||
788 | * @since 2.0.9 |
||
789 | */ |
||
790 | 20 | protected function getAccessChecker() |
|
794 | } |
||
795 |
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.