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 |
||
30 | class User extends ActiveRecord implements IdentityInterface |
||
31 | { |
||
32 | /** Active user status */ |
||
33 | const STATUS_ACTIVE = 'active'; |
||
34 | |||
35 | /** Blocked user status */ |
||
36 | const STATUS_BLOCKED = 'blocked'; |
||
37 | |||
38 | /** Created user status */ |
||
39 | const STATUS_CREATED = 'created'; |
||
40 | |||
41 | /** Role user */ |
||
42 | const ROLE_USER = 'user'; |
||
43 | |||
44 | /** Role admin */ |
||
45 | const ROLE_ADMIN = 'admin'; |
||
46 | |||
47 | const DEFAULT_AVATAR_URL = '/img/no_image.png'; |
||
48 | |||
49 | public $rememberMe = true; |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | 52 | public static function tableName() |
|
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | 7 | public function rules() |
|
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | 10 | public function attributeLabels() |
|
92 | |||
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | 54 | View Code Duplication | public function behaviors() |
|
|||
97 | { |
||
98 | return [ |
||
99 | [ |
||
100 | 54 | 'class' => TimestampBehavior::className(), |
|
101 | 'attributes' => [ |
||
102 | 54 | ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'], |
|
103 | ], |
||
104 | 54 | 'value' => date('Y-m-d H:i:s'), |
|
105 | ], |
||
106 | ]; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @inheritdoc |
||
111 | */ |
||
112 | 44 | public static function findIdentity($id) |
|
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | public static function findIdentityByAccessToken($token, $type = null) |
||
124 | |||
125 | /** |
||
126 | * @param $email |
||
127 | * @return static |
||
128 | */ |
||
129 | 2 | public static function findByEmail($email) |
|
130 | { |
||
131 | 2 | return static::findOne(['email' => $email]); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | 46 | public function getId() |
|
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | 2 | public function getAuthKey() |
|
149 | |||
150 | /** |
||
151 | * @inheritdoc |
||
152 | */ |
||
153 | public function validateAuthKey($authKey) |
||
157 | |||
158 | /** |
||
159 | * Validates password |
||
160 | * |
||
161 | * @param string $password password to validate |
||
162 | * @return bool if password provided is valid for current user |
||
163 | */ |
||
164 | 1 | public function validatePassword($password) |
|
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | */ |
||
172 | 1 | public function hasEmptyPassword() |
|
176 | |||
177 | /** |
||
178 | * Logs in a user using the provided username and password. |
||
179 | * |
||
180 | * @return boolean whether the user is logged in successfully |
||
181 | * @throws Exception |
||
182 | */ |
||
183 | 2 | public function login() |
|
184 | { |
||
185 | 2 | if (!Yii::$app->user->login($this, $this->rememberMe ? 3600 * 24 * 7 : 0)) { |
|
186 | throw new Exception('User could not be logged in.'); |
||
187 | } |
||
188 | 2 | $this->last_login_at = date('Y-m-d H:i:s'); |
|
189 | 2 | $this->update(); |
|
190 | 2 | } |
|
191 | |||
192 | /** |
||
193 | * @param $userData |
||
194 | * @return User |
||
195 | */ |
||
196 | 2 | public function create($userData) |
|
197 | { |
||
198 | 2 | $this->first_name = $userData->firstName; |
|
199 | 2 | $this->last_name = $userData->lastName; |
|
200 | 2 | $this->email = $userData->email; |
|
201 | 2 | $this->password = Yii::$app->security->generatePasswordHash($userData->password); |
|
202 | 2 | $this->auth_key = Yii::$app->security->generateRandomString(); |
|
203 | 2 | $this->avatar = self::DEFAULT_AVATAR_URL; |
|
204 | |||
205 | 2 | $this->save(); |
|
206 | 2 | $this->setRole(self::ROLE_USER); |
|
207 | 2 | return $this; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Set new role |
||
212 | * |
||
213 | * @param $role |
||
214 | */ |
||
215 | 3 | public function setRole($role) |
|
216 | { |
||
217 | 3 | $auth = Yii::$app->authManager; |
|
218 | 3 | $auth->revokeAll($this->id); |
|
219 | 3 | $userRole = $auth->getRole($role); |
|
220 | 3 | $auth->assign($userRole, $this->getId()); |
|
221 | 3 | } |
|
222 | |||
223 | /** |
||
224 | * Return role for user |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 12 | public function getRoleName() |
|
229 | { |
||
230 | 12 | $auth = Yii::$app->authManager; |
|
231 | 12 | $userRole = $auth->getRolesByUser($this->id); |
|
232 | 12 | return !empty($userRole) ? array_shift($userRole)->name : ''; |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * @param $hash |
||
237 | * @return static |
||
238 | * @throws ServerErrorHttpException |
||
239 | */ |
||
240 | 10 | public static function findByHash($hash) |
|
241 | { |
||
242 | 10 | if (!$hash = Hash::findOne(['hash' => $hash])) { |
|
243 | throw new ServerErrorHttpException('The server encountered an internal error and could not complete your request.'); |
||
244 | } |
||
245 | 10 | return static::findOne($hash->user_id); |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param $socialId |
||
250 | * @return static |
||
251 | */ |
||
252 | public static function findBySocialId($socialId) |
||
253 | { |
||
254 | return static::findOne(['social_id' => $socialId]); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param $userAttributes |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function saveSocialAccountInfo($userAttributes) |
||
262 | { |
||
263 | if ($this->isNewRecord) { |
||
264 | $this->first_name = ArrayHelper::getValue($userAttributes, 'firstName'); |
||
265 | $this->last_name = ArrayHelper::getValue($userAttributes, 'lastName'); |
||
266 | $this->email = ArrayHelper::getValue($userAttributes, 'email'); |
||
267 | $this->avatar = ArrayHelper::getValue($userAttributes, 'avatar'); |
||
268 | $this->status = self::STATUS_ACTIVE; |
||
269 | } |
||
270 | $this->social_id = ArrayHelper::getValue($userAttributes, 'socialId'); |
||
271 | $this->auth_provider = ArrayHelper::getValue($userAttributes, 'authProvider'); |
||
272 | |||
273 | return $this->save(); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param $form |
||
278 | * @return bool |
||
279 | */ |
||
280 | 10 | public function changePassword($form) |
|
290 | } |
||
291 |
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.