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:
Complex classes like MySQLiDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MySQLiDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class MySQLiDriver extends DBBaseDriver implements DBDriverInterface{ |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Holds the database resource object |
||
| 33 | * |
||
| 34 | * @var mysqli |
||
| 35 | */ |
||
| 36 | protected $db; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Establishes a database connection and returns the connection object |
||
| 40 | * |
||
| 41 | * @return mysqli the database resource object |
||
| 42 | * @throws DBException |
||
| 43 | */ |
||
| 44 | public function connect(){ |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Closes a database connection |
||
| 89 | * |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | public function disconnect(){ |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns info about the used php client |
||
| 98 | * |
||
| 99 | * @return string php's database client string |
||
| 100 | */ |
||
| 101 | public function getClientInfo(){ |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns info about the database server |
||
| 107 | * |
||
| 108 | * @return string database's serverinfo string |
||
| 109 | */ |
||
| 110 | public function getServerInfo(){ |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Sanitizer. |
||
| 116 | * |
||
| 117 | * Recursively escapes string values, optional htmlspecialchars() |
||
| 118 | * |
||
| 119 | * @param array|string $data array or string to escape |
||
| 120 | * @param bool $specialchars [optional] if true, it performs a htmlspecialchars() on each value given |
||
| 121 | * |
||
| 122 | * @return array|string array or string. escaped. obviously. |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function escape($data, $specialchars = false){ |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Basic SQL query for non prepared statements |
||
| 154 | * |
||
| 155 | * There is no escaping in here, so make sure, your SQL is clean/escaped. |
||
| 156 | * Also, your SQL should NEVER contain user input, use prepared statements in this case. |
||
| 157 | * |
||
| 158 | * If the query was successful it returns either an array of results or true |
||
| 159 | * if it was a void query. On errors, a false will be returned, obviously. |
||
| 160 | * |
||
| 161 | * @param string $sql The SQL statement |
||
| 162 | * @param string $index [optional] an index column to assingn as the result's keys |
||
| 163 | * @param bool $assoc [optional] If true, the fields are named with the respective column names, otherwise numbered |
||
| 164 | * @param bool $fetch_array [optional] fetch the vaues as array instead of object |
||
| 165 | * |
||
| 166 | * @return array|bool array with results, true on void query success, otherwise false. |
||
| 167 | * @throws \chillerlan\Database\DBException |
||
| 168 | */ |
||
| 169 | public function raw($sql, $index = '', $assoc = true, $fetch_array = false){ |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Prepared statements wrapper |
||
| 222 | * |
||
| 223 | * Does everything for you: prepares the statement and fetches the results as an object or array |
||
| 224 | * just pass a query along with values and you're done. Not meant for multi-inserts. |
||
| 225 | * |
||
| 226 | * @param string $sql The SQL statement to prepare |
||
| 227 | * @param array $values [optional] the value for each "?" in the statement - in the respective order, of course |
||
| 228 | * @param string $index [optional] an index column to assingn as the result's keys |
||
| 229 | * @param bool $assoc [optional] If true, the fields are named with the respective column names, otherwise numbered |
||
| 230 | * @param bool $fetch_array [optional] fetch the vaues as array instead of object |
||
| 231 | * |
||
| 232 | * @return array|bool Array with results, true on void query success, otherwise false |
||
| 233 | * @throws \chillerlan\Database\DBException |
||
| 234 | */ |
||
| 235 | public function prepared($sql, array $values = [], $index = '', $assoc = true, $fetch_array = false){ |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Prepared multi line insert |
||
| 277 | * |
||
| 278 | * Prepared statement multi insert/update |
||
| 279 | * |
||
| 280 | * @param string $sql The SQL statement to prepare |
||
| 281 | * @param array $values a multidimensional array with the values, each row represents one line to insert. |
||
| 282 | * |
||
| 283 | * @return bool true query success, otherwise false |
||
| 284 | * @throws \chillerlan\Database\DBException |
||
| 285 | */ |
||
| 286 | public function multi($sql, array $values){ |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Prepared multi line insert/update with callback |
||
| 310 | * @see https://gist.github.com/krakjoe/6437782 |
||
| 311 | * @see https://gist.github.com/krakjoe/9384409 |
||
| 312 | * |
||
| 313 | * @param string $sql The SQL statement to prepare |
||
| 314 | * @param array $data an array with the (raw) data to insert, each row represents one line to insert. |
||
| 315 | * @param callable|array $callback a callback that processes the values for each row. |
||
| 316 | * |
||
| 317 | * @return bool true query success, otherwise false |
||
| 318 | * @throws \chillerlan\Database\DBException |
||
| 319 | */ |
||
| 320 | public function multi_callback($sql, array $data, $callback){ |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param \mysqli_stmt $stmt |
||
| 351 | * @param \ReflectionMethod $reflectionMethod |
||
| 352 | * @param array $row |
||
| 353 | */ |
||
| 354 | protected function insertPreparedRow(mysqli_stmt &$stmt, ReflectionMethod &$reflectionMethod, array &$row){ |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param \mysqli_stmt $stmt |
||
| 363 | * @param \mysqli_result $result |
||
| 364 | * @param bool $assoc |
||
| 365 | * @param bool $fetch_array |
||
| 366 | * |
||
| 367 | * @return array|bool |
||
| 368 | */ |
||
| 369 | protected function getResult(mysqli_stmt &$stmt, mysqli_result &$result, $assoc, $fetch_array){ |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Returns a string of types for the given values |
||
| 411 | * |
||
| 412 | * @link http://php.net/manual/mysqli-stmt.bind-param.php |
||
| 413 | * |
||
| 414 | * @param array $values |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | * @internal |
||
| 418 | */ |
||
| 419 | protected function getTypes(array &$values){ |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Copies an array to an array of referenced values |
||
| 435 | * |
||
| 436 | * @param array $row |
||
| 437 | * |
||
| 438 | * @return array |
||
| 439 | * @see http://php.net/manual/mysqli-stmt.bind-param.php |
||
| 440 | */ |
||
| 441 | protected function getReferences(array &$row){ |
||
| 450 | |||
| 451 | } |
||
| 452 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..