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 | 5 | public function __construct(DB $db, $query) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Prepare destructor. |
||
| 63 | */ |
||
| 64 | 5 | 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 | 3 | private function _buildArguments() |
|
| 76 | { |
||
| 77 | 3 | $arguments = array(); |
|
| 78 | 3 | $arguments[0] = ''; |
|
| 79 | |||
| 80 | 3 | foreach ($this->_boundParams as $param) { |
|
| 81 | 3 | $arguments[0] .= $param['type']; |
|
| 82 | 3 | $arguments[] = &$param['value']; |
|
| 83 | 3 | } |
|
| 84 | |||
| 85 | 3 | return $arguments; |
|
| 86 | } |
||
| 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 | 3 | private function _prepareValue(&$value, $type) |
|
| 98 | { |
||
| 99 | /** @noinspection ReferenceMismatchInspection */ |
||
| 100 | 3 | $value = $this->_db->escape($value); |
|
| 101 | |||
| 102 | 3 | if ('s' === $type) { |
|
| 103 | 2 | $valueForSqlWithBoundParameters = "'" . $value . "'"; |
|
| 104 | 2 | } else { |
|
| 105 | 1 | $valueForSqlWithBoundParameters = $value; |
|
| 106 | } |
||
| 107 | |||
| 108 | 3 | return array($value, $valueForSqlWithBoundParameters); |
|
| 109 | } |
||
| 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 | 3 | 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) |
|
| 170 | { |
||
| 171 | 3 | $this->_use_bound_parameters_interpolated = true; |
|
| 172 | |||
| 173 | // debug_backtrace returns arguments by reference, see comments at http://php.net/manual/de/function.func-get-args.php |
||
| 174 | 3 | if (Bootup::is_php('5.4')) { |
|
| 175 | 3 | $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1); |
|
| 176 | 3 | } else { |
|
| 177 | $trace = debug_backtrace(); |
||
| 178 | } |
||
| 179 | |||
| 180 | 3 | $args = &$trace[0]['args']; |
|
| 181 | 3 | $types = str_split($types); |
|
| 182 | |||
| 183 | 3 | $args_count = count($args) - 1; |
|
| 184 | 3 | $types_count = count($types); |
|
| 185 | |||
| 186 | 3 | if ($args_count !== $types_count) { |
|
| 187 | trigger_error('Number of variables doesn\'t match number of parameters in prepared statement', E_WARNING); |
||
| 188 | |||
| 189 | return false; |
||
| 190 | } |
||
| 191 | |||
| 192 | 3 | $arg = 1; |
|
| 193 | 3 | foreach ($types as $typeInner) { |
|
| 194 | 3 | $val = &$args[$arg]; |
|
| 195 | 3 | $this->_boundParams[] = array( |
|
| 196 | 3 | 'type' => $typeInner, |
|
| 197 | 3 | 'value' => &$val, |
|
| 198 | ); |
||
| 199 | 3 | $arg++; |
|
| 200 | 3 | } |
|
| 201 | |||
| 202 | 3 | return true; |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Executes a prepared Query |
||
| 207 | * |
||
| 208 | * @link http://php.net/manual/en/mysqli-stmt.execute.php |
||
| 209 | * @return bool true on success or false on failure. |
||
| 210 | * @since 5.0 |
||
| 211 | */ |
||
| 212 | 5 | public function execute() |
|
| 213 | { |
||
| 214 | 5 | if ($this->_use_bound_parameters_interpolated === true) { |
|
| 215 | 3 | $this->interpolateQuery(); |
|
| 216 | 3 | call_user_func_array(array('parent', 'bind_param'), $this->_buildArguments()); |
|
| 217 | 3 | } |
|
| 218 | |||
| 219 | 5 | $query_start_time = microtime(true); |
|
| 220 | 5 | $result = parent::execute(); |
|
| 221 | 5 | $query_duration = microtime(true) - $query_start_time; |
|
| 222 | |||
| 223 | 5 | $this->_debug->logQuery($this->_sql_with_bound_parameters, $query_duration, $this->num_rows); |
|
| 224 | |||
| 225 | 5 | if ($result === true) { |
|
| 226 | |||
| 227 | 3 | if (preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE)\s+/i', $this->_sql)) { |
|
| 228 | |||
| 229 | // it is an "INSERT" || "REPLACE" |
||
| 230 | 3 | if ($this->insert_id > 0) { |
|
| 231 | 3 | return (int)$this->insert_id; |
|
| 232 | } |
||
| 233 | |||
| 234 | // it is an "UPDATE" || "DELETE" |
||
| 235 | if ($this->affected_rows > 0) { |
||
| 236 | return (int)$this->affected_rows; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | return true; |
||
| 241 | } |
||
| 242 | |||
| 243 | 2 | return $this->queryErrorHandling($this->error, $this->_sql_with_bound_parameters); |
|
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Prepare an SQL statement for execution |
||
| 248 | * |
||
| 249 | * @link http://php.net/manual/en/mysqli-stmt.prepare.php |
||
| 250 | * |
||
| 251 | * @param string $query <p> |
||
| 252 | * The query, as a string. It must consist of a single SQL statement. |
||
| 253 | * </p> |
||
| 254 | * <p> |
||
| 255 | * You can include one or more parameter markers in the SQL statement by |
||
| 256 | * embedding question mark (?) characters at the |
||
| 257 | * appropriate positions. |
||
| 258 | * </p> |
||
| 259 | * <p> |
||
| 260 | * You should not add a terminating semicolon or \g |
||
| 261 | * to the statement. |
||
| 262 | * </p> |
||
| 263 | * <p> |
||
| 264 | * The markers are legal only in certain places in SQL statements. |
||
| 265 | * For example, they are allowed in the VALUES() list of an INSERT statement |
||
| 266 | * (to specify column values for a row), or in a comparison with a column in |
||
| 267 | * a WHERE clause to specify a comparison value. |
||
| 268 | * </p> |
||
| 269 | * <p> |
||
| 270 | * However, they are not allowed for identifiers (such as table or column names), |
||
| 271 | * in the select list that names the columns to be returned by a SELECT statement), |
||
| 272 | * or to specify both operands of a binary operator such as the = |
||
| 273 | * equal sign. The latter restriction is necessary because it would be impossible |
||
| 274 | * to determine the parameter type. In general, parameters are legal only in Data |
||
| 275 | * Manipulation Language (DML) statements, and not in Data Definition Language |
||
| 276 | * (DDL) statements. |
||
| 277 | * </p> |
||
| 278 | * |
||
| 279 | * @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 280 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 281 | * "true" by e.g. "SELECT"-queries<br /> |
||
| 282 | * "false" on error |
||
| 283 | * @since 5.0 |
||
| 284 | */ |
||
| 285 | 5 | public function prepare($query) |
|
| 286 | { |
||
| 287 | 5 | $this->_sql = $query; |
|
| 288 | 5 | $this->_sql_with_bound_parameters = $query; |
|
| 289 | |||
| 290 | 5 | if (!$this->_db->isReady()) { |
|
| 291 | return false; |
||
| 292 | } |
||
| 293 | |||
| 294 | 5 | View Code Duplication | if (!$query || $query === '') { |
| 295 | $this->_debug->displayError('Can\'t prepare an empty Query', false); |
||
| 296 | |||
| 297 | return false; |
||
| 298 | } |
||
| 299 | |||
| 300 | 5 | $bool = parent::prepare($query); |
|
| 301 | |||
| 302 | 5 | if ($bool === false) { |
|
| 303 | 2 | $this->_debug->displayError('Can\'t prepare Query: ' . $query . ' | ' . $this->error, false); |
|
| 304 | 2 | } |
|
| 305 | |||
| 306 | 5 | return true; |
|
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Ger the bound parameters from sql-query as array, if you use the "$this->bind_param_debug()" method. |
||
| 311 | * |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | public function get_bound_params() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function get_sql() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get the sql-query with bound parameters, if you use the "$this->bind_param_debug()" method. |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | 2 | public function get_sql_with_bound_parameters() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @return int |
||
| 339 | */ |
||
| 340 | public function insert_id() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Copies $this->_sql then replaces bound markers with associated values ($this->_sql is not modified |
||
| 347 | * but the resulting query string is assigned to $this->sql_bound_parameters) |
||
| 348 | * |
||
| 349 | * @return string $testQuery - interpolated db query string |
||
| 350 | */ |
||
| 351 | 3 | private function interpolateQuery() |
|
| 352 | { |
||
| 353 | 3 | $testQuery = $this->_sql; |
|
| 354 | 3 | if ($this->_boundParams) { |
|
| 355 | 3 | foreach ($this->_boundParams as &$param) { |
|
| 356 | 3 | $type = &$param['type']; |
|
| 357 | 3 | $value = &$param['value']; |
|
| 358 | 3 | $values = $this->_prepareValue($value, $type); |
|
| 359 | |||
| 360 | // set new values |
||
| 361 | 3 | $param['value'] = $values[0]; |
|
| 362 | // we need to replace the question mark "?" here |
||
| 363 | 3 | $values[1] = str_replace('?', '###simple_mysqli__prepare_question_mark###', $values[1]); |
|
| 364 | // build the query (only for debugging) |
||
| 365 | 3 | $testQuery = preg_replace("/\?/", $values[1], $testQuery, 1); |
|
| 366 | 3 | } |
|
| 367 | 3 | unset($param); |
|
| 368 | 3 | $testQuery = str_replace('###simple_mysqli__prepare_question_mark###', '?', $testQuery); |
|
| 369 | 3 | } |
|
| 370 | 3 | $this->_sql_with_bound_parameters = $testQuery; |
|
| 371 | |||
| 372 | 3 | return $testQuery; |
|
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Error-handling for the sql-query. |
||
| 377 | * |
||
| 378 | * @param string $errorMsg |
||
| 379 | * @param string $sql |
||
| 380 | * |
||
| 381 | * @throws \Exception |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | 2 | View Code Duplication | private function queryErrorHandling($errorMsg, $sql) |
| 413 | |||
| 414 | } |
||
| 415 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.