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 |
||
| 12 | final class Prepare extends \mysqli_stmt |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string $_sql - the unchanged query string provided to the constructor |
||
| 17 | */ |
||
| 18 | private $_sql; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string $_sql_with_bound_parameters - the query string with bound parameters interpolated |
||
| 22 | */ |
||
| 23 | private $_sql_with_bound_parameters; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $_use_bound_parameters_interpolated = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array $_boundParams - array of arrays containing values that have been bound to the query as parameters |
||
| 32 | */ |
||
| 33 | private $_boundParams = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var DB |
||
| 37 | */ |
||
| 38 | private $_db; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Debug |
||
| 42 | */ |
||
| 43 | private $_debug; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Prepare constructor. |
||
| 47 | * |
||
| 48 | * @param DB $db |
||
| 49 | * @param string $query |
||
| 50 | */ |
||
| 51 | 7 | public function __construct(DB $db, $query) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Prepare destructor. |
||
| 63 | */ |
||
| 64 | 7 | public function __destruct() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Combines the values stored in $this->boundParams into one array suitable for pushing as the input arguments to |
||
| 71 | * parent::bind_param when used with call_user_func_array |
||
| 72 | * |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | 5 | private function _buildArguments() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Escapes the supplied value. |
||
| 90 | * |
||
| 91 | * @param mixed $value |
||
| 92 | * @param string $type (one of 'i', 'b', 's', 'd') |
||
| 93 | * |
||
| 94 | * @return array 0 => "$value" escaped and 1 => "$valueForSqlWithBoundParameters" for insertion into the interpolated |
||
| 95 | * query string |
||
| 96 | */ |
||
| 97 | 5 | private function _prepareValue(&$value, $type) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @return int |
||
| 113 | */ |
||
| 114 | public function affected_rows() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * This is a wrapper for "bind_param" what binds variables to a prepared statement as parameters. If you use this |
||
| 121 | * wrapper, you can debug your query with e.g. "$this->get_sql_with_bound_parameters()". |
||
| 122 | * |
||
| 123 | * @param string $types <strong>i<strong> corresponding variable has type integer<br /> |
||
| 124 | * <strong>d</strong> corresponding variable has type double<br /> |
||
| 125 | * <strong>s</strong> corresponding variable has type string<br /> |
||
| 126 | * <strong>b</strong> corresponding variable is a blob and will be sent in packets |
||
| 127 | * |
||
| 128 | * INFO: We have to explicitly declare all parameters as references, otherwise it does not seem possible to pass them |
||
| 129 | * on without losing the reference property. |
||
| 130 | * |
||
| 131 | * @param null $v1 |
||
| 132 | * @param null $v2 |
||
| 133 | * @param null $v3 |
||
| 134 | * @param null $v4 |
||
| 135 | * @param null $v5 |
||
| 136 | * @param null $v6 |
||
| 137 | * @param null $v7 |
||
| 138 | * @param null $v8 |
||
| 139 | * @param null $v9 |
||
| 140 | * @param null $v10 |
||
| 141 | * @param null $v11 |
||
| 142 | * @param null $v12 |
||
| 143 | * @param null $v13 |
||
| 144 | * @param null $v14 |
||
| 145 | * @param null $v15 |
||
| 146 | * @param null $v16 |
||
| 147 | * @param null $v17 |
||
| 148 | * @param null $v18 |
||
| 149 | * @param null $v19 |
||
| 150 | * @param null $v20 |
||
| 151 | * @param null $v21 |
||
| 152 | * @param null $v22 |
||
| 153 | * @param null $v23 |
||
| 154 | * @param null $v24 |
||
| 155 | * @param null $v25 |
||
| 156 | * @param null $v26 |
||
| 157 | * @param null $v27 |
||
| 158 | * @param null $v28 |
||
| 159 | * @param null $v29 |
||
| 160 | * @param null $v30 |
||
| 161 | * @param null $v31 |
||
| 162 | * @param null $v32 |
||
| 163 | * @param null $v33 |
||
| 164 | * @param null $v34 |
||
| 165 | * @param null $v35 |
||
| 166 | * |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | 5 | public function bind_param_debug($types, &$v1 = null, &$v2 = null, &$v3 = null, &$v4 = null, &$v5 = null, &$v6 = null, &$v7 = null, &$v8 = null, &$v9 = null, &$v10 = null, &$v11 = null, &$v12 = null, &$v13 = null, &$v14 = null, &$v15 = null, &$v16 = null, &$v17 = null, &$v18 = null, &$v19 = null, &$v20 = null, &$v21 = null, &$v22 = null, &$v23 = null, &$v24 = null, &$v25 = null, &$v26 = null, &$v27 = null, &$v28 = null, &$v29 = null, &$v30 = null, &$v31 = null, &$v32 = null, &$v33 = null, &$v34 = null, &$v35 = null) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Executes a prepared Query |
||
| 207 | * |
||
| 208 | * @link http://php.net/manual/en/mysqli-stmt.execute.php |
||
| 209 | * @return bool "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 210 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 211 | * "true" by e.g. "SELECT"-queries<br /> |
||
| 212 | * "false" on error |
||
| 213 | * @since 5.0 |
||
| 214 | */ |
||
| 215 | 7 | public function execute() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Prepare an SQL statement for execution |
||
| 266 | * |
||
| 267 | * @link http://php.net/manual/en/mysqli-stmt.prepare.php |
||
| 268 | * |
||
| 269 | * @param string $query <p> |
||
| 270 | * The query, as a string. It must consist of a single SQL statement. |
||
| 271 | * </p> |
||
| 272 | * <p> |
||
| 273 | * You can include one or more parameter markers in the SQL statement by |
||
| 274 | * embedding question mark (?) characters at the |
||
| 275 | * appropriate positions. |
||
| 276 | * </p> |
||
| 277 | * <p> |
||
| 278 | * You should not add a terminating semicolon or \g |
||
| 279 | * to the statement. |
||
| 280 | * </p> |
||
| 281 | * <p> |
||
| 282 | * The markers are legal only in certain places in SQL statements. |
||
| 283 | * For example, they are allowed in the VALUES() list of an INSERT statement |
||
| 284 | * (to specify column values for a row), or in a comparison with a column in |
||
| 285 | * a WHERE clause to specify a comparison value. |
||
| 286 | * </p> |
||
| 287 | * <p> |
||
| 288 | * However, they are not allowed for identifiers (such as table or column names), |
||
| 289 | * in the select list that names the columns to be returned by a SELECT statement), |
||
| 290 | * or to specify both operands of a binary operator such as the = |
||
| 291 | * equal sign. The latter restriction is necessary because it would be impossible |
||
| 292 | * to determine the parameter type. In general, parameters are legal only in Data |
||
| 293 | * Manipulation Language (DML) statements, and not in Data Definition Language |
||
| 294 | * (DDL) statements. |
||
| 295 | * </p> |
||
| 296 | * |
||
| 297 | * @return bool false on error |
||
| 298 | * @since 5.0 |
||
| 299 | */ |
||
| 300 | 7 | public function prepare($query) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Ger the bound parameters from sql-query as array, if you use the "$this->bind_param_debug()" method. |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function get_bound_params() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public function get_sql() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get the sql-query with bound parameters, if you use the "$this->bind_param_debug()" method. |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | 4 | public function get_sql_with_bound_parameters() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @return int |
||
| 354 | */ |
||
| 355 | public function insert_id() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Copies $this->_sql then replaces bound markers with associated values ($this->_sql is not modified |
||
| 362 | * but the resulting query string is assigned to $this->sql_bound_parameters) |
||
| 363 | * |
||
| 364 | * @return string $testQuery - interpolated db query string |
||
| 365 | */ |
||
| 366 | 5 | private function interpolateQuery() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Error-handling for the sql-query. |
||
| 392 | * |
||
| 393 | * @param string $errorMsg |
||
| 394 | * @param string $sql |
||
| 395 | * |
||
| 396 | * @throws \Exception |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | 2 | View Code Duplication | private function queryErrorHandling($errorMsg, $sql) |
| 428 | |||
| 429 | } |
||
| 430 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.