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 |
||
| 16 | class ToolbarButton |
||
| 17 | { |
||
| 18 | use Traits\TButtonTryAddIcon; |
||
| 19 | use Traits\TButtonClass; |
||
| 20 | use Traits\TButtonIcon; |
||
| 21 | use Traits\TButtonRenderer; |
||
| 22 | use Traits\TButtonText; |
||
| 23 | use Traits\TButtonTitle; |
||
| 24 | use Traits\TLink; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var DataGrid |
||
| 28 | */ |
||
| 29 | protected $grid; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $href; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $params; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $attributes = []; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * @param DataGrid $grid |
||
| 49 | * @param string $href |
||
| 50 | * @param string $text |
||
| 51 | * @param array $params |
||
| 52 | */ |
||
| 53 | View Code Duplication | public function __construct(DataGrid $grid, $href, $text, $params = []) |
|
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * Render toolbar button |
||
| 64 | * @return Html |
||
| 65 | */ |
||
| 66 | public function renderButton() |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @param array $attrs |
||
| 101 | * @return static |
||
| 102 | */ |
||
| 103 | public function addAttributes(array $attrs) |
||
| 109 | } |
||
| 110 |
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.