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 |
||
| 44 | class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { |
||
| 45 | /** |
||
| 46 | * @var string $tablePrefix |
||
| 47 | */ |
||
| 48 | protected $tablePrefix; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var \OC\DB\Adapter $adapter |
||
| 52 | */ |
||
| 53 | protected $adapter; |
||
| 54 | |||
| 55 | protected $lockedTable = null; |
||
| 56 | |||
| 57 | public function connect() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns a QueryBuilder for the connection. |
||
| 68 | * |
||
| 69 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
| 70 | */ |
||
| 71 | public function getQueryBuilder() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Gets the QueryBuilder for the connection. |
||
| 77 | * |
||
| 78 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
| 79 | * @deprecated please use $this->getQueryBuilder() instead |
||
| 80 | */ |
||
| 81 | public function createQueryBuilder() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Gets the ExpressionBuilder for the connection. |
||
| 89 | * |
||
| 90 | * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
||
| 91 | * @deprecated please use $this->getQueryBuilder()->expr() instead |
||
| 92 | */ |
||
| 93 | public function getExpressionBuilder() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the file and line that called the method where `getCallerBacktrace()` was used |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | protected function getCallerBacktrace() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function getPrefix() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Initializes a new instance of the Connection class. |
||
| 125 | * |
||
| 126 | * @param array $params The connection parameters. |
||
| 127 | * @param \Doctrine\DBAL\Driver $driver |
||
| 128 | * @param \Doctrine\DBAL\Configuration $config |
||
| 129 | * @param \Doctrine\Common\EventManager $eventManager |
||
| 130 | * @throws \Exception |
||
| 131 | */ |
||
| 132 | public function __construct(array $params, Driver $driver, Configuration $config = null, |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Prepares an SQL statement. |
||
| 150 | * |
||
| 151 | * @param string $statement The SQL statement to prepare. |
||
| 152 | * @param int $limit |
||
| 153 | * @param int $offset |
||
| 154 | * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
||
| 155 | */ |
||
| 156 | public function prepare( $statement, $limit=null, $offset=null ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Executes an, optionally parametrized, SQL query. |
||
| 175 | * |
||
| 176 | * If the query is parametrized, a prepared statement is used. |
||
| 177 | * If an SQLLogger is configured, the execution is logged. |
||
| 178 | * |
||
| 179 | * @param string $query The SQL query to execute. |
||
| 180 | * @param array $params The parameters to bind to the query, if any. |
||
| 181 | * @param array $types The types the previous parameters are in. |
||
| 182 | * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
||
| 183 | * |
||
| 184 | * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
||
| 185 | * |
||
| 186 | * @throws \Doctrine\DBAL\DBALException |
||
| 187 | */ |
||
| 188 | public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
||
| 197 | * and returns the number of affected rows. |
||
| 198 | * |
||
| 199 | * This method supports PDO binding types as well as DBAL mapping types. |
||
| 200 | * |
||
| 201 | * @param string $query The SQL query. |
||
| 202 | * @param array $params The query parameters. |
||
| 203 | * @param array $types The parameter types. |
||
| 204 | * |
||
| 205 | * @return integer The number of affected rows. |
||
| 206 | * |
||
| 207 | * @throws \Doctrine\DBAL\DBALException |
||
| 208 | */ |
||
| 209 | public function executeUpdate($query, array $params = [], array $types = []) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the ID of the last inserted row, or the last value from a sequence object, |
||
| 218 | * depending on the underlying driver. |
||
| 219 | * |
||
| 220 | * Note: This method may not return a meaningful or consistent result across different drivers, |
||
| 221 | * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
||
| 222 | * columns or sequences. |
||
| 223 | * |
||
| 224 | * @param string $seqName Name of the sequence object from which the ID should be returned. |
||
| 225 | * @return string A string representation of the last inserted ID. |
||
| 226 | */ |
||
| 227 | public function lastInsertId($seqName = null) { |
||
| 233 | |||
| 234 | // internal use |
||
| 235 | public function realLastInsertId($seqName = null) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Insert a row if the matching row does not exists. |
||
| 241 | * |
||
| 242 | * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
||
| 243 | * @param array $input data that should be inserted into the table (column name => value) |
||
| 244 | * @param array|null $compare List of values that should be checked for "if not exists" |
||
| 245 | * If this is null or an empty array, all keys of $input will be compared |
||
| 246 | * Please note: text fields (clob) must not be used in the compare array |
||
| 247 | * @return int number of inserted rows |
||
| 248 | * @throws \Doctrine\DBAL\DBALException |
||
| 249 | */ |
||
| 250 | public function insertIfNotExist($table, $input, array $compare = null) { |
||
| 253 | |||
| 254 | private function getType($value) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Insert or update a row value |
||
| 266 | * |
||
| 267 | * @param string $table |
||
| 268 | * @param array $keys (column name => value) |
||
| 269 | * @param array $values (column name => value) |
||
| 270 | * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
||
| 271 | * @return int number of new rows |
||
| 272 | * @throws \Doctrine\DBAL\DBALException |
||
| 273 | * @throws PreConditionNotMetException |
||
| 274 | */ |
||
| 275 | public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Create an exclusive read+write lock on a table |
||
| 314 | * |
||
| 315 | * @param string $tableName |
||
| 316 | * @throws \BadMethodCallException When trying to acquire a second lock |
||
| 317 | * @since 9.1.0 |
||
| 318 | */ |
||
| 319 | public function lockTable($tableName) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Release a previous acquired lock again |
||
| 331 | * |
||
| 332 | * @since 9.1.0 |
||
| 333 | */ |
||
| 334 | public function unlockTable() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * returns the error code and message as a string for logging |
||
| 341 | * works with DoctrineException |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | public function getError() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Drop a table from the database if it exists |
||
| 357 | * |
||
| 358 | * @param string $table table name without the prefix |
||
| 359 | */ |
||
| 360 | View Code Duplication | public function dropTable($table) { |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Check if a table exists |
||
| 370 | * |
||
| 371 | * @param string $table table name without the prefix |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | public function tableExists($table){ |
||
| 379 | |||
| 380 | // internal use |
||
| 381 | /** |
||
| 382 | * @param string $statement |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | protected function replaceTablePrefix($statement) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Check if a transaction is active |
||
| 391 | * |
||
| 392 | * @return bool |
||
| 393 | * @since 8.2.0 |
||
| 394 | */ |
||
| 395 | public function inTransaction() { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Espace a parameter to be used in a LIKE query |
||
| 401 | * |
||
| 402 | * @param string $param |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function escapeLikeParameter($param) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Create the schema of the connected database |
||
| 411 | * |
||
| 412 | * @return Schema |
||
| 413 | */ |
||
| 414 | public function createSchema() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Migrate the database to the given schema |
||
| 422 | * |
||
| 423 | * @param Schema $toSchema |
||
| 424 | */ |
||
| 425 | public function migrateToSchema(Schema $toSchema) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Are 4-byte characters allowed or only 3-byte |
||
| 433 | * |
||
| 434 | * @return bool |
||
| 435 | * @since 10.0 |
||
| 436 | */ |
||
| 437 | public function allows4ByteCharacters() { |
||
| 446 | } |
||
| 447 |
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.