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 |
||
| 23 | abstract class DBDriverAbstract implements DBDriverInterface{ |
||
| 24 | |||
| 25 | const CACHEKEY_HASH_ALGO = 'sha256'; |
||
| 26 | /** |
||
| 27 | * Holds the database resource object |
||
| 28 | * |
||
| 29 | * @var resource |
||
| 30 | */ |
||
| 31 | protected $db; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Holds the settings |
||
| 35 | * |
||
| 36 | * @var \chillerlan\Database\DBOptions |
||
| 37 | */ |
||
| 38 | protected $options; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \Psr\SimpleCache\CacheInterface |
||
| 42 | */ |
||
| 43 | protected $cacheDriver; |
||
| 44 | |||
| 45 | abstract protected function __raw(string $sql, string $index = null, bool $assoc = true); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor. |
||
| 52 | * |
||
| 53 | * @param \chillerlan\Database\DBOptions $options |
||
| 54 | * @param \Psr\SimpleCache\CacheInterface|null $cacheDriver |
||
| 55 | */ |
||
| 56 | public function __construct(DBOptions $options, CacheInterface $cacheDriver = null){ |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Returns the plain connection object |
||
| 63 | * |
||
| 64 | * @return resource the database resource object |
||
| 65 | */ |
||
| 66 | public function getDBResource(){ |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param $callable |
||
| 72 | * @param array $args |
||
| 73 | * @param string|null $index |
||
| 74 | * @param bool $assoc |
||
| 75 | * |
||
| 76 | * @return bool|\chillerlan\Database\DBResult |
||
| 77 | */ |
||
| 78 | protected function getResult($callable, array $args, string $index = null, bool $assoc){ |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $sql |
||
| 98 | * @param string|null $index |
||
| 99 | * @param bool $assoc |
||
| 100 | * |
||
| 101 | * @return mixed |
||
| 102 | * @throws \chillerlan\Database\DBException |
||
| 103 | */ |
||
| 104 | public function raw(string $sql, string $index = null, bool $assoc = true){ |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $sql |
||
| 117 | * @param array $values |
||
| 118 | * @param string|null $index |
||
| 119 | * @param bool $assoc |
||
| 120 | * |
||
| 121 | * @return mixed |
||
| 122 | * @throws \chillerlan\Database\DBException |
||
| 123 | */ |
||
| 124 | public function prepared(string $sql, array $values = [], string $index = null, bool $assoc = true){ |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $sql |
||
| 137 | * @param array $values |
||
| 138 | * |
||
| 139 | * @return mixed |
||
| 140 | * @throws \chillerlan\Database\DBException |
||
| 141 | */ |
||
| 142 | public function multi(string $sql, array $values){ |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $sql |
||
| 159 | * @param array $data |
||
| 160 | * @param array|callable $callback |
||
| 161 | * |
||
| 162 | * @return mixed |
||
| 163 | * @throws \chillerlan\Database\DBException |
||
| 164 | */ |
||
| 165 | public function multi_callback(string $sql, array $data, $callback){ |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param string $sql |
||
| 186 | * @param array|null $values |
||
| 187 | * @param string|null $index |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function cacheKey(string $sql, array $values = [], string $index = null):string{ |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $sql |
||
| 197 | * @param array $values |
||
| 198 | * @param string|null $index |
||
| 199 | * |
||
| 200 | * @return bool|mixed |
||
| 201 | */ |
||
| 202 | View Code Duplication | protected function cacheGet(string $sql, array $values = [], string $index = null){ |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $sql |
||
| 213 | * @param array $values |
||
| 214 | * @param string|null $index |
||
| 215 | * @param $response |
||
| 216 | * @param int|null $ttl |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | View Code Duplication | protected function cacheSet(string $sql, array $values = [], string $index = null, $response, int $ttl = null):bool{ |
|
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $sql |
||
| 232 | * @param string|null $index |
||
| 233 | * @param bool $assoc |
||
| 234 | * @param int|null $ttl |
||
| 235 | * |
||
| 236 | * @return bool|mixed |
||
| 237 | */ |
||
| 238 | public function rawCached(string $sql, string $index = null, bool $assoc = true, int $ttl = null){ |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $sql |
||
| 252 | * @param array $values |
||
| 253 | * @param string|null $index |
||
| 254 | * @param bool $assoc |
||
| 255 | * @param int|null $ttl |
||
| 256 | * |
||
| 257 | * @return bool|mixed |
||
| 258 | */ |
||
| 259 | public function preparedCached(string $sql, array $values = [], string $index = null, bool $assoc = true, int $ttl = null){ |
||
| 270 | } |
||
| 271 |
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
@returndoc comment to communicate to implementors of these methods what they are expected to return.