LoginForm   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 3
dl 0
loc 117
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 8 1
A attributeHints() 0 6 1
A init() 0 6 2
A rules() 0 9 1
A validateId() 0 9 3
A validatePassword() 0 10 4
A login() 0 7 3
A getUser() 0 12 4
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\forms;
14
15
use rhosocial\user\User;
16
use Yii;
17
use yii\base\Model;
18
19
/**
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
class LoginForm extends Model
24
{
25
    public $id;
26
    public $password;
27
    public $rememberMe = true;
28
    public $userClass;
29
    private $_user = false;
30
31
    /**
32
     * @return array
33
     */
34
    public function attributeLabels()
35
    {
36
        return [
37
            'id' => Yii::t('user', 'ID'),
38
            'password' => Yii::t('user', 'Password'),
39
            'rememberMe' => Yii::t('user', 'Remember Me'),
40
        ];
41
    }
42
43
    /**
44
     * @return array
45
     */
46
    public function attributeHints()
47
    {
48
        return [
49
            'rememberMe' => Yii::t('user', 'When this option is checked, each login is valid for 30 days.'),
50
        ];
51
    }
52
53
    /**
54
     *
55
     */
56
    public function init()
57
    {
58
        if (empty($this->userClass)) {
59
            $this->userClass = Yii::$app->user->identityClass;
60
        }
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function rules()
67
    {
68
        return [
69
            [['id', 'password'], 'required'],
70
            ['id', 'validateId'],
71
            ['password', 'validatePassword'],
72
            ['rememberMe', 'boolean'],
73
        ];
74
    }
75
76
    /**
77
     * Validates the ID.
78
     *
79
     * @param $attribute
80
     * @param $params
81
     */
82
    public function validateId($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...
83
    {
84
        foreach (Yii::$app->user->getLoginPriority() as $item) {
85
            if ($item::validate($this->$attribute)) {
86
                return;
87
            }
88
        }
89
        $this->addError($attribute, Yii::t('user', 'Incorrect ID.'));
90
    }
91
92
    /**
93
     * Validates the password.
94
     * This method serves as the inline validation for password.
95
     *
96
     * @param string $attribute the attribute currently being validated
97
     * @param array $params the additional name-value pairs given in the rule
98
     */
99
    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...
100
    {
101
        if (!$this->hasErrors()) {
102
            $user = $this->getUser();
103
104
            if (!$user || !$user->validatePassword($this->password)) {
105
                $this->addError($attribute, Yii::t('user', 'Incorrect ID or password.'));
106
            }
107
        }
108
    }
109
110
    /**
111
     * Logs in a user using the provided id and password.
112
     * @return bool whether the user is logged in successfully
113
     */
114
    public function login()
115
    {
116
        if ($this->validate()) {
117
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
118
        }
119
        return false;
120
    }
121
122
    /**
123
     * Finds user by [[id]]
124
     *
125
     * @return User|null
126
     */
127
    public function getUser()
128
    {
129
        if ($this->_user === false) {
130
            foreach (Yii::$app->user->getLoginPriority() as $item) {
131
                if ($item::validate($this->id)) {
132
                    return $item::getUser($this->id);
133
                }
134
            }
135
        }
136
137
        return $this->_user;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_user; (boolean) is incompatible with the return type documented by rhosocial\user\forms\LoginForm::getUser of type rhosocial\user\User|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
138
    }
139
}
140