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:
Complex classes like Connection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { |
||
| 46 | /** |
||
| 47 | * @var string $tablePrefix |
||
| 48 | */ |
||
| 49 | protected $tablePrefix; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \OC\DB\Adapter $adapter |
||
| 53 | */ |
||
| 54 | protected $adapter; |
||
| 55 | |||
| 56 | protected $lockedTable = null; |
||
| 57 | |||
| 58 | public function connect() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns a QueryBuilder for the connection. |
||
| 69 | * |
||
| 70 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 71 | */ |
||
| 72 | public function getQueryBuilder() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Gets the QueryBuilder for the connection. |
||
| 78 | * |
||
| 79 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
| 80 | * @deprecated please use $this->getQueryBuilder() instead |
||
| 81 | */ |
||
| 82 | public function createQueryBuilder() { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Gets the ExpressionBuilder for the connection. |
||
| 90 | * |
||
| 91 | * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
||
| 92 | * @deprecated please use $this->getQueryBuilder()->expr() instead |
||
| 93 | */ |
||
| 94 | public function getExpressionBuilder() { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get the file and line that called the method where `getCallerBacktrace()` was used |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | protected function getCallerBacktrace() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getPrefix() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Initializes a new instance of the Connection class. |
||
| 126 | * |
||
| 127 | * @param array $params The connection parameters. |
||
| 128 | * @param \Doctrine\DBAL\Driver $driver |
||
| 129 | * @param \Doctrine\DBAL\Configuration $config |
||
| 130 | * @param \Doctrine\Common\EventManager $eventManager |
||
| 131 | * @throws \Exception |
||
| 132 | */ |
||
| 133 | public function __construct(array $params, Driver $driver, Configuration $config = null, |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Prepares an SQL statement. |
||
| 151 | * |
||
| 152 | * @param string $statement The SQL statement to prepare. |
||
| 153 | * @param int $limit |
||
| 154 | * @param int $offset |
||
| 155 | * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
||
| 156 | */ |
||
| 157 | public function prepare( $statement, $limit=null, $offset=null ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Executes an, optionally parametrized, SQL query. |
||
| 173 | * |
||
| 174 | * If the query is parametrized, a prepared statement is used. |
||
| 175 | * If an SQLLogger is configured, the execution is logged. |
||
| 176 | * |
||
| 177 | * @param string $query The SQL query to execute. |
||
| 178 | * @param array $params The parameters to bind to the query, if any. |
||
| 179 | * @param array $types The types the previous parameters are in. |
||
| 180 | * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
||
| 181 | * |
||
| 182 | * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
||
| 183 | * |
||
| 184 | * @throws \Doctrine\DBAL\DBALException |
||
| 185 | */ |
||
| 186 | public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
||
| 195 | * and returns the number of affected rows. |
||
| 196 | * |
||
| 197 | * This method supports PDO binding types as well as DBAL mapping types. |
||
| 198 | * |
||
| 199 | * @param string $query The SQL query. |
||
| 200 | * @param array $params The query parameters. |
||
| 201 | * @param array $types The parameter types. |
||
| 202 | * |
||
| 203 | * @return integer The number of affected rows. |
||
| 204 | * |
||
| 205 | * @throws \Doctrine\DBAL\DBALException |
||
| 206 | */ |
||
| 207 | public function executeUpdate($query, array $params = [], array $types = []) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Returns the ID of the last inserted row, or the last value from a sequence object, |
||
| 216 | * depending on the underlying driver. |
||
| 217 | * |
||
| 218 | * Note: This method may not return a meaningful or consistent result across different drivers, |
||
| 219 | * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
||
| 220 | * columns or sequences. |
||
| 221 | * |
||
| 222 | * @param string $seqName Name of the sequence object from which the ID should be returned. |
||
| 223 | * @return string A string representation of the last inserted ID. |
||
| 224 | */ |
||
| 225 | public function lastInsertId($seqName = null) { |
||
| 231 | |||
| 232 | // internal use |
||
| 233 | public function realLastInsertId($seqName = null) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Insert a row if the matching row does not exists. |
||
| 239 | * |
||
| 240 | * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
||
| 241 | * @param array $input data that should be inserted into the table (column name => value) |
||
| 242 | * @param array|null $compare List of values that should be checked for "if not exists" |
||
| 243 | * If this is null or an empty array, all keys of $input will be compared |
||
| 244 | * Please note: text fields (clob) must not be used in the compare array |
||
| 245 | * @return int number of inserted rows |
||
| 246 | * @throws \Doctrine\DBAL\DBALException |
||
| 247 | */ |
||
| 248 | public function insertIfNotExist($table, $input, array $compare = null) { |
||
| 251 | |||
| 252 | private function getType($value) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Insert or update a row value |
||
| 264 | * |
||
| 265 | * @param string $table |
||
| 266 | * @param array $keys (column name => value) |
||
| 267 | * @param array $values (column name => value) |
||
| 268 | * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
||
| 269 | * @return int number of new rows |
||
| 270 | * @throws \Doctrine\DBAL\DBALException |
||
| 271 | * @throws PreConditionNotMetException |
||
| 272 | */ |
||
| 273 | public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Create an exclusive read+write lock on a table |
||
| 312 | * |
||
| 313 | * @param string $tableName |
||
| 314 | * @throws \BadMethodCallException When trying to acquire a second lock |
||
| 315 | * @since 9.1.0 |
||
| 316 | */ |
||
| 317 | public function lockTable($tableName) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Release a previous acquired lock again |
||
| 329 | * |
||
| 330 | * @since 9.1.0 |
||
| 331 | */ |
||
| 332 | public function unlockTable() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * returns the error code and message as a string for logging |
||
| 339 | * works with DoctrineException |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getError() { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Drop a table from the database if it exists |
||
| 355 | * |
||
| 356 | * @param string $table table name without the prefix |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function dropTable($table) { |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Check if a table exists |
||
| 368 | * |
||
| 369 | * @param string $table table name without the prefix |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | public function tableExists($table){ |
||
| 377 | |||
| 378 | // internal use |
||
| 379 | /** |
||
| 380 | * @param string $statement |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | protected function replaceTablePrefix($statement) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Check if a transaction is active |
||
| 389 | * |
||
| 390 | * @return bool |
||
| 391 | * @since 8.2.0 |
||
| 392 | */ |
||
| 393 | public function inTransaction() { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Espace a parameter to be used in a LIKE query |
||
| 399 | * |
||
| 400 | * @param string $param |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function escapeLikeParameter($param) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Create the schema of the connected database |
||
| 409 | * |
||
| 410 | * @return Schema |
||
| 411 | */ |
||
| 412 | public function createSchema() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Migrate the database to the given schema |
||
| 420 | * |
||
| 421 | * @param Schema $toSchema |
||
| 422 | */ |
||
| 423 | public function migrateToSchema(Schema $toSchema) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Are 4-byte characters allowed or only 3-byte |
||
| 431 | * |
||
| 432 | * @return bool |
||
| 433 | * @since 10.0 |
||
| 434 | */ |
||
| 435 | public function allows4ByteCharacters() { |
||
| 444 | } |
||
| 445 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.