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 |
||
14 | trait ExtendedSelectionTrait |
||
15 | { |
||
16 | use SelectionTrait; |
||
17 | |||
18 | protected $from = []; |
||
19 | protected $fields = []; |
||
20 | |||
21 | /** |
||
22 | * Adds new extra table to the query. |
||
23 | * |
||
24 | * @param string $table |
||
25 | * |
||
26 | * @return self |
||
27 | */ |
||
28 | public function from($table) |
||
34 | |||
35 | /** |
||
36 | * Adds a new extra field to the query. |
||
37 | * |
||
38 | * @param string $field |
||
39 | * |
||
40 | * @return self |
||
41 | */ |
||
42 | public function field($field) |
||
48 | |||
49 | /** |
||
50 | * Adds a WHERE according with the relation of other table. |
||
51 | * |
||
52 | * @param AbstractRow $row |
||
53 | * |
||
54 | * @return self |
||
55 | */ |
||
56 | public function relatedWith(AbstractRow $row) |
||
89 | |||
90 | /** |
||
91 | * add extra fields to the code. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | protected function fieldsToString($prepend = ', ') |
||
99 | |||
100 | /** |
||
101 | * add extra fields to the code. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function fromToString($prepend = ', ') |
||
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.