LoginForm::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Cjt Terabyte LLC [yii2-extension].
5
 *
6
 * (c) Cjt Terabyte LLC [yii2-extension] <http://github.com/cjtterabytesoft>.
7
 * For the full copyright and license information, please view the LICENSE.md.
8
 * file that was distributed with this source code.
9
 *
10
 * @link http://www.cjtterabyte.com.
11
 * @author Wilmer Arámbula <[email protected]>.
12
 * @copyright (c) 2015 Cjt Terabyte LLC.
13
 * @Extension: [yii2-adminlte-basic].
14
 * @Models App [LoginForm].
15
 * @since 1.0
16
 */
17
18
namespace cjtterabytesoft\adminlte\basic\models;
19
20
use Yii;
21
use yii\base\Model;
22
23
class LoginForm extends Model
24
{
25
    public $username;
26
    public $password;
27
    public $rememberMe = true;
28
29
    private $_user = false;
30
31
    /**
32
     * @return array the validation rules.
33
     */
34
    public function rules()
35
    {
36
        return [
37
            // username and password are both required
38
            [['username', 'password'], 'required'],
39
            // rememberMe must be a boolean value
40
            ['rememberMe', 'boolean'],
41
            // password is validated by validatePassword()
42
            ['password', 'validatePassword'],
43
        ];
44
    }
45
46
    /** @inheritdoc */
47
    public function attributeLabels()
48
    {
49
        return [
50
            'username'   => Yii::t('adminlte', 'Username'),
51
            'password'   => Yii::t('adminlte', 'Password'),
52
            'rememberMe' => Yii::t('adminlte', 'Remember'),
53
        ];
54
    }
55
56
    /**
57
     * Validates the password.
58
     * This method serves as the inline validation for password.
59
     *
60
     * @param string $attribute the attribute currently being validated
61
     * @param array $params the additional name-value pairs given in the rule
62
     */
63
    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...
64
    {
65
        if (!$this->hasErrors()) {
66
            $user = $this->getUser();
67
68
            if (!$user || !$user->validatePassword($this->password)) {
69
                $this->addError($attribute, yii::t('adminlte', 'Incorrect username or password.'));
70
            }
71
        }
72
    }
73
74
    /**
75
     * Logs in a user using the provided username and password.
76
     * @return boolean whether the user is logged in successfully
77
     */
78
    public function login()
79
    {
80
        if ($this->validate()) {
81
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
82
        }
83
        return false;
84
    }
85
86
    /**
87
     * Finds user by [[username]]
88
     *
89
     * @return User|null
90
     */
91
    public function getUser()
92
    {
93
        if ($this->_user === false) {
94
            $this->_user = User::findByUsername($this->username);
0 ignored issues
show
Documentation Bug introduced by
It seems like \cjtterabytesoft\adminlt...ername($this->username) can also be of type object<cjtterabytesoft\a...nlte\basic\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...
95
        }
96
97
        return $this->_user;
98
    }
99
}