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 |
||
26 | abstract class DBDriverAbstract implements DBDriverInterface{ |
||
27 | use Magic; |
||
28 | |||
29 | const CACHEKEY_HASH_ALGO = 'sha256'; |
||
30 | |||
31 | /** |
||
32 | * Holds the database resource object |
||
33 | * |
||
34 | * @var resource |
||
35 | */ |
||
36 | protected $db; |
||
37 | |||
38 | /** |
||
39 | * Holds the settings |
||
40 | * |
||
41 | * @var \chillerlan\Database\DBOptions |
||
42 | */ |
||
43 | protected $options; |
||
44 | |||
45 | /** |
||
46 | * @var \Psr\SimpleCache\CacheInterface |
||
47 | */ |
||
48 | protected $cacheDriver; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $dialect; |
||
54 | |||
55 | /** |
||
56 | * @var string[] |
||
57 | */ |
||
58 | protected $quotes; |
||
59 | |||
60 | protected $source_encoding; |
||
61 | protected $dest_encoding = 'UTF-8'; |
||
62 | |||
63 | /** |
||
64 | * Constructor. |
||
65 | * |
||
66 | * @param \chillerlan\Database\DBOptions $options |
||
67 | * @param \Psr\SimpleCache\CacheInterface|null $cacheDriver |
||
68 | */ |
||
69 | public function __construct(DBOptions $options, CacheInterface $cacheDriver = null){ |
||
73 | |||
74 | /********* |
||
75 | * magic * |
||
76 | *********/ |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | protected function magic_get_dialect():string { |
||
84 | |||
85 | /** |
||
86 | * @return array |
||
87 | */ |
||
88 | protected function magic_get_quotes():array { |
||
91 | |||
92 | abstract protected function __raw(string $sql, string $index = null, bool $assoc = true); |
||
96 | |||
97 | /** |
||
98 | * @param string $source |
||
99 | * @param string $dest |
||
100 | * |
||
101 | * @return \chillerlan\Database\Drivers\DBDriverInterface |
||
102 | */ |
||
103 | public function convertEncoding(string $source, string $dest = 'UTF-8'):DBDriverInterface{ |
||
109 | |||
110 | /** |
||
111 | * Returns the plain connection object |
||
112 | * |
||
113 | * @return resource the database resource object |
||
114 | */ |
||
115 | public function getDBResource(){ |
||
118 | |||
119 | /** |
||
120 | * @param string $dbname |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function selectDB(string $dbname):bool{ |
||
127 | |||
128 | /** |
||
129 | * @param $callable |
||
130 | * @param array $args |
||
131 | * @param string|null $index |
||
132 | * @param bool $assoc |
||
133 | * |
||
134 | * @return bool|\chillerlan\Database\DBResult |
||
135 | */ |
||
136 | protected function getResult($callable, array $args, string $index = null, bool $assoc){ |
||
153 | |||
154 | /** |
||
155 | * @param string $sql |
||
156 | * @param string|null $index |
||
157 | * @param bool $assoc |
||
158 | * |
||
159 | * @return mixed |
||
160 | * @throws \chillerlan\Database\Drivers\DBDriverException |
||
161 | */ |
||
162 | View Code Duplication | public function raw(string $sql, string $index = null, bool $assoc = true){ |
|
172 | |||
173 | /** |
||
174 | * @param string $sql |
||
175 | * @param array $values |
||
176 | * @param string|null $index |
||
177 | * @param bool $assoc |
||
178 | * |
||
179 | * @return mixed |
||
180 | * @throws \chillerlan\Database\Drivers\DBDriverException |
||
181 | */ |
||
182 | View Code Duplication | public function prepared(string $sql, array $values = [], string $index = null, bool $assoc = true){ |
|
192 | |||
193 | /** |
||
194 | * @param string $sql |
||
195 | * @param array $values |
||
196 | * |
||
197 | * @return mixed |
||
198 | * @throws \chillerlan\Database\Drivers\DBDriverException |
||
199 | */ |
||
200 | public function multi(string $sql, array $values){ |
||
214 | |||
215 | /** |
||
216 | * @param string $sql |
||
217 | * @param array $data |
||
218 | * @param array|callable $callback |
||
219 | * |
||
220 | * @return mixed |
||
221 | * @throws \chillerlan\Database\Drivers\DBDriverException |
||
222 | */ |
||
223 | public function multi_callback(string $sql, array $data, $callback){ |
||
241 | |||
242 | /** |
||
243 | * @param string $sql |
||
244 | * @param array|null $values |
||
245 | * @param string|null $index |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | public function cacheKey(string $sql, array $values = [], string $index = null):string{ |
||
252 | |||
253 | /** |
||
254 | * @param string $sql |
||
255 | * @param array $values |
||
256 | * @param string|null $index |
||
257 | * |
||
258 | * @return bool|mixed |
||
259 | */ |
||
260 | View Code Duplication | protected function cacheGet(string $sql, array $values = [], string $index = null){ |
|
268 | |||
269 | /** |
||
270 | * @param string $sql |
||
271 | * @param array $values |
||
272 | * @param string|null $index |
||
273 | * @param $response |
||
274 | * @param int|null $ttl |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | View Code Duplication | protected function cacheSet(string $sql, array $values = [], string $index = null, $response, int $ttl = null):bool{ |
|
286 | |||
287 | |||
288 | /** |
||
289 | * @param string $sql |
||
290 | * @param string|null $index |
||
291 | * @param bool $assoc |
||
292 | * @param int|null $ttl |
||
293 | * |
||
294 | * @return bool|mixed |
||
295 | */ |
||
296 | public function rawCached(string $sql, string $index = null, bool $assoc = true, int $ttl = null){ |
||
307 | |||
308 | /** |
||
309 | * @param string $sql |
||
310 | * @param array $values |
||
311 | * @param string|null $index |
||
312 | * @param bool $assoc |
||
313 | * @param int|null $ttl |
||
314 | * |
||
315 | * @return bool|mixed |
||
316 | */ |
||
317 | public function preparedCached(string $sql, array $values = [], string $index = null, bool $assoc = true, int $ttl = null){ |
||
328 | } |
||
329 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.