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 | class Sql |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @const ERR_QUERY_BAD_REQUEST Exception code if the request executed |
||
| 18 | * on query method have an error. |
||
| 19 | */ |
||
| 20 | const ERR_QUERY_BAD_REQUEST = 2103001; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var \BfwSql\SqlConnect $sqlConnect SqlConnect object |
||
| 24 | */ |
||
| 25 | protected $sqlConnect; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string $prefix Tables prefix |
||
| 29 | */ |
||
| 30 | protected $prefix = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructor |
||
| 34 | * |
||
| 35 | * @param \BfwSql\SqlConnect $sqlConnect SqlConnect instance |
||
| 36 | * |
||
| 37 | * @throws \Exception |
||
| 38 | */ |
||
| 39 | public function __construct(\BfwSql\SqlConnect $sqlConnect) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Getter to the property sqlConnect |
||
| 47 | * |
||
| 48 | * @return \BfwSql\SqlConnect |
||
| 49 | */ |
||
| 50 | public function getSqlConnect() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Getter to the property prefix |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function getPrefix() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get the id for the last item has been insert in database |
||
| 67 | * |
||
| 68 | * @param string|null $name (default: null) Name of the sequence for the id |
||
| 69 | * Used for SGDB like PostgreSQL. Not use it for mysql. |
||
| 70 | * |
||
| 71 | * @return integer |
||
| 72 | */ |
||
| 73 | public function obtainLastInsertedId($name = null) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get the id for the last item has been insert in database for a table |
||
| 80 | * without auto-increment |
||
| 81 | * |
||
| 82 | * @param string $table The table name |
||
| 83 | * @param string $colId The column name for the ID |
||
| 84 | * @param string|array $order Columns to sort table content |
||
| 85 | * @param string|array $where All where instruction used for filter content |
||
| 86 | * |
||
| 87 | * @return integer |
||
| 88 | */ |
||
| 89 | public function obtainLastInsertedIdWithoutAI( |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return a new instance of SqlSelect |
||
| 127 | * |
||
| 128 | * @param string $type (default: "array") Return PHP type |
||
| 129 | * Possible value : "array" or "object" |
||
| 130 | * |
||
| 131 | * @return \BfwSql\SqlSelect |
||
| 132 | */ |
||
| 133 | public function select($type = 'array') |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Return a new instance of SqlInsert |
||
| 143 | * |
||
| 144 | * @param string $table The table concerned by the request |
||
| 145 | * @param array $columns (default: null) All datas to add |
||
| 146 | * Format is array('columnName' => 'value', ...); |
||
| 147 | * @param string $quoteStatus (default: QUOTE_ALL) Status to automatic |
||
| 148 | * quoted string value system. |
||
| 149 | * |
||
| 150 | * @return \BfwSql\SqlInsert |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function insert( |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Return a new instance of SqlUpdate |
||
| 170 | * |
||
| 171 | * @param string $table The table concerned by the request |
||
| 172 | * @param array $columns (default: null) All datas to update |
||
| 173 | * Format is array('columnName' => 'newValue', ...); |
||
| 174 | * @param string $quoteStatus (default: QUOTE_ALL) Status to automatic |
||
| 175 | * quoted string value system. |
||
| 176 | * |
||
| 177 | * @return \BfwSql\SqlUpdate |
||
| 178 | */ |
||
| 179 | View Code Duplication | public function update( |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Return a new instance of SqlDelete |
||
| 197 | * |
||
| 198 | * @param string $table The table concerned by the request |
||
| 199 | * |
||
| 200 | * @return \BfwSql\SqlDelete |
||
| 201 | */ |
||
| 202 | public function delete($table) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Find the first vacant id on a table and for a column |
||
| 212 | * |
||
| 213 | * @param string $table The table concerned by the request |
||
| 214 | * @param string $column The id column. Must be an integer.. |
||
| 215 | * |
||
| 216 | * @throws \Exception If a error has been throw during the search |
||
| 217 | * |
||
| 218 | * @return integer |
||
| 219 | */ |
||
| 220 | public function createId($table, $column) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Run the query in parameter |
||
| 256 | * |
||
| 257 | * @param string $request The request to run |
||
| 258 | * |
||
| 259 | * @throws \Exception If the request has failed |
||
| 260 | * |
||
| 261 | * @return \PDOStatement |
||
| 262 | */ |
||
| 263 | public function query($request) |
||
| 294 | } |
||
| 295 |
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.