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 |
||
| 32 | class Connection extends \Doctrine\DBAL\Connection |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var bool $safe true means it is safe to write to database |
||
| 36 | * removed allowedWebChanges as unnecessary. Using this instead. |
||
| 37 | */ |
||
| 38 | protected $safe; |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool $force true means force writing to database even if safe is not true. |
||
| 43 | */ |
||
| 44 | protected $force; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var bool $transactionActive true means a transaction is in process. |
||
| 48 | */ |
||
| 49 | protected $transactionActive; |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * this is a public setter for the safe variable |
||
| 54 | * |
||
| 55 | * @param bool $safe set safe to true if safe to write data to database |
||
| 56 | * |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | 28 | public function setSafe($safe = true) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * this is a public getter for the safe variable |
||
| 66 | * |
||
| 67 | * @return boolean |
||
| 68 | */ |
||
| 69 | 1 | public function getSafe() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * this is a public setter for the $force variable |
||
| 76 | * |
||
| 77 | * @param bool $force when true will force a write to database when safe is false. |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | 10 | public function setForce($force = false) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * this is a public getter for the $force variable |
||
| 88 | * |
||
| 89 | * @return boolean |
||
| 90 | */ |
||
| 91 | 1 | public function getForce() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Initializes a new instance of the Connection class. |
||
| 98 | * |
||
| 99 | * This sets up necessary variables before calling parent constructor |
||
| 100 | * |
||
| 101 | * @param array $params Parameters for the driver |
||
| 102 | * @param Driver $driver The driver to use |
||
| 103 | * @param Configuration $config The connection configuration |
||
| 104 | * @param EventManager $eventManager Event manager to use |
||
| 105 | */ |
||
| 106 | 18 | public function __construct( |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Prepend the prefix.'_' to the given tablename |
||
| 128 | * If tablename is empty, just return the prefix. |
||
| 129 | * |
||
| 130 | * @param string $tablename tablename |
||
| 131 | * |
||
| 132 | * @return string prefixed tablename, or prefix if tablename is empty |
||
| 133 | */ |
||
| 134 | 138 | public function prefix($tablename = '') |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Inserts a table row with specified data. |
||
| 146 | * |
||
| 147 | * Adds prefix to the name of the table then passes to normal function. |
||
| 148 | * |
||
| 149 | * @param string $tableName The name of the table to insert data into. |
||
| 150 | * @param array $data An associative array containing column-value pairs. |
||
| 151 | * @param array $types Types of the inserted data. |
||
| 152 | * |
||
| 153 | * @return integer The number of affected rows. |
||
| 154 | */ |
||
| 155 | 2 | public function insertPrefix($tableName, array $data, array $types = array()) |
|
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * Executes an SQL UPDATE statement on a table. |
||
| 164 | * |
||
| 165 | * Adds prefix to the name of the table then passes to normal function. |
||
| 166 | * |
||
| 167 | * @param string $tableName The name of the table to update. |
||
| 168 | * @param array $data The data to update |
||
| 169 | * @param array $identifier The update criteria. |
||
| 170 | * An associative array containing column-value pairs. |
||
| 171 | * @param array $types Types of the merged $data and |
||
| 172 | * $identifier arrays in that order. |
||
| 173 | * |
||
| 174 | * @return integer The number of affected rows. |
||
| 175 | */ |
||
| 176 | 1 | public function updatePrefix($tableName, array $data, array $identifier, array $types = array()) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Executes an SQL DELETE statement on a table. |
||
| 184 | * |
||
| 185 | * Adds prefix to the name of the table then passes to delete function. |
||
| 186 | * |
||
| 187 | * @param string $tableName The name of the table on which to delete. |
||
| 188 | * @param array $identifier The deletion criteria. |
||
| 189 | * An associative array containing column-value pairs. |
||
| 190 | * |
||
| 191 | * @return integer The number of affected rows. |
||
| 192 | * |
||
| 193 | */ |
||
| 194 | 1 | public function deletePrefix($tableName, array $identifier) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Executes an, optionally parametrized, SQL query. |
||
| 202 | * |
||
| 203 | * If the query is parametrized, a prepared statement is used. |
||
| 204 | * If an SQLLogger is configured, the execution is logged. |
||
| 205 | * |
||
| 206 | * @param string $query The SQL query to execute. |
||
| 207 | * @param array $params The parameters to bind to the query, if any. |
||
| 208 | * @param array $types The types the previous parameters are in. |
||
| 209 | * @param QueryCacheProfile $qcp The query Cache profile |
||
| 210 | * |
||
| 211 | * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
||
| 212 | * |
||
| 213 | * @internal PERF: Directly prepares a driver statement, not a wrapper. |
||
| 214 | */ |
||
| 215 | 76 | public function executeQuery( |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
||
| 226 | * and returns the number of affected rows. |
||
| 227 | * |
||
| 228 | * This method supports PDO binding types as well as DBAL mapping types. |
||
| 229 | * |
||
| 230 | * This over ridding process checks to make sure it is safe to do these. |
||
| 231 | * If force is active then it will over ride the safe setting. |
||
| 232 | * |
||
| 233 | * @param string $query The SQL query. |
||
| 234 | * @param array $params The query parameters. |
||
| 235 | * @param array $types The parameter types. |
||
| 236 | * |
||
| 237 | * @return integer The number of affected rows. |
||
| 238 | * |
||
| 239 | * @internal PERF: Directly prepares a driver statement, not a wrapper. |
||
| 240 | * |
||
| 241 | * @todo build a better exception catching system. |
||
| 242 | */ |
||
| 243 | 21 | public function executeUpdate($query, array $params = array(), array $types = array()) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Starts a transaction by suspending auto-commit mode. |
||
| 273 | * |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | public function beginTransaction() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Commits the current transaction. |
||
| 284 | * |
||
| 285 | * @return void |
||
| 286 | */ |
||
| 287 | public function commit() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * rolls back the current transaction. |
||
| 296 | * |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | public function rollBack() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * perform a safe query if allowed |
||
| 308 | * can receive variable number of arguments |
||
| 309 | * |
||
| 310 | * @return mixed returns the value received or null if nothing received. |
||
| 311 | * |
||
| 312 | * @todo add error report for using non select while not safe. |
||
| 313 | * @todo need to check if doctrine allows more than one query to be sent. |
||
| 314 | * This code assumes only one query is sent and anything else sent is not |
||
| 315 | * a query. This will have to be readdressed if this is wrong. |
||
| 316 | * |
||
| 317 | */ |
||
| 318 | public function query() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * perform queries from SQL dump file in a batch |
||
| 348 | * |
||
| 349 | * @param string $file file path to an SQL dump file |
||
| 350 | * |
||
| 351 | * @return bool FALSE if failed reading SQL file or |
||
| 352 | * TRUE if the file has been read and queries executed |
||
| 353 | */ |
||
| 354 | View Code Duplication | public function queryFromFile($file) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Create a new instance of a SQL query builder. |
||
| 372 | * |
||
| 373 | * @return QueryBuilder |
||
| 374 | */ |
||
| 375 | 89 | public function createXoopsQueryBuilder() |
|
| 379 | } |
||
| 380 |