LoginForm::validatePassword()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\base\Model;
7
8
/**
9
 * LoginForm is the model behind the login form.
10
 *
11
 * @property-read User|null $user
12
 *
13
 */
14
class LoginForm extends Model
15
{
16
    public $username;
17
    public $password;
18
    public $rememberMe = true;
19
20
    private $_user = false;
21
22
23
    /**
24
     * @return array the validation rules.
25
     */
26
    public function rules()
27
    {
28
        return [
29
            // username and password are both required
30
            [['username', 'password'], 'required'],
31
            // rememberMe must be a boolean value
32
            ['rememberMe', 'boolean'],
33
            // password is validated by validatePassword()
34
            ['password', 'validatePassword'],
35
        ];
36
    }
37
38
    /**
39
     * Validates the password.
40
     * This method serves as the inline validation for password.
41
     *
42
     * @param string $attribute the attribute currently being validated
43
     * @param array $params the additional name-value pairs given in the rule
44
     */
45
    public function validatePassword($attribute, $params)
0 ignored issues
show
Unused Code introduced by
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

45
    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...
46
    {
47
        if (!$this->hasErrors()) {
48
            $user = $this->getUser();
49
50
            if (!$user || !$user->validatePassword($this->password)) {
0 ignored issues
show
introduced by
$user is of type app\models\User, thus it always evaluated to true.
Loading history...
51
                $this->addError($attribute, 'Incorrect username or password.');
52
            }
53
        }
54
    }
55
56
    /**
57
     * Logs in a user using the provided username and password.
58
     * @return bool whether the user is logged in successfully
59
     */
60
    public function login()
61
    {
62
        if ($this->validate()) {
63
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
0 ignored issues
show
Bug introduced by
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

63
            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...
Bug introduced by
The method login() does not exist on __WebUser. ( Ignorable by Annotation )

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

63
            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...
64
        }
65
        return false;
66
    }
67
68
    /**
69
     * Finds user by [[username]]
70
     *
71
     * @return User|null
72
     */
73
    public function getUser()
74
    {
75
        if ($this->_user === false) {
76
            $this->_user = User::findByUsername($this->username);
77
        }
78
79
        return $this->_user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_user also could return the type true which is incompatible with the documented return type app\models\User|null.
Loading history...
80
    }
81
}
82