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 | class Query |
||
15 | { |
||
16 | protected $fields = []; |
||
17 | protected $table = ""; |
||
18 | protected $where = []; |
||
19 | protected $groupBy = []; |
||
20 | protected $orderBy = []; |
||
21 | protected $join = []; |
||
22 | protected $limitStart = null; |
||
23 | protected $limitEnd = null; |
||
24 | protected $top = null; |
||
25 | |||
26 | protected $forUpdate = false; |
||
27 | |||
28 | 6 | public static function getInstance() |
|
32 | |||
33 | /** |
||
34 | * Example: |
||
35 | * $query->fields(['name', 'price']); |
||
36 | * |
||
37 | * @param array $fields |
||
38 | * @return $this |
||
39 | */ |
||
40 | 5 | public function fields(array $fields) |
|
52 | |||
53 | 1 | private function addFieldFromMapper(Mapper $mapper) |
|
73 | |||
74 | /** |
||
75 | * Example |
||
76 | * $query->table('product'); |
||
77 | * |
||
78 | * @param string $table |
||
79 | * @return $this |
||
80 | */ |
||
81 | 24 | public function table($table) |
|
87 | |||
88 | /** |
||
89 | * Example: |
||
90 | * $query->join('sales', 'product.id = sales.id'); |
||
91 | * |
||
92 | * @param string $table |
||
93 | * @param string $filter |
||
94 | * @return $this |
||
95 | */ |
||
96 | 1 | public function join($table, $filter) |
|
101 | |||
102 | /** |
||
103 | * Example: |
||
104 | * $query->join('sales', 'product.id = sales.id'); |
||
105 | * |
||
106 | * @param string $table |
||
107 | * @param string $filter |
||
108 | * @return $this |
||
109 | */ |
||
110 | 1 | public function leftJoin($table, $filter) |
|
115 | |||
116 | /** |
||
117 | * Example: |
||
118 | * $query->filter('price > [[amount]]', [ 'amount' => 1000] ); |
||
119 | * |
||
120 | * @param string $filter |
||
121 | * @param array $params |
||
122 | * @return $this |
||
123 | */ |
||
124 | 22 | public function where($filter, array $params = []) |
|
129 | |||
130 | /** |
||
131 | * Example: |
||
132 | * $query->groupBy(['name']); |
||
133 | * |
||
134 | * @param array $fields |
||
135 | * @return $this |
||
136 | */ |
||
137 | 1 | public function groupBy(array $fields) |
|
143 | |||
144 | /** |
||
145 | * Example: |
||
146 | * $query->orderBy(['price desc']); |
||
147 | * |
||
148 | * @param array $fields |
||
149 | * @return $this |
||
150 | */ |
||
151 | 4 | public function orderBy(array $fields) |
|
157 | |||
158 | public function forUpdate() |
||
164 | |||
165 | 1 | public function limit($start, $end) |
|
174 | |||
175 | 1 | public function top($top) |
|
183 | |||
184 | 24 | View Code Duplication | protected function getFields() |
192 | |||
193 | 24 | protected function getJoin() |
|
201 | |||
202 | 24 | View Code Duplication | protected function getWhere() |
218 | |||
219 | /** |
||
220 | * @param \ByJG\AnyDataset\DbDriverInterface|null $dbDriver |
||
221 | * @return array |
||
222 | */ |
||
223 | 24 | public function build(DbDriverInterface $dbDriver = null) |
|
269 | } |
||
270 |
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.