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 Updatable |
||
15 | { |
||
16 | protected $fields = []; |
||
17 | protected $table = ""; |
||
18 | protected $where = []; |
||
19 | |||
20 | 11 | public static function getInstance() |
|
24 | |||
25 | /** |
||
26 | * Example: |
||
27 | * $query->fields(['name', 'price']); |
||
28 | * |
||
29 | * @param array $fields |
||
30 | * @return $this |
||
31 | */ |
||
32 | 11 | public function fields(array $fields) |
|
38 | |||
39 | /** |
||
40 | * Example |
||
41 | * $query->table('product'); |
||
42 | * |
||
43 | * @param string $table |
||
44 | * @return $this |
||
45 | */ |
||
46 | 16 | public function table($table) |
|
52 | |||
53 | /** |
||
54 | * Example: |
||
55 | * $query->filter('price > [[amount]]', [ 'amount' => 1000] ); |
||
56 | * |
||
57 | * @param string $filter |
||
58 | * @param array $params |
||
59 | * @return $this |
||
60 | */ |
||
61 | 8 | public function where($filter, array $params = []) |
|
66 | |||
67 | View Code Duplication | protected function getFields() |
|
75 | |||
76 | 10 | View Code Duplication | protected function getWhere() |
92 | |||
93 | /** |
||
94 | * @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper |
||
95 | * @param $params |
||
96 | * @return string |
||
97 | * @throws \Exception |
||
98 | */ |
||
99 | 6 | public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null) |
|
125 | |||
126 | /** |
||
127 | * @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper |
||
128 | * @param $params |
||
129 | * @return array |
||
130 | * @throws \Exception |
||
131 | */ |
||
132 | 5 | public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null) |
|
167 | |||
168 | /** |
||
169 | * @param $params |
||
170 | * @return array |
||
171 | * @throws \Exception |
||
172 | */ |
||
173 | 5 | public function buildDelete(&$params) |
|
189 | } |
||
190 |
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.