ResetPasswordForm::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace yii2mod\user\models;
4
5
use Yii;
6
use yii\base\InvalidParamException;
7
use yii\base\Model;
8
9
/**
10
 * Class ResetPasswordForm
11
 *
12
 * @package yii2mod\user\models
13
 */
14
class ResetPasswordForm extends Model
15
{
16
    /**
17
     * @var string password
18
     */
19
    public $password;
20
21
    /**
22
     * @var UserModel
23
     */
24
    protected $user;
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
     *
32
     * @throws \yii\base\InvalidParamException if token is empty or not valid
33
     */
34
    public function __construct($token, $config = [])
35
    {
36
        if (empty($token) || !is_string($token)) {
37
            throw new InvalidParamException('Password reset token cannot be blank.');
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\InvalidParamException has been deprecated with message: since 2.0.14. Use [[InvalidArgumentException]] instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
38
        }
39
40
        $this->user = UserModel::findByPasswordResetToken($token);
0 ignored issues
show
Documentation Bug introduced by
It seems like \yii2mod\user\models\Use...swordResetToken($token) can also be of type object<yii\db\ActiveRecordInterface> or array. However, the property $user is declared as type object<yii2mod\user\models\UserModel>. 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...
41
42
        if (!$this->user) {
43
            throw new InvalidParamException('Wrong password reset token.');
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\InvalidParamException has been deprecated with message: since 2.0.14. Use [[InvalidArgumentException]] instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
44
        }
45
46
        parent::__construct($config);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function rules()
53
    {
54
        return [
55
            ['password', 'required'],
56
            ['password', 'string', 'min' => 6],
57
        ];
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function attributeLabels()
64
    {
65
        return [
66
            'password' => Yii::t('yii2mod.user', 'Password'),
67
        ];
68
    }
69
70
    /**
71
     * Resets password.
72
     *
73
     * @return bool if password was reset
74
     */
75
    public function resetPassword()
76
    {
77
        $user = $this->user;
78
        $user->setPassword($this->password);
79
        $user->removePasswordResetToken();
80
81
        return $user->save();
82
    }
83
}
84