Skip to content

Commit

Permalink
raise phpstan level = 4 and fix all related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TonisOrmisson committed Feb 27, 2024
1 parent b5c0b29 commit 904f8e0
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 53 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 3
level: 4
paths:
- src
excludePaths:
Expand Down
2 changes: 1 addition & 1 deletion src/User/Command/CreateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/User/Command/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/User/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/User/Controller/RecoveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
6 changes: 3 additions & 3 deletions src/User/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/User/Controller/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 5 additions & 15 deletions src/User/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions src/User/Controller/api/v1/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/User/Factory/MailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

Expand All @@ -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,
];

Expand All @@ -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,
];

Expand Down
12 changes: 4 additions & 8 deletions src/User/Helper/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/User/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion src/User/Service/EmailChangeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions src/User/Service/PasswordRecoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down

0 comments on commit 904f8e0

Please sign in to comment.