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 |
||
| 14 | final class Prepare extends \mysqli_stmt |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string $_sql - the unchanged query string provided to the constructor |
||
| 19 | */ |
||
| 20 | private $_sql; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string $_sql_with_bound_parameters - the query string with bound parameters interpolated |
||
| 24 | */ |
||
| 25 | private $_sql_with_bound_parameters; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $_use_bound_parameters_interpolated = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array $_boundParams - array of arrays containing values that have been bound to the query as parameters |
||
| 34 | */ |
||
| 35 | private $_boundParams = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var DB |
||
| 39 | */ |
||
| 40 | private $_db; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Debug |
||
| 44 | */ |
||
| 45 | private $_debug; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Prepare constructor. |
||
| 49 | * |
||
| 50 | * @param DB $db |
||
| 51 | * @param string $query |
||
| 52 | */ |
||
| 53 | 9 | public function __construct(DB $db, $query) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Prepare destructor. |
||
| 65 | */ |
||
| 66 | 9 | public function __destruct() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Combines the values stored in $this->boundParams into one array suitable for pushing as the input arguments to |
||
| 73 | * parent::bind_param when used with call_user_func_array |
||
| 74 | * |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | 6 | private function _buildArguments() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Escapes the supplied value. |
||
| 92 | * |
||
| 93 | * @param array $param |
||
| 94 | * |
||
| 95 | * @return array 0 => "$value" escaped<br /> |
||
| 96 | * 1 => "$valueForSqlWithBoundParameters" for insertion into the interpolated query string |
||
| 97 | */ |
||
| 98 | 6 | private function _prepareValue(&$param) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return int |
||
| 121 | */ |
||
| 122 | public function affected_rows() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * This is a wrapper for "bind_param" what binds variables to a prepared statement as parameters. If you use this |
||
| 129 | * wrapper, you can debug your query with e.g. "$this->get_sql_with_bound_parameters()". |
||
| 130 | * |
||
| 131 | * @param string $types <strong>i<strong> corresponding variable has type integer<br /> |
||
| 132 | * <strong>d</strong> corresponding variable has type double<br /> |
||
| 133 | * <strong>s</strong> corresponding variable has type string<br /> |
||
| 134 | * <strong>b</strong> corresponding variable is a blob and will be sent in packets |
||
| 135 | * |
||
| 136 | * INFO: We have to explicitly declare all parameters as references, otherwise it does not seem possible to pass them |
||
| 137 | * on without losing the reference property. |
||
| 138 | * |
||
| 139 | * @param mixed $v1 |
||
| 140 | * @param mixed $v2 |
||
| 141 | * @param mixed $v3 |
||
| 142 | * @param mixed $v4 |
||
| 143 | * @param mixed $v5 |
||
| 144 | * @param mixed $v6 |
||
| 145 | * @param mixed $v7 |
||
| 146 | * @param mixed $v8 |
||
| 147 | * @param mixed $v9 |
||
| 148 | * @param mixed $v10 |
||
| 149 | * @param mixed $v11 |
||
| 150 | * @param mixed $v12 |
||
| 151 | * @param mixed $v13 |
||
| 152 | * @param mixed $v14 |
||
| 153 | * @param mixed $v15 |
||
| 154 | * @param mixed $v16 |
||
| 155 | * @param mixed $v17 |
||
| 156 | * @param mixed $v18 |
||
| 157 | * @param mixed $v19 |
||
| 158 | * @param mixed $v20 |
||
| 159 | * @param mixed $v21 |
||
| 160 | * @param mixed $v22 |
||
| 161 | * @param mixed $v23 |
||
| 162 | * @param mixed $v24 |
||
| 163 | * @param mixed $v25 |
||
| 164 | * @param mixed $v26 |
||
| 165 | * @param mixed $v27 |
||
| 166 | * @param mixed $v28 |
||
| 167 | * @param mixed $v29 |
||
| 168 | * @param mixed $v30 |
||
| 169 | * @param mixed $v31 |
||
| 170 | * @param mixed $v32 |
||
| 171 | * @param mixed $v33 |
||
| 172 | * @param mixed $v34 |
||
| 173 | * @param mixed $v35 |
||
| 174 | * |
||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | 6 | 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) |
|
| 178 | { |
||
| 179 | 6 | $this->_use_bound_parameters_interpolated = true; |
|
| 180 | |||
| 181 | // debug_backtrace returns arguments by reference, see comments at http://php.net/manual/de/function.func-get-args.php |
||
| 182 | 6 | if (Bootup::is_php('5.4')) { |
|
| 183 | $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1); |
||
| 184 | } else { |
||
| 185 | 6 | $trace = debug_backtrace(); |
|
| 186 | } |
||
| 187 | |||
| 188 | 6 | $args =& $trace[0]['args']; |
|
| 189 | 6 | $types = str_split($types); |
|
| 190 | |||
| 191 | 6 | $args_count = count($args) - 1; |
|
| 192 | 6 | $types_count = count($types); |
|
| 193 | |||
| 194 | 6 | if ($args_count !== $types_count) { |
|
| 195 | trigger_error('Number of variables doesn\'t match number of parameters in prepared statement', E_WARNING); |
||
| 196 | |||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | 6 | $arg = 1; |
|
| 201 | 6 | foreach ($types as $typeInner) { |
|
| 202 | 6 | $val =& $args[$arg]; |
|
| 203 | 6 | $this->_boundParams[] = array( |
|
| 204 | 6 | 'type' => $typeInner, |
|
| 205 | 6 | 'value' => &$val, |
|
| 206 | ); |
||
| 207 | 6 | $arg++; |
|
| 208 | 6 | } |
|
| 209 | |||
| 210 | 6 | return true; |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @inheritdoc |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | public function execute_raw() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Executes a prepared Query |
||
| 225 | * |
||
| 226 | * @link http://php.net/manual/en/mysqli-stmt.execute.php |
||
| 227 | * |
||
| 228 | * @return bool|int|Result "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 229 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 230 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 231 | * "true" by e.g. "DROP"-queries<br /> |
||
| 232 | * "false" on error |
||
| 233 | */ |
||
| 234 | 9 | public function execute() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Prepare an SQL statement for execution |
||
| 286 | * |
||
| 287 | * @link http://php.net/manual/en/mysqli-stmt.prepare.php |
||
| 288 | * |
||
| 289 | * @param string $query <p> |
||
| 290 | * The query, as a string. It must consist of a single SQL statement. |
||
| 291 | * </p> |
||
| 292 | * <p> |
||
| 293 | * You can include one or more parameter markers in the SQL statement by |
||
| 294 | * embedding question mark (?) characters at the |
||
| 295 | * appropriate positions. |
||
| 296 | * </p> |
||
| 297 | * <p> |
||
| 298 | * You should not add a terminating semicolon or \g |
||
| 299 | * to the statement. |
||
| 300 | * </p> |
||
| 301 | * <p> |
||
| 302 | * The markers are legal only in certain places in SQL statements. |
||
| 303 | * For example, they are allowed in the VALUES() list of an INSERT statement |
||
| 304 | * (to specify column values for a row), or in a comparison with a column in |
||
| 305 | * a WHERE clause to specify a comparison value. |
||
| 306 | * </p> |
||
| 307 | * <p> |
||
| 308 | * However, they are not allowed for identifiers (such as table or column names), |
||
| 309 | * in the select list that names the columns to be returned by a SELECT statement), |
||
| 310 | * or to specify both operands of a binary operator such as the = |
||
| 311 | * equal sign. The latter restriction is necessary because it would be impossible |
||
| 312 | * to determine the parameter type. In general, parameters are legal only in Data |
||
| 313 | * Manipulation Language (DML) statements, and not in Data Definition Language |
||
| 314 | * (DDL) statements. |
||
| 315 | * </p> |
||
| 316 | * |
||
| 317 | * @return bool false on error |
||
| 318 | * @since 5.0 |
||
| 319 | */ |
||
| 320 | 9 | public function prepare($query) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Ger the bound parameters from sql-query as array, if you use the "$this->bind_param_debug()" method. |
||
| 346 | * |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public function get_bound_params() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function get_sql() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Get the sql-query with bound parameters, if you use the "$this->bind_param_debug()" method. |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | 4 | public function get_sql_with_bound_parameters() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @return int |
||
| 374 | */ |
||
| 375 | public function insert_id() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Copies $this->_sql then replaces bound markers with associated values ($this->_sql is not modified |
||
| 382 | * but the resulting query string is assigned to $this->sql_bound_parameters) |
||
| 383 | * |
||
| 384 | * @return string $testQuery - interpolated db query string |
||
| 385 | */ |
||
| 386 | 6 | private function interpolateQuery() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Error-handling for the sql-query. |
||
| 410 | * |
||
| 411 | * @param string $errorMsg |
||
| 412 | * @param string $sql |
||
| 413 | * |
||
| 414 | * @throws QueryException |
||
| 415 | * @throws DBGoneAwayException |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | 2 | private function queryErrorHandling($errorMsg, $sql) |
|
| 447 | |||
| 448 | } |
||
| 449 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.