RegForm::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 72
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 43
nc 1
nop 0
dl 0
loc 72
c 0
b 0
f 0
cc 1
rs 9.232

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace app\models;
4
5
use yii\base\Model;
6
use app\helpers\InitialUserSettings;
7
8
/**
9
 * Class RegForm
10
 *
11
 * @package app\models
12
 */
13
class RegForm extends Model
14
{
15
    /**
16
     * Code for captcha.
17
     *
18
     * @var string
19
     */
20
    public $verifyCode;
21
22
    /**
23
     * Name of user.
24
     *
25
     * @var string
26
     */
27
    public $first_name;
28
29
    /**
30
     * Login to go in to system.
31
     *
32
     * @var string
33
     */
34
    public $login;
35
36
    /**
37
     * Email.
38
     *
39
     * @var string
40
     */
41
    public $email;
42
43
    /**
44
     * Password to go in to system.
45
     *
46
     * @var string
47
     */
48
    public $password;
49
50
    /**
51
     * Repeat password to go in to system.
52
     *
53
     * @var string
54
     */
55
    public $passwordRepeat;
56
57
    /**
58
     * User model.
59
     *
60
     * @var null
61
     */
62
    private $_user = null;
63
64
    /**
65
     * Validate rules.
66
     *
67
     * @return array
68
     */
69
    public function rules()
70
    {
71
        return [
72
            [
73
                [
74
                    'first_name',
75
                    'login',
76
                    'email',
77
                    'password',
78
                    'passwordRepeat',
79
                ],
80
                'filter',
81
                'filter' => 'trim',
82
            ],
83
            [
84
                [
85
                    'first_name',
86
                    'login',
87
                    'email',
88
                    'password',
89
                    'passwordRepeat',
90
                ],
91
                'required',
92
            ],
93
            [
94
                [
95
                    'first_name',
96
                    'login'
97
                ],
98
                'string',
99
                'min' => 2,
100
                'max' => 255,
101
            ],
102
            [
103
                [
104
                    'password',
105
                    'passwordRepeat',
106
                ],
107
                'string',
108
                'min' => 5,
109
                'max' => 255,
110
            ],
111
            [
112
                'login',
113
                'unique',
114
                'targetClass' => User::class,
115
                'message' => 'This login already exists.',
116
            ],
117
            [
118
                'email',
119
                'email',
120
            ],
121
            [
122
                'email',
123
                'unique',
124
                'targetClass' => User::class,
125
                'message' => 'This email already exists.',
126
            ],
127
            [
128
                'password',
129
                'compare',
130
                'compareAttribute' => 'passwordRepeat',
131
            ],
132
            [
133
                'passwordRepeat',
134
                'compare',
135
                'compareAttribute' => 'password',
136
            ],
137
            [
138
                'verifyCode',
139
                'captcha',
140
                'captchaAction' => 'site/captcha'
141
            ],
142
        ];
143
    }
144
145
    /**
146
     * Attribute labels.
147
     *
148
     * @return array
149
     */
150
    public function attributeLabels(){
151
152
        return[
153
            'first_name' => 'First name',
154
            'login' => 'Login',
155
            'email' => 'Email',
156
            'password' => 'Password',
157
            'passwordRepeat' => 'Password repeat',
158
            'verifyCode' => 'Verification Code',
159
        ];
160
    }
161
162
    /**
163
     * Register user.
164
     *
165
     * @return bool
166
     */
167
    public function reg()
168
    {
169
        if (!$this->validate()) {
170
            return false;
171
        }
172
173
        $this->_user = new User();
0 ignored issues
show
Documentation Bug introduced by
It seems like new app\models\User() of type app\models\User is incompatible with the declared type null of property $_user.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
174
175
        $this->_user->first_name = $this->first_name;
176
        $this->_user->login = $this->login;
177
        $this->_user->email = $this->email;
178
179
        $this->_user->setPassword($this->password);
180
181
        if (!$this->_user->save()) {
182
            return false;
183
        }
184
185
        InitialUserSettings::run($this->_user);
186
187
        return true;
188
    }
189
190
    /**
191
     * Returns user record.
192
     *
193
     * @return null|User
194
     */
195
    public function getUser()
196
    {
197
        return $this->_user;
198
    }
199
}
200