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 |
||
| 12 | View Code Duplication | class Min extends Query |
|
|
|
|||
| 13 | { |
||
| 14 | use ExtendedSelectionTrait; |
||
| 15 | |||
| 16 | protected $field; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Set the field name to maximize over. |
||
| 20 | * |
||
| 21 | * @param string $field |
||
| 22 | * |
||
| 23 | * @return self |
||
| 24 | */ |
||
| 25 | public function field($field) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Run the query and return the value. |
||
| 34 | * |
||
| 35 | * @return int |
||
| 36 | */ |
||
| 37 | public function run() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritdoc} |
||
| 47 | */ |
||
| 48 | public function __invoke() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Build and return the query. |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function __toString() |
||
| 71 | } |
||
| 72 |
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.