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 |
||
| 21 | View Code Duplication | trait CountAttributeTrait |
|
|
|
|||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Returns the aggregate value of a field. |
||
| 25 | * |
||
| 26 | * @param string $field |
||
| 27 | * |
||
| 28 | * @return int |
||
| 29 | */ |
||
| 30 | protected function getCountAttribute($field) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Eager load relations on the model. |
||
| 45 | * |
||
| 46 | * @param array|string $relations |
||
| 47 | * |
||
| 48 | * @return $this |
||
| 49 | */ |
||
| 50 | abstract public function load($relations); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get a specified relationship. |
||
| 54 | * |
||
| 55 | * @param string $relation |
||
| 56 | * |
||
| 57 | * @return mixed |
||
| 58 | */ |
||
| 59 | abstract public function getRelation($relation); |
||
| 60 | } |
||
| 61 |
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.