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 |
||
24 | View Code Duplication | trait ModelTrait |
|
|
|||
25 | { |
||
26 | |||
27 | use ObjectTrait; |
||
28 | |||
29 | /******************************************* |
||
30 | * Model -to- Record |
||
31 | *******************************************/ |
||
32 | |||
33 | /** |
||
34 | * @param Model $model |
||
35 | * @param Record $record |
||
36 | * @param bool $mirrorScenario |
||
37 | * @return void |
||
38 | */ |
||
39 | public function transferToRecord(Model $model, Record $record, bool $mirrorScenario = true) |
||
53 | |||
54 | /** |
||
55 | * @param Model $model |
||
56 | * @param bool $mirrorScenario |
||
57 | * @return Record |
||
58 | */ |
||
59 | public function toRecord(Model $model, bool $mirrorScenario = true): Record |
||
81 | |||
82 | } |
||
83 |
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.