Completed
Push — master ( 5e7707...6e3d76 )
by Igor
03:56
created

ResetPasswordForm::validateToken()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 3
nop 1
crap 30
1
<?php
2
3
namespace app\models\forms;
4
5
use Yii;
6
use app\models\User;
7
use app\services\Tokenizer;
8
9
class ResetPasswordForm extends \yii\base\Model
10
{
11
    /**
12
     * @var string
13
     */
14
    public $password;
15
    /**
16
     * @var \app\models\User
17
     */
18
    private $user;
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function rules()
24
    {
25
        return [
26
            ['password', 'required'],
27
            ['password', 'string', 'min' => 6],
28
        ];
29
    }
30
31
    /**
32
     * @inheritdoc
33
     * @codeCoverageIgnore
34
     */
35
    public function attributeLabels()
36
    {
37
        return (new User())->attributeLabels();
38
    }
39
40
    /**
41
     * Validate token
42
     *
43
     * @param string $token
44
     * @return boolean
45
     */
46
    public function validateToken(string $token): bool
47
    {
48
        $tokenizer = new Tokenizer();
49
        if (empty($token) || !is_string($token) || !$tokenizer->validate($token)) {
50
            return false;
51
        }
52
53
        $this->user = User::find()->passwordResetToken($token)->one();
0 ignored issues
show
Documentation Bug introduced by
It seems like \app\models\User::find()...setToken($token)->one() can also be of type object<yii\db\ActiveRecord> or array. However, the property $user is declared as type object<app\models\User>. 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...
54
55
        if (!$this->user) {
56
            return false;
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * Resets password
64
     *
65
     * @return boolean
66
     */
67
    public function resetPassword(): bool
68
    {
69
        $this->user->setPassword($this->password);
70
        $this->user->removePasswordResetToken();
71
        $this->user->updateDateLogin();
72
73
        Yii::$app->user->login($this->user, 3600 * 24 * 30);
74
75
        return $this->user->save(false);
76
    }
77
}
78