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 |
||
25 | abstract class DBDriverAbstract implements DBDriverInterface{ |
||
26 | |||
27 | /** |
||
28 | * Holds the database resource object |
||
29 | * |
||
30 | * @var resource |
||
31 | */ |
||
32 | protected $db; |
||
33 | |||
34 | /** |
||
35 | * Holds the settings |
||
36 | * |
||
37 | * @var \chillerlan\Database\DBOptions |
||
38 | */ |
||
39 | protected $options; |
||
40 | |||
41 | /** |
||
42 | * @var \Psr\SimpleCache\CacheInterface |
||
43 | */ |
||
44 | protected $cacheDriver; |
||
45 | |||
46 | abstract protected function __escape($data); |
||
47 | |||
48 | abstract protected function __raw(string $sql, string $index = null, bool $assoc = true); |
||
49 | |||
50 | abstract protected function __prepared(string $sql, array $values = [], string $index = null, bool $assoc = true); |
||
51 | |||
52 | abstract protected function __multi(string $sql, array $values); |
||
53 | |||
54 | abstract protected function __multi_callback(string $sql, array $data, $callback); |
||
55 | |||
56 | /** |
||
57 | * Constructor. |
||
58 | * |
||
59 | * @param \chillerlan\Database\DBOptions $options |
||
60 | * @param \Psr\SimpleCache\CacheInterface|null $cacheDriver |
||
61 | */ |
||
62 | public function __construct(DBOptions $options, CacheInterface $cacheDriver = null){ |
||
66 | |||
67 | /** |
||
68 | * Returns the plain connection object |
||
69 | * |
||
70 | * @return resource the database resource object |
||
71 | */ |
||
72 | public function getDBResource(){ |
||
75 | |||
76 | /** |
||
77 | * @param array|string $data |
||
78 | * @param bool $specialchars |
||
79 | * |
||
80 | * @return array|string |
||
81 | */ |
||
82 | View Code Duplication | public function escape($data, bool $specialchars = false){ |
|
110 | |||
111 | /** |
||
112 | * @param $callable |
||
113 | * @param array $args |
||
114 | * @param string|null $index |
||
115 | * @param bool $assoc |
||
116 | * |
||
117 | * @return bool|\chillerlan\Database\DBResult |
||
118 | */ |
||
119 | protected function getResult($callable, array $args, string $index = null, bool $assoc){ |
||
136 | |||
137 | /** |
||
138 | * @param string $sql |
||
139 | * @param string|null $index |
||
140 | * @param bool $assoc |
||
141 | * |
||
142 | * @return mixed |
||
143 | * @throws \chillerlan\Database\DBException |
||
144 | */ |
||
145 | public function raw(string $sql, string $index = null, bool $assoc = true){ |
||
155 | |||
156 | /** |
||
157 | * @param string $sql |
||
158 | * @param array $values |
||
159 | * @param string|null $index |
||
160 | * @param bool $assoc |
||
161 | * |
||
162 | * @return mixed |
||
163 | * @throws \chillerlan\Database\DBException |
||
164 | */ |
||
165 | public function prepared(string $sql, array $values = [], string $index = null, bool $assoc = true){ |
||
175 | |||
176 | /** |
||
177 | * @param string $sql |
||
178 | * @param array $values |
||
179 | * |
||
180 | * @return mixed |
||
181 | * @throws \chillerlan\Database\DBException |
||
182 | */ |
||
183 | public function multi(string $sql, array $values){ |
||
197 | |||
198 | /** |
||
199 | * @param string $sql |
||
200 | * @param array $data |
||
201 | * @param array|callable $callback |
||
202 | * |
||
203 | * @return mixed |
||
204 | * @throws \chillerlan\Database\DBException |
||
205 | */ |
||
206 | public function multi_callback(string $sql, array $data, $callback){ |
||
224 | |||
225 | protected function cacheGet(string $sql, array $values = []){ |
||
233 | |||
234 | protected function cacheSet(string $sql, array $values = [], $response):bool{ |
||
243 | |||
244 | protected function cacheKey(string $sql, array $values):string{ |
||
247 | |||
248 | View Code Duplication | public function rawCached(string $sql, string $index = null, bool $assoc = true){ |
|
260 | |||
261 | View Code Duplication | public function preparedCached(string $sql, array $values = [], string $index = null, bool $assoc = true){ |
|
272 | } |
||
273 |
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.