LoginForm::rules()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
rs 8.8571
ccs 12
cts 12
cp 1
cc 6
eloc 12
nc 1
nop 0
crap 6
1
<?php
2
3
namespace inblank\activeuser\models\forms;
4
5
use inblank\activeuser\traits\CommonTrait;
6
use yii;
7
use yii\base\Model;
8
use yii\base\Security;
9
10
class LoginForm extends Model
11
{
12
    use CommonTrait;
13
14
    /**
15
     * @var string user email for login
16
     */
17
    public $email;
18
    /**
19
     * @var string user password for login in scenario SCENARIO_LOGIN
20
     */
21
    public $password;
22
    /**
23
     * @var bool remember me check in scenario SCENARIO_LOGIN
24
     */
25
    public $remember;
26
27
    /**
28
     * Founded user
29
     * @var \inblank\activeuser\models\User
30
     */
31
    protected $user = false;
32
33
    /** @inheritdoc */
34 1
    public function attributeLabels()
35
    {
36
        return [
37 1
            'email' => Yii::t('activeuser_general', 'Email'),
38 1
            'password' => Yii::t('activeuser_general', 'Password'),
39 1
            'remember' => Yii::t('activeuser_general', 'Remember me next time'),
40 1
            'hash' => Yii::t('activeuser_general', 'Special hash'),
41
        ];
42
    }
43
44
    /** @inheritdoc */
45 1
    public function rules()
46
    {
47
        return [
48 1
            [['email', 'password'], 'required'],
49
            ['password', function () {
50 1
                if ($this->getUser() === null || !(new Security())->validatePassword($this->password, $this->getUser()->pass_hash)) {
51 1
                    $this->addError('password', Yii::t('activeuser_general', 'Invalid email or password'));
52
                }
53 1
            }],
54 1
            ['email', function () {
55 1
                if ($this->getUser() !== null) {
56 1
                    if (!$this->getUser()->isConfirmed()) {
57 1
                        $this->addError('password', Yii::t('activeuser_general', 'You need to confirm your email address'));
58 1
                    } elseif ($this->getUser()->isBlocked()) {
59 1
                        $this->addError('password', Yii::t('activeuser_general', 'Your account has been blocked'));
60
                    }
61
                }
62 1
            }],
63
        ];
64
    }
65
66
    /**
67
     * Validates form and logs the user in.
68
     * @return bool whether the user is logged in successfully
69
     */
70 1
    public function login()
71
    {
72 1
        if ($this->validate()) {
73 1
            return Yii::$app->getUser()->login($this->getUser(), $this->remember ? $this->module->rememberTime : 0);
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
74
        }
75 1
        return false;
76
    }
77
78 1
    protected function getUser()
79
    {
80 1
        if ($this->user === false) {
81 1
            $userClass = self::di('User');
82 1
            $this->user = empty($this->email) ? null : $userClass::findOne(['email' => $this->email]);
83
        }
84 1
        return $this->user;
85
    }
86
}
87