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 Database |
||
15 | { |
||
16 | /** |
||
17 | * @var Connector |
||
18 | */ |
||
19 | protected $connector; |
||
20 | |||
21 | /** |
||
22 | * @var \PDO |
||
23 | */ |
||
24 | protected $pdo; |
||
25 | |||
26 | /** |
||
27 | * @var \PDOStatement |
||
28 | */ |
||
29 | protected $statement; |
||
30 | |||
31 | /** |
||
32 | * @var Query |
||
33 | */ |
||
34 | protected $query; |
||
35 | |||
36 | /** |
||
37 | * @var |
||
38 | */ |
||
39 | protected $grammar; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $driver = [ |
||
45 | "mysql" => [ |
||
46 | "connector" => ConnectorMySql::class, |
||
47 | "grammar" => GrammarMySql::class, |
||
48 | ] |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $config = [ |
||
55 | "driver" => "mysql", |
||
56 | |||
57 | "host" => "", |
||
58 | "username" => "", |
||
59 | "password" => "", |
||
60 | |||
61 | "dbname" => "", |
||
62 | "options" => [ |
||
63 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
64 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC |
||
65 | ] |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $model = null; |
||
72 | |||
73 | /** |
||
74 | * @param $config |
||
75 | */ |
||
76 | public function __construct($config) |
||
84 | |||
85 | /** |
||
86 | * @param $config |
||
87 | * @return static |
||
88 | */ |
||
89 | public static function create($config) |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | */ |
||
97 | protected function initialize() |
||
107 | |||
108 | /** |
||
109 | * @return PDO |
||
110 | */ |
||
111 | public function createConnection() |
||
116 | |||
117 | /** |
||
118 | * @param $query |
||
119 | * @param null $params |
||
120 | * @return $this |
||
121 | */ |
||
122 | public function sql($query, $params = null) |
||
127 | |||
128 | /** |
||
129 | * @param $table |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function table($table) |
||
137 | |||
138 | /** |
||
139 | * @param null $columns |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function select($columns = null) |
||
147 | |||
148 | /** |
||
149 | * @param $query |
||
150 | * @param string $type |
||
151 | * @return Query |
||
152 | */ |
||
153 | public function whereQ($query, $type = "AND") |
||
158 | |||
159 | /** |
||
160 | * @param $column |
||
161 | * @param $value |
||
162 | * @param string $operator |
||
163 | * @param string $type |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function where($column, $value, $operator = "=", $type = "AND") |
||
171 | |||
172 | /** |
||
173 | * @param $column |
||
174 | * @param $value |
||
175 | * @param string $operator |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function andWhere($column, $value, $operator = "=") |
||
183 | |||
184 | /** |
||
185 | * @param $column |
||
186 | * @param $value |
||
187 | * @param string $operator |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function orWhere($column, $value, $operator = "=") |
||
195 | |||
196 | /** |
||
197 | * @param $column |
||
198 | * @param $value |
||
199 | * @param string $type |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function like($column, $value, $type = "AND") |
||
207 | |||
208 | /** |
||
209 | * @param $column |
||
210 | * @param $value |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function andLike($column, $value) |
||
218 | |||
219 | /** |
||
220 | * @param $column |
||
221 | * @param $value |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function orLike($column, $value) |
||
229 | |||
230 | /** |
||
231 | * @param $column |
||
232 | * @param $value |
||
233 | * @param string $type |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function between($column, $value, $type = "AND") |
||
241 | |||
242 | /** |
||
243 | * @param $column |
||
244 | * @param $value |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function andBetween($column, $value) |
||
252 | |||
253 | /** |
||
254 | * @param $column |
||
255 | * @param $value |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function orBetween($column, $value) |
||
263 | |||
264 | /** |
||
265 | * @return $this |
||
266 | * @throws \Exception |
||
267 | */ |
||
268 | public function execute() |
||
280 | |||
281 | /** |
||
282 | * @return array |
||
283 | * @throws \Exception |
||
284 | */ |
||
285 | View Code Duplication | public function all() |
|
300 | |||
301 | /** |
||
302 | * @return mixed |
||
303 | * @throws \Exception |
||
304 | */ |
||
305 | View Code Duplication | public function get() |
|
318 | |||
319 | /** |
||
320 | * Clear statement PDO and query builder |
||
321 | */ |
||
322 | public function clear() |
||
327 | |||
328 | /** |
||
329 | * @return string |
||
330 | */ |
||
331 | public function getModel() |
||
335 | |||
336 | /** |
||
337 | * @param string $model |
||
338 | * @param null $table |
||
339 | */ |
||
340 | public function setModel($model, $table = null) |
||
349 | } |
||
350 |
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.