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 |
||
29 | class UserModel extends ActiveRecord implements IdentityInterface |
||
30 | { |
||
31 | use EventTrait; |
||
32 | |||
33 | /** |
||
34 | * Event is triggered before creating a user. |
||
35 | * Triggered with \yii2mod\user\events\CreateUserEvent. |
||
36 | */ |
||
37 | const BEFORE_CREATE = 'beforeCreate'; |
||
38 | |||
39 | /** |
||
40 | * Event is triggered after creating a user. |
||
41 | * Triggered with \yii2mod\user\events\CreateUserEvent. |
||
42 | */ |
||
43 | const AFTER_CREATE = 'afterCreate'; |
||
44 | |||
45 | /** |
||
46 | * @var string plain password |
||
47 | */ |
||
48 | public $plainPassword; |
||
49 | |||
50 | /** |
||
51 | * @inheritdoc |
||
52 | */ |
||
53 | public static function tableName() |
||
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | */ |
||
61 | View Code Duplication | public function rules() |
|
76 | |||
77 | /** |
||
78 | * @inheritdoc |
||
79 | */ |
||
80 | public function attributeLabels() |
||
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | */ |
||
95 | public function behaviors() |
||
101 | |||
102 | /** |
||
103 | * Create user |
||
104 | * |
||
105 | * @return null|UserModel the saved model or null if saving fails |
||
106 | * |
||
107 | * @throws \Exception |
||
108 | */ |
||
109 | public function create() |
||
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | public static function findIdentity($id) |
||
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | public static function findIdentityByAccessToken($token, $type = null) |
||
153 | |||
154 | /** |
||
155 | * Finds user (with active status) by username |
||
156 | * |
||
157 | * @param string $username |
||
158 | * |
||
159 | * @return static|null |
||
160 | */ |
||
161 | public static function findByUsername($username) |
||
165 | |||
166 | /** |
||
167 | * Finds user by email |
||
168 | * |
||
169 | * @param $email |
||
170 | * |
||
171 | * @return null|static |
||
172 | */ |
||
173 | public static function findByEmail($email) |
||
177 | |||
178 | /** |
||
179 | * Finds user by password reset token |
||
180 | * |
||
181 | * @param string $token password reset token |
||
182 | * |
||
183 | * @return static|null |
||
184 | */ |
||
185 | public static function findByPasswordResetToken($token) |
||
196 | |||
197 | /** |
||
198 | * Finds out if password reset token is valid |
||
199 | * |
||
200 | * @param string $token password reset token |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public static function isPasswordResetTokenValid($token) |
||
215 | |||
216 | /** |
||
217 | * @inheritdoc |
||
218 | */ |
||
219 | public function getId() |
||
223 | |||
224 | /** |
||
225 | * @inheritdoc |
||
226 | */ |
||
227 | public function getAuthKey() |
||
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | public function validateAuthKey($authKey) |
||
239 | |||
240 | /** |
||
241 | * Validates password |
||
242 | * |
||
243 | * @param string $password password to validate |
||
244 | * |
||
245 | * @return bool if password provided is valid for current user |
||
246 | */ |
||
247 | public function validatePassword($password) |
||
251 | |||
252 | /** |
||
253 | * Generates password hash from password and sets it to the model |
||
254 | * |
||
255 | * @param string $password |
||
256 | */ |
||
257 | public function setPassword($password) |
||
261 | |||
262 | /** |
||
263 | * Generates "remember me" authentication key |
||
264 | */ |
||
265 | public function generateAuthKey() |
||
269 | |||
270 | /** |
||
271 | * Generates new password reset token |
||
272 | */ |
||
273 | public function generatePasswordResetToken() |
||
277 | |||
278 | /** |
||
279 | * Removes password reset token |
||
280 | */ |
||
281 | public function removePasswordResetToken() |
||
285 | |||
286 | /** |
||
287 | * @param $lastLogin |
||
288 | */ |
||
289 | public function setLastLogin($lastLogin) |
||
293 | |||
294 | /** |
||
295 | * Update last login |
||
296 | */ |
||
297 | public function updateLastLogin() |
||
301 | |||
302 | /** |
||
303 | * Resets password. |
||
304 | * |
||
305 | * @param string $password |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function resetPassword($password) |
||
315 | } |
||
316 |
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.