SignupForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 30.95 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 26
loc 84
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 18 18 1
A attributeLabels() 8 8 1
A signup() 0 14 3
A getUser() 0 4 1

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 yii2mod\user\models;
4
5
use Yii;
6
use yii\base\Model;
7
8
/**
9
 * Class SignupForm
10
 *
11
 * @package yii2mod\user\models
12
 */
13
class SignupForm extends Model
14
{
15
    /**
16
     * @var string username
17
     */
18
    public $username;
19
20
    /**
21
     * @var string email
22
     */
23
    public $email;
24
25
    /**
26
     * @var string password
27
     */
28
    public $password;
29
30
    /**
31
     * @var UserModel
32
     */
33
    protected $user;
34
35
    /**
36
     * @inheritdoc
37
     */
38 View Code Duplication
    public function rules()
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...
39
    {
40
        return [
41
            ['username', 'trim'],
42
            ['username', 'required'],
43
            ['username', 'unique', 'targetClass' => UserModel::class, 'message' => Yii::t('yii2mod.user', 'This username has already been taken.')],
44
            ['username', 'string', 'min' => 2, 'max' => 255],
45
46
            ['email', 'trim'],
47
            ['email', 'required'],
48
            ['email', 'email'],
49
            ['email', 'string', 'max' => 255],
50
            ['email', 'unique', 'targetClass' => UserModel::class, 'message' => Yii::t('yii2mod.user', 'This email address has already been taken.')],
51
52
            ['password', 'required'],
53
            ['password', 'string', 'min' => 6],
54
        ];
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 View Code Duplication
    public function attributeLabels()
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...
61
    {
62
        return [
63
            'username' => Yii::t('yii2mod.user', 'Username'),
64
            'email' => Yii::t('yii2mod.user', 'Email'),
65
            'password' => Yii::t('yii2mod.user', 'Password'),
66
        ];
67
    }
68
69
    /**
70
     * Signs user up.
71
     *
72
     * @return UserModel|null the saved model or null if saving fails
73
     */
74
    public function signup()
75
    {
76
        if (!$this->validate()) {
77
            return null;
78
        }
79
80
        $this->user = new UserModel();
81
        $this->user->setAttributes($this->attributes);
82
        $this->user->setPassword($this->password);
83
        $this->user->setLastLogin(time());
84
        $this->user->generateAuthKey();
85
86
        return $this->user->save() ? $this->user : null;
87
    }
88
89
    /**
90
     * @return UserModel|null
91
     */
92
    public function getUser()
93
    {
94
        return $this->user;
95
    }
96
}
97