Issues (1319)

models/LoginForm.php (4 issues)

1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\helpers\ArrayHelper;
8
use app\behaviors\LoginAttemptBehavior;
9
10
/**
11
 * Class LoginForm
12
 *
13
 * @package app\models
14
 */
15
class LoginForm extends Model
16
{
17
    /**
18
     * Login to go in to system.
19
     *
20
     * @var string
21
     */
22
    public $login;
23
24
    /**
25
     * Password to go in to system.
26
     *
27
     * @var string
28
     */
29
    public $password;
30
31
    /**
32
     * Use cookie to remember user in browser.
33
     *
34
     * @var bool
35
     */
36
    public $rememberMe = true;
37
38
    /**
39
     * User model.
40
     *
41
     * @var null
42
     */
43
    private $_user = null;
44
45
46
    /**
47
     * @return array the validation rules.
48
     */
49
    public function rules()
50
    {
51
        return [
52
            [
53
                [
54
                    'login',
55
                    'password',
56
                ],
57
                'required',
58
            ],
59
            [
60
                'rememberMe',
61
                'boolean',
62
            ],
63
            [
64
                'password',
65
                'validatePassword',
66
            ],
67
        ];
68
    }
69
70
    public function behaviors()
71
    {
72
        return ArrayHelper::merge(parent::behaviors(), [
73
74
                'attempts' => [
75
76
                    'class' => LoginAttemptBehavior::class,
77
78
                    // Amount of attempts in the given time period
79
                    'attempts' => 3,
80
                ]
81
            ]
82
        );
83
    }
84
85
    /**
86
     * Validates the password.
87
     * This method serves as the inline validation for password.
88
     *
89
     * @param string $attribute the attribute currently being validated
90
     * @param array $params the additional name-value pairs given in the rule
91
     */
92
    public function validatePassword($attribute, $params)
0 ignored issues
show
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
    public function validatePassword($attribute, /** @scrutinizer ignore-unused */ $params)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        if (!$this->hasErrors()) {
95
            $user = $this->getUser();
96
97
            if (null === $user || !$user->validatePassword($this->password)) {
98
                $this->addError($attribute, 'Incorrect username or password.');
99
            }
100
        }
101
    }
102
103
    /**
104
     * Logs in a user using the provided username and password.
105
     *
106
     * @return bool whether the user is logged in successfully
107
     */
108
    public function login()
109
    {
110
        if (!$this->validate()) {
111
            return false;
112
        }
113
114
        return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
0 ignored issues
show
It seems like $this->getUser() can also be of type null; however, parameter $identity of yii\web\User::login() does only seem to accept yii\web\IdentityInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
        return Yii::$app->user->login(/** @scrutinizer ignore-type */ $this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
Loading history...
The method login() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        return Yii::$app->user->/** @scrutinizer ignore-call */ login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
    }
116
117
    /**
118
     * Finds user by [[username]]
119
     *
120
     * @return User|null
121
     */
122
    public function getUser()
123
    {
124
        if ($this->_user === null) {
125
            $this->_user = User::findByLogin($this->login);
0 ignored issues
show
Documentation Bug introduced by
It seems like app\models\User::findByLogin($this->login) of type app\models\User is incompatible with the declared type null of property $_user.

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..

Loading history...
126
        }
127
128
        return $this->_user;
129
    }
130
}
131