LoginForm::validatePassword()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 20
1
<?php
2
/**
3
 * HiSite Yii2 base project.
4
 *
5
 * @link      https://github.com/hiqdev/hisite
6
 * @package   hisite
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hisite\models;
12
13
use Yii;
14
use yii\base\Model;
15
16
/**
17
 * LoginForm is the model behind the login form.
18
 *
19
 * @property User|null $user This property is read-only
20
 */
21
class LoginForm extends Model
22
{
23
    public $username;
24
    public $password;
25
    public $rememberMe = true;
26
27
    private $_user = false;
28
29
    /**
30
     * @return array the validation rules
31
     */
32
    public function rules()
33
    {
34
        return [
35
            // username and password are both required
36
            [['username', 'password'], 'required'],
37
            // rememberMe must be a boolean value
38
            ['rememberMe', 'boolean'],
39
            // password is validated by validatePassword()
40
            ['password', 'validatePassword'],
41
        ];
42
    }
43
44
    /**
45
     * Validates the password.
46
     * This method serves as the inline validation for password.
47
     *
48
     * @param string $attribute the attribute currently being validated
49
     * @param array $params the additional name-value pairs given in the rule
50
     */
51
    public function validatePassword($attribute, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

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

Loading history...
52
    {
53
        if (!$this->hasErrors()) {
54
            $user = $this->getUser();
55
56
            if (!$user || !$user->validatePassword($this->password)) {
57
                $this->addError($attribute, 'Incorrect username or password.');
58
            }
59
        }
60
    }
61
62
    /**
63
     * Logs in a user using the provided username and password.
64
     * @return boolean whether the user is logged in successfully
65
     */
66
    public function login()
67
    {
68
        if ($this->validate()) {
69
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
70
        }
71
72
        return false;
73
    }
74
75
    /**
76
     * Finds user by [[username]].
77
     *
78
     * @return User|null
79
     */
80
    public function getUser()
81
    {
82
        if ($this->_user === false) {
83
            $this->_user = User::findByUsername($this->username);
0 ignored issues
show
Documentation Bug introduced by
It seems like \hisite\models\User::fin...ername($this->username) can also be of type object<hisite\models\User>. However, the property $_user is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
84
        }
85
86
        return $this->_user;
87
    }
88
}
89