ChangePasswordForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 60
rs 10
ccs 0
cts 32
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 7 1
A rules() 0 11 2
A changePassword() 0 11 2
A reset() 0 4 1
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