Completed
Push — master ( 032082...7ec744 )
by zacksleo
36:12 queued 34:41
created

ResetPasswordForm::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace zacksleo\yii2\backend\models\forms;
3
4
use yii\base\Model;
5
use yii\base\InvalidParamException;
6
use zacksleo\yii2\backend\models\Admin;
7
8
/**
9
 * Password reset form
10
 */
11
class ResetPasswordForm extends Model
12
{
13
    public $password;
14
15
    /**
16
     * @var \zacksleo\yii2\backend\models\Admin
17
     */
18
    private $_user;
19
20
21
    /**
22
     * Creates a form model given a token.
23
     *
24
     * @param string $token
25
     * @param array $config name-value pairs that will be used to initialize the object properties
26
     * @throws \yii\base\InvalidParamException if token is empty or not valid
27
     */
28 2
    public function __construct($token, $config = [])
29
    {
30 2
        if (empty($token) || !is_string($token)) {
31 1
            throw new InvalidParamException('Password reset token cannot be blank.');
32
        }
33 2
        $this->_user = Admin::find()->canLogin()->passwordResetToken($token)->one();
0 ignored issues
show
Documentation Bug introduced by
It seems like \zacksleo\yii2\backend\m...setToken($token)->one() can also be of type object<yii\db\ActiveRecord> or array. However, the property $_user is declared as type object<zacksleo\yii2\backend\models\Admin>. 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...
34 2
        if (!$this->_user) {
35 1
            throw new InvalidParamException('Wrong password reset token.');
36
        }
37 2
        parent::__construct($config);
38 2
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 2
    public function rules()
44
    {
45
        return [
46 2
            ['password', 'required'],
47
            ['password', 'string', 'min' => 6],
48
        ];
49
    }
50
51
    /**
52
     * Resets password.
53
     *
54
     * @return bool if password was reset.
55
     */
56 2
    public function resetPassword()
57
    {
58 2
        return $this->_user->resetPassword($this->password);
59
    }
60
}
61