diff --git a/phpstan.neon b/phpstan.neon index f5a97a53..25855aed 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 3 + level: 4 paths: - src excludePaths: diff --git a/src/User/Command/CreateController.php b/src/User/Command/CreateController.php index 0af7d666..d9efd9eb 100644 --- a/src/User/Command/CreateController.php +++ b/src/User/Command/CreateController.php @@ -64,7 +64,7 @@ public function actionIndex($email, $username, $password = null, $role = null) protected function assignRole(User $user, $role) { $auth = Yii::$app->getAuthManager(); - if (false === $auth) { + if (empty($auth)) { $this->stdout( Yii::t( 'usuario', diff --git a/src/User/Command/PasswordController.php b/src/User/Command/PasswordController.php index 9e97fcb9..8666ae76 100644 --- a/src/User/Command/PasswordController.php +++ b/src/User/Command/PasswordController.php @@ -43,7 +43,7 @@ public function __construct($id, Module $module, UserQuery $userQuery, array $co */ public function actionIndex($usernameOrEmail, $password) { - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereUsernameOrEmail($usernameOrEmail)->one(); if ($user === null) { diff --git a/src/User/Controller/AdminController.php b/src/User/Controller/AdminController.php index a1d87608..b71e47f0 100755 --- a/src/User/Controller/AdminController.php +++ b/src/User/Controller/AdminController.php @@ -191,9 +191,8 @@ public function actionUpdate($id) public function actionUpdateProfile($id) { - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->where(['id' => $id])->one(); - /** @var Profile $profile */ $profile = $user->profile; if ($profile === null) { $profile = $this->make(Profile::class); diff --git a/src/User/Controller/RecoveryController.php b/src/User/Controller/RecoveryController.php index 9925c874..9e675c19 100644 --- a/src/User/Controller/RecoveryController.php +++ b/src/User/Controller/RecoveryController.php @@ -134,7 +134,7 @@ public function actionReset($id, $code) if (!$this->module->allowPasswordRecovery && !$this->module->allowAdminPasswordRecovery) { throw new NotFoundHttpException(); } - /** @var Token $token */ + /** @var ?Token $token */ $token = $this->tokenQuery->whereUserId($id)->whereCode($code)->whereIsRecoveryType()->one(); /** @var ResetPasswordEvent $event */ $event = $this->make(ResetPasswordEvent::class, [$token]); diff --git a/src/User/Controller/RegistrationController.php b/src/User/Controller/RegistrationController.php index 0cfe1953..0463b319 100644 --- a/src/User/Controller/RegistrationController.php +++ b/src/User/Controller/RegistrationController.php @@ -157,7 +157,7 @@ public function actionConnect($code) throw new NotFoundHttpException(); } - /** @var SocialNetworkAccount $account */ + /** @var ?SocialNetworkAccount $account */ $account = $this->socialNetworkAccountQuery->whereCode($code)->one(); if ($account === null || $account->getIsConnected()) { throw new NotFoundHttpException(); @@ -205,7 +205,7 @@ public function actionConnect($code) */ public function actionConfirm($id, $code) { - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereId($id)->one(); if ($user === null || $this->module->enableEmailConfirmation === false) { @@ -254,7 +254,7 @@ public function actionResend() $this->make(AjaxRequestModelValidator::class, [$form])->validate(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereEmail($form->email)->one(); $success = true; if ($user !== null) { diff --git a/src/User/Controller/SecurityController.php b/src/User/Controller/SecurityController.php index 95a83c68..d3bae0e1 100644 --- a/src/User/Controller/SecurityController.php +++ b/src/User/Controller/SecurityController.php @@ -135,7 +135,6 @@ public function actionLogin() $errors = ActiveForm::validate($form); if (empty($errors)) { - throw new \Exception(json_encode($errors)); return $errors; } $this->trigger(FormEvent::EVENT_FAILED_LOGIN, $event); diff --git a/src/User/Controller/SettingsController.php b/src/User/Controller/SettingsController.php index 73b32855..e3ee6b84 100644 --- a/src/User/Controller/SettingsController.php +++ b/src/User/Controller/SettingsController.php @@ -464,7 +464,7 @@ public function actionTwoFactor($id) } $choice = Yii::$app->request->post('choice'); - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereId($id)->one(); if (null === $user) { @@ -496,7 +496,7 @@ public function actionTwoFactorEnable($id) Yii::$app->response->format = Response::FORMAT_JSON; - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereId($id)->one(); if (null === $user) { @@ -534,9 +534,7 @@ public function actionTwoFactorDisable($id) throw new ForbiddenHttpException(); } - /** - * @var User $user - */ + /** @var ?User $user */ $user = $this->userQuery->whereId($id)->one(); if (null === $user) { @@ -586,11 +584,7 @@ public function actionTwoFactorMobilePhone($id) { Yii::$app->response->format = Response::FORMAT_JSON; - /** - * - * - * @var User $user - */ + /** @var ?User $user */ $user = $this->userQuery->whereId($id)->one(); if (null === $user) { @@ -627,11 +621,7 @@ public function actionTwoFactorMobilePhone($id) */ protected function disconnectSocialNetwork($id) { - /** - * - * - * @var SocialNetworkAccount $account - */ + /** @var ?SocialNetworkAccount $account */ $account = $this->socialNetworkAccountQuery->whereId($id)->one(); if ($account === null) { diff --git a/src/User/Controller/api/v1/AdminController.php b/src/User/Controller/api/v1/AdminController.php index 433be3c8..e05a192f 100644 --- a/src/User/Controller/api/v1/AdminController.php +++ b/src/User/Controller/api/v1/AdminController.php @@ -189,7 +189,7 @@ public function actionUpdate($id) $this->checkAccess($this->action); // Get user model - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereIdOrUsernameOrEmail($id)->one(); if (is_null($user)) { // Check user, so `$id` parameter $this->throwUser404(); @@ -223,7 +223,7 @@ public function actionDelete($id) $this->checkAccess($this->action); // Get user model - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereIdOrUsernameOrEmail($id)->one(); if (is_null($user)) { // Check user, so `$id` parameter $this->throwUser404(); @@ -258,14 +258,14 @@ public function actionUpdateProfile($id) $this->checkAccess($this->action); // Get user model - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereIdOrUsernameOrEmail($id)->one(); if (is_null($user)) { // Check user, so `$id` parameter $this->throwUser404(); } // Get profile model - /** @var Profile $profile */ + /** @var ?Profile $profile */ $profile = $user->profile; if ($profile === null) { $profile = $this->make(Profile::class); @@ -296,7 +296,7 @@ public function actionAssignments($id) $this->checkAccess($this->action); // Get user model - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereIdOrUsernameOrEmail($id)->one(); if (is_null($user)) { // Check user, so `$id` parameter $this->throwUser404(); @@ -317,7 +317,7 @@ public function actionConfirm($id) $this->checkAccess($this->action); // Get user model - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereIdOrUsernameOrEmail($id)->one(); if (is_null($user)) { // Check user, so `$id` parameter $this->throwUser404(); @@ -352,7 +352,7 @@ public function actionBlock($id) } // Get user model - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereIdOrUsernameOrEmail($id)->one(); if (is_null($user)) { // Check user, so `$id` parameter $this->throwUser404(); @@ -380,7 +380,7 @@ public function actionPasswordReset($id) $this->checkAccess($this->action); // Get user model - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereIdOrUsernameOrEmail($id)->one(); if (is_null($user)) { // Check user, so `$id` parameter $this->throwUser404(); @@ -405,7 +405,7 @@ public function actionForcePasswordChange($id) $this->checkAccess($this->action); // Get user model - /** @var User $user */ + /** @var ?User $user */ $user = $this->userQuery->whereIdOrUsernameOrEmail($id)->one(); if (is_null($user)) { // Check user, so `$id` parameter $this->throwUser404(); diff --git a/src/User/Factory/MailFactory.php b/src/User/Factory/MailFactory.php index 6950de55..42fc6b68 100644 --- a/src/User/Factory/MailFactory.php +++ b/src/User/Factory/MailFactory.php @@ -60,7 +60,7 @@ public static function makeRecoveryMailerService($email, Token $token = null) $from = $module->mailParams['fromEmail']; $subject = $module->mailParams['recoveryMailSubject']; $params = [ - 'user' => $token && $token->user ? $token->user : null, + 'user' => $token->user, 'token' => $token, ]; @@ -82,7 +82,7 @@ public static function makeConfirmationMailerService(User $user, Token $token = $from = $module->mailParams['fromEmail']; $subject = $module->mailParams['confirmationMailSubject']; $params = [ - 'user' => $token && $token->user ? $token->user : null, + 'user' => $token->user, 'token' => $token, ]; @@ -107,7 +107,7 @@ public static function makeReconfirmationMailerService(User $user, Token $token) $from = $module->mailParams['fromEmail']; $subject = $module->mailParams['reconfirmationMailSubject']; $params = [ - 'user' => $token && $token->user ? $token->user : null, + 'user' => $token->user, 'token' => $token, ]; diff --git a/src/User/Helper/AuthHelper.php b/src/User/Helper/AuthHelper.php index ae74de96..e8b6e683 100644 --- a/src/User/Helper/AuthHelper.php +++ b/src/User/Helper/AuthHelper.php @@ -37,13 +37,8 @@ class AuthHelper */ public function hasRole($userId, $role) { - if ($this->getAuthManager()) { - $roles = array_keys($this->getAuthManager()->getRolesByUser($userId)); - - return in_array($role, $roles, true); - } - - return false; + $roles = array_keys($this->getAuthManager()->getRolesByUser($userId)); + return in_array($role, $roles, true); } /** @@ -54,7 +49,8 @@ public function hasRole($userId, $role) public function isAdmin($username) { $module = $this->getModule(); - $hasAdministratorPermissionName = $this->getAuthManager() && $module->administratorPermissionName + $this->getAuthManager(); + $hasAdministratorPermissionName = $module->administratorPermissionName ? Yii::$app->getUser()->can($module->administratorPermissionName) : false; diff --git a/src/User/Model/User.php b/src/User/Model/User.php index 26f705b7..4a6d9907 100644 --- a/src/User/Model/User.php +++ b/src/User/Model/User.php @@ -59,7 +59,7 @@ * @property int $password_age * Defined relations: * @property SocialNetworkAccount[] $socialNetworkAccounts - * @property Profile $profile + * @property ?Profile $profile */ class User extends ActiveRecord implements IdentityInterface { @@ -387,7 +387,7 @@ public function getAuthTfType() /** * Returns the mobile phone number used for sms authentication two factor for the user - * @return string + * @return ?string */ public function getAuthTfMobilePhone() { diff --git a/src/User/Service/EmailChangeService.php b/src/User/Service/EmailChangeService.php index 5eb4af6b..567ace16 100644 --- a/src/User/Service/EmailChangeService.php +++ b/src/User/Service/EmailChangeService.php @@ -39,7 +39,7 @@ public function __construct(string $code, User $model, TokenQuery $tokenQuery, U public function run() { - /** @var Token $token */ + /** @var ?Token $token */ $token = $this->tokenQuery ->whereUserId($this->model->id) ->whereCode($this->code) diff --git a/src/User/Service/PasswordRecoveryService.php b/src/User/Service/PasswordRecoveryService.php index 0e5b5978..0ed9aa91 100644 --- a/src/User/Service/PasswordRecoveryService.php +++ b/src/User/Service/PasswordRecoveryService.php @@ -46,7 +46,7 @@ public function run() Yii::t('usuario', 'An email with instructions to create a new password has been sent to {email} if it is associated with an {appName} account. Your existing password has not been changed.', ['email' => $this->email, 'appName' => Yii::$app->name]) ); - /** @var User $user */ + /** @var ?User $user */ $user = $this->query->whereEmail($this->email)->one(); if ($user === null) { @@ -55,10 +55,6 @@ public function run() $token = TokenFactory::makeRecoveryToken($user->id); - if (!$token) { - return false; - } - $this->mailService->setViewParam('user', $user); $this->mailService->setViewParam('token', $token); if (!$this->sendMail($user)) {