ResetPasswordForm::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace app\models\forms;
4
5
use app\models\UserModel;
6
use Yii;
7
use yii\base\Model;
8
use yii\web\IdentityInterface;
9
10
/**
11
 * Class ResetPasswordForm
12
 *
13
 * @package app\models\forms
14
 */
15
class ResetPasswordForm extends Model
16
{
17
    /**
18
     * @var string password
19
     */
20
    public $password;
21
22
    /**
23
     * @var string confirmPassword
24
     */
25
    public $confirmPassword;
26
27
    /**
28
     * @var UserModel
29
     */
30
    private $_user;
31
32
    /**
33
     * ResetPasswordForm constructor.
34
     *
35
     * @param IdentityInterface $user
36
     * @param array $config
37
     */
38
    public function __construct(IdentityInterface $user, array $config = [])
39
    {
40
        $this->_user = $user;
0 ignored issues
show
Documentation Bug introduced by
$user is of type object<yii\web\IdentityInterface>, but the property $_user was declared to be of type object<app\models\UserModel>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
41
42
        parent::__construct($config);
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function rules(): array
49
    {
50
        return [
51
            [['password', 'confirmPassword'], 'trim'],
52
            ['password', 'required'],
53
            ['confirmPassword', 'required'],
54
            [['password', 'confirmPassword'], 'string', 'min' => 6],
55
            ['confirmPassword', 'compare', 'compareAttribute' => 'password'],
56
        ];
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function attributeLabels(): array
63
    {
64
        return [
65
            'password' => Yii::t('app', 'New Password'),
66
            'confirmPassword' => Yii::t('app', 'Confirm New Password'),
67
        ];
68
    }
69
70
    /**
71
     * Resets password.
72
     *
73
     * @return bool if password was reset
74
     */
75
    public function resetPassword(): bool
76
    {
77
        if ($this->validate()) {
78
            $this->_user->setPassword($this->password);
79
80
            return $this->_user->save(true, ['password_hash']);
81
        }
82
83
        return false;
84
    }
85
}
86