RestoreForm   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 25
loc 75
c 0
b 0
f 0
rs 10
ccs 0
cts 39
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 7 1
A rules() 0 10 1
A restore() 12 12 3
A changePassword() 13 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace inblank\activeuser\models\forms;
4
5
use inblank\activeuser\traits\CommonTrait;
6
use yii;
7
use yii\base\Model;
8
9
class RestoreForm extends Model
10
{
11
    use CommonTrait;
12
13
    const SCENARIO_PASSWORD = 'password';
14
    const SCENARIO_EMAIL = 'email';
15
16
    /**
17
     * @var string user email for restore password
18
     */
19
    public $email;
20
    /**
21
     * @var string new password
22
     */
23
    public $password;
24
25
    /** @inheritdoc */
26
    public function attributeLabels()
27
    {
28
        return [
29
            'email' => Yii::t('activeuser_general', 'Email'),
30
            'password' => Yii::t('activeuser_general', 'Password'),
31
        ];
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function rules()
38
    {
39
        return [
40
            ['email', 'required', 'on' => self::SCENARIO_EMAIL],
41
            ['email', 'email', 'skipOnEmpty' => true],
42
43
            ['password', 'required', 'on' => self::SCENARIO_PASSWORD],
44
            ['password', 'string', 'length' => [6, 20], 'skipOnEmpty' => true],
45
        ];
46
    }
47
48
    /**
49
     * Restore
50
     * @return bool
51
     * @throws yii\base\InvalidConfigException
52
     */
53 View Code Duplication
    public function restore()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        if (!$this->validate()) {
56
            return false;
57
        }
58
        /** @var \inblank\activeuser\models\User $user */
59
        $user = Yii::createObject(self::di('User'))->findOne(['email' => $this->email]);
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
        if ($user) {
61
            $user->restore();
62
        }
63
        return true;
64
    }
65
    /**
66
     * Change password
67
     * @return bool
68
     * @throws yii\base\InvalidConfigException
69
     */
70 View Code Duplication
    public function changePassword()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        if (!$this->validate()) {
73
            return false;
74
        }
75
        /** @var \inblank\activeuser\models\User $user */
76
        $user = Yii::createObject(self::di('User'))->findOne(['email' => $this->email]);
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        if ($user) {
78
            $user->password = $this->password;
79
            $user->newPassword();
80
        }
81
        return true;
82
    }
83
}
84