Completed
Push — master ( 30de12...6a52a2 )
by Pavel
02:01
created

ChangePasswordForm::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
ccs 0
cts 7
cp 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace inblank\activeuser\models\forms;
4
5
use inblank\activeuser\models\User;
6
use inblank\activeuser\traits\CommonTrait;
7
use yii;
8
use yii\base\Model;
9
use yii\base\Security;
10
11
class ChangePasswordForm extends Model
12
{
13
    use CommonTrait;
14
15
    /**
16
     * @var string old user password
17
     */
18
    public $oldPassword;
19
20
    /**
21
     * @var string new user password
22
     */
23
    public $newPassword;
24
25
    /** @inheritdoc */
26
    public function attributeLabels()
27
    {
28
        return [
29
            'oldPassword' => Yii::t('activeuser_frontend', 'Old Password'),
30
            'newPassword' => Yii::t('activeuser_frontend', 'New Password'),
31
        ];
32
    }
33
34
    /** @inheritdoc */
35
    public function rules()
36
    {
37
        return [
38
            [['oldPassword', 'newPassword',], 'required'],
39
            ['newPassword', function () {
40
                if (!(new Security())->validatePassword($this->oldPassword, Yii::$app->getUser()->identity->pass_hash)) {
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...
41
                    $this->addError('oldPassword', Yii::t('activeuser_frontend', 'Invalid old password'));
42
                }
43
            }],
44
        ];
45
    }
46
47
    /**
48
     * Validates form and logs the user in.
49
     * @return bool whether the user is logged in successfully
50
     */
51
    public function changePassword()
52
    {
53
        if (!$this->validate()) {
54
            return false;
55
        }
56
        /** @var User $user */
57
        $user = Yii::$app->getUser()->identity;
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...
58
        $user->password = $this->newPassword;
59
        $user->newPassword(false);
60
        return true;
61
    }
62
63
    /**
64
     * Reset from fields
65
     */
66
    public function reset()
67
    {
68
        $this->oldPassword = $this->newPassword = null;
69
    }
70
}
71