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 |
||
11 | class PasswordValidator extends Validator implements PasswordValidatorInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string login attribute name |
||
15 | */ |
||
16 | public $loginAttribute = 'login'; |
||
17 | |||
18 | /** |
||
19 | * @var string password attribute name |
||
20 | */ |
||
21 | public $passwordAttribute = 'current_password'; |
||
22 | |||
23 | View Code Duplication | public function init() |
|
|
|||
24 | { |
||
25 | parent::init(); |
||
26 | if ($this->message !== null) { |
||
27 | return; |
||
28 | } |
||
29 | $this->message = Yii::t('hiam', 'The current password is incorrect'); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public function validateAttribute($model, $attribute) |
||
47 | |||
48 | public function inlineFor(Model $model): \Closure |
||
54 | } |
||
55 |
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.