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 |
||
7 | class Builder extends BaseBuilder |
||
8 | { |
||
9 | /** |
||
10 | * Table attributes. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $tableAttributes = [ |
||
15 | 'id' => 'dataTable', |
||
16 | 'class' => 'table table-bordered table-hover dt-responsive', |
||
17 | 'width' => '100%', |
||
18 | ]; |
||
19 | |||
20 | /** |
||
21 | * Set table "id" attribute. |
||
22 | * |
||
23 | * @param string $id |
||
24 | * @return $this |
||
25 | */ |
||
26 | public function tableId($id) |
||
30 | |||
31 | /** |
||
32 | * Change table style to striped. |
||
33 | * |
||
34 | * @return $this |
||
35 | */ |
||
36 | View Code Duplication | public function stripedTable() |
|
47 | |||
48 | /** |
||
49 | * Change table style to hovered. |
||
50 | * |
||
51 | * @return $this |
||
52 | */ |
||
53 | View Code Duplication | public function hoveredTable() |
|
64 | } |
||
65 |
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.