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 |
||
| 5 | View Code Duplication | trait RequiredTrait |
|
|
|
|||
| 6 | { |
||
| 7 | use OptionAccessorsTrait; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @param bool $required |
||
| 11 | * @return $this |
||
| 12 | * @link http://symfony.com/doc/current/reference/forms/types/form.html#required |
||
| 13 | */ |
||
| 14 | public function setRequired($required = true) |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @return bool|string |
||
| 21 | * @link http://symfony.com/doc/current/reference/forms/types/form.html#required |
||
| 22 | */ |
||
| 23 | public function getRequired() |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @return $this |
||
| 30 | * @link http://symfony.com/doc/current/reference/forms/types/form.html#required |
||
| 31 | */ |
||
| 32 | public function removeRequired() |
||
| 36 | } |
||
| 37 |
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.