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 |
||
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() |
||
33 | |||
34 | /** |
||
35 | * @inheritdoc |
||
36 | */ |
||
37 | public function rules() |
||
47 | |||
48 | /** |
||
49 | * Restore |
||
50 | * @return bool |
||
51 | * @throws yii\base\InvalidConfigException |
||
52 | */ |
||
53 | View Code Duplication | public function restore() |
|
65 | /** |
||
66 | * Change password |
||
67 | * @return bool |
||
68 | * @throws yii\base\InvalidConfigException |
||
69 | */ |
||
70 | View Code Duplication | public function changePassword() |
|
83 | } |
||
84 |
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.