Completed
Push — master ( 7daf33...4afa09 )
by Alexey
39:01 queued 24:02
created

ResetPasswordForm::resetPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 2
b 1
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace modules\users\models;
4
5
use yii\base\Model;
6
use yii\base\InvalidParamException;
7
use modules\users\Module;
8
9
/**
10
 * Class ResetPasswordForm
11
 * @package modules\users\models\frontend
12
 */
13
class ResetPasswordForm extends Model
14
{
15
    /**
16
     * @var string
17
     */
18
    public $password;
19
20
    /**
21
     * @var \modules\users\models\BaseUser
22
     */
23
    private $_user;
24
25
26
    /**
27
     * Creates a form model given a token.
28
     *
29
     * @param string $token
30
     * @param array $config name-value pairs that will be used to initialize the object properties
31
     * @throws \yii\base\InvalidParamException if token is empty or not valid
32
     */
33
    public function __construct($token = '', $config = [])
34
    {
35
        if (empty($token) || !is_string($token)) {
36
            throw new InvalidParamException(Module::t('module', 'Password reset token cannot be blank.'));
37
        }
38
        $this->_user = BaseUser::findByPasswordResetToken($token);
0 ignored issues
show
Documentation Bug introduced by
It seems like \modules\users\models\Ba...swordResetToken($token) can also be of type object<yii\db\ActiveRecordInterface> or array. However, the property $_user is declared as type object<modules\users\models\BaseUser>. 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...
39
        if (!$this->_user) {
40
            throw new InvalidParamException(Module::t('module', 'Wrong password reset token.'));
41
        }
42
        parent::__construct($config);
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function rules()
49
    {
50
        return [
51
            ['password', 'required'],
52
            ['password', 'string', 'min' => User::LENGTH_STRING_PASSWORD_MIN, 'max' => User::LENGTH_STRING_PASSWORD_MAX],
53
        ];
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function attributeLabels()
60
    {
61
        return [
62
            'password' => Module::t('module', 'New Password'),
63
        ];
64
    }
65
66
    /**
67
     * Resets password.
68
     *
69
     * @return boolean if password was reset.
70
     */
71
    public function resetPassword()
72
    {
73
        $user = $this->_user;
74
        $user->setPassword($this->password);
75
        $user->removePasswordResetToken();
76
        return $user->save(false);
77
    }
78
}
79