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 |
||
| 10 | final class Prepare extends \mysqli_stmt |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | private $_sql; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var DB |
||
| 20 | */ |
||
| 21 | private $_db; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Debug |
||
| 25 | */ |
||
| 26 | private $_debug; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Prepare constructor. |
||
| 30 | * |
||
| 31 | * @param DB $db |
||
| 32 | * @param string $query |
||
| 33 | */ |
||
| 34 | 2 | public function __construct(DB $db, $query) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Executes a prepared Query |
||
| 46 | * |
||
| 47 | * @link http://php.net/manual/en/mysqli-stmt.execute.php |
||
| 48 | * @return bool true on success or false on failure. |
||
| 49 | * @since 5.0 |
||
| 50 | */ |
||
| 51 | 2 | public function execute() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Error-handling for the sql-query. |
||
| 68 | * |
||
| 69 | * @param string $errorMsg |
||
| 70 | * @param string $sql |
||
| 71 | * |
||
| 72 | * @throws \Exception |
||
| 73 | */ |
||
| 74 | 1 | View Code Duplication | protected function queryErrorHandling($errorMsg, $sql) |
| 100 | |||
| 101 | /** |
||
| 102 | * Prepare an SQL statement for execution |
||
| 103 | * |
||
| 104 | * @link http://php.net/manual/en/mysqli-stmt.prepare.php |
||
| 105 | * |
||
| 106 | * @param string $query <p> |
||
| 107 | * The query, as a string. It must consist of a single SQL statement. |
||
| 108 | * </p> |
||
| 109 | * <p> |
||
| 110 | * You can include one or more parameter markers in the SQL statement by |
||
| 111 | * embedding question mark (?) characters at the |
||
| 112 | * appropriate positions. |
||
| 113 | * </p> |
||
| 114 | * <p> |
||
| 115 | * You should not add a terminating semicolon or \g |
||
| 116 | * to the statement. |
||
| 117 | * </p> |
||
| 118 | * <p> |
||
| 119 | * The markers are legal only in certain places in SQL statements. |
||
| 120 | * For example, they are allowed in the VALUES() list of an INSERT statement |
||
| 121 | * (to specify column values for a row), or in a comparison with a column in |
||
| 122 | * a WHERE clause to specify a comparison value. |
||
| 123 | * </p> |
||
| 124 | * <p> |
||
| 125 | * However, they are not allowed for identifiers (such as table or column names), |
||
| 126 | * in the select list that names the columns to be returned by a SELECT statement), |
||
| 127 | * or to specify both operands of a binary operator such as the = |
||
| 128 | * equal sign. The latter restriction is necessary because it would be impossible |
||
| 129 | * to determine the parameter type. In general, parameters are legal only in Data |
||
| 130 | * Manipulation Language (DML) statements, and not in Data Definition Language |
||
| 131 | * (DDL) statements. |
||
| 132 | * </p> |
||
| 133 | * |
||
| 134 | * @return mixed true on success or false on failure. |
||
| 135 | * @since 5.0 |
||
| 136 | */ |
||
| 137 | 2 | public function prepare($query) |
|
| 159 | } |
||
| 160 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.