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 ConnectionHandler 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 ConnectionHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class ConnectionHandler implements DatabaseHandler |
||
|
|||
22 | { |
||
23 | /** @var \Doctrine\DBAL\Connection */ |
||
24 | protected $connection; |
||
25 | |||
26 | /** |
||
27 | * @param string|array $dsn |
||
28 | * |
||
29 | * @return \Doctrine\DBAL\Connection |
||
30 | */ |
||
31 | public static function createConnectionFromDSN($dsn) |
||
44 | |||
45 | /** |
||
46 | * Create a Connection Handler from given Doctrine $connection. |
||
47 | * |
||
48 | * @param \Doctrine\DBAL\Connection $connection |
||
49 | * |
||
50 | * @return \eZ\Publish\Core\Persistence\Doctrine\ConnectionHandler |
||
51 | */ |
||
52 | public static function createFromConnection(Connection $connection) |
||
66 | |||
67 | /** |
||
68 | * Create a Connection Handler with corresponding Doctrine connection from DSN. |
||
69 | * |
||
70 | * @param string|array $dsn |
||
71 | * |
||
72 | * @return ConnectionHandler |
||
73 | */ |
||
74 | public static function createFromDSN($dsn) |
||
94 | |||
95 | /** |
||
96 | * Returns the Data Source Name as a structure containing the various parts of the DSN. |
||
97 | * |
||
98 | * Additional keys can be added by appending a URI query string to the |
||
99 | * end of the DSN. |
||
100 | * |
||
101 | * The format of the supplied DSN is in its fullest form: |
||
102 | * <code> |
||
103 | * driver://user:password@protocol+host/database?option=8&another=true |
||
104 | * </code> |
||
105 | * |
||
106 | * Most variations are allowed: |
||
107 | * <code> |
||
108 | * driver://user:password@protocol+host:110//usr/db_file.db?mode=0644 |
||
109 | * driver://user:password@host/dbname |
||
110 | * driver://user:password@host |
||
111 | * driver://user@host |
||
112 | * driver://host/dbname |
||
113 | * driver://host |
||
114 | * driver |
||
115 | * </code> |
||
116 | * |
||
117 | * This function is 'borrowed' from PEAR /DB.php . |
||
118 | * |
||
119 | * @license Apache2 from Zeta Components Database project |
||
120 | * |
||
121 | * @param string $dsn Data Source Name to be parsed |
||
122 | * |
||
123 | * @return array an associative array with the following keys: |
||
124 | * + driver: Database backend used in PHP (mysql, odbc etc.) |
||
125 | * + host: Host specification (hostname[:port]) |
||
126 | * + dbname: Database to use on the DBMS server |
||
127 | * + username: User name for login |
||
128 | * + password: Password for login |
||
129 | */ |
||
130 | public static function parseDSN($dsn) |
||
266 | |||
267 | /** |
||
268 | * @param \Doctrine\DBAL\Connection $connection |
||
269 | */ |
||
270 | public function __construct(Connection $connection) |
||
274 | |||
275 | /** |
||
276 | * @return \Doctrine\DBAL\Connection |
||
277 | */ |
||
278 | public function getConnection() |
||
282 | |||
283 | /** |
||
284 | * @return string |
||
285 | */ |
||
286 | public function getName() |
||
290 | |||
291 | /** |
||
292 | * Begin a transaction. |
||
293 | */ |
||
294 | public function beginTransaction() |
||
302 | |||
303 | /** |
||
304 | * Commit a transaction. |
||
305 | */ |
||
306 | public function commit() |
||
314 | |||
315 | /** |
||
316 | * Rollback a transaction. |
||
317 | */ |
||
318 | public function rollBack() |
||
326 | |||
327 | public function prepare($query) |
||
331 | |||
332 | /** |
||
333 | * Retrieve the last auto incremet or sequence id. |
||
334 | * |
||
335 | * @param string $sequenceName |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | public function lastInsertId($sequenceName = null) |
||
343 | |||
344 | /** |
||
345 | * @return bool |
||
346 | */ |
||
347 | public function useSequences() |
||
351 | |||
352 | /** |
||
353 | * Execute a query against the database. |
||
354 | * |
||
355 | * @param string $query |
||
356 | */ |
||
357 | public function exec($query) |
||
365 | |||
366 | /** |
||
367 | * Create Select Query object. |
||
368 | * |
||
369 | * @return \eZ\Publish\Core\Persistence\Database\SelectQuery |
||
370 | */ |
||
371 | public function createSelectQuery() |
||
375 | |||
376 | /** |
||
377 | * Create Insert Query object. |
||
378 | * |
||
379 | * @return \eZ\Publish\Core\Persistence\Database\InsertQuery |
||
380 | */ |
||
381 | public function createInsertQuery() |
||
385 | |||
386 | /** |
||
387 | * Create update Query object. |
||
388 | * |
||
389 | * @return \eZ\Publish\Core\Persistence\Database\UpdateQuery |
||
390 | */ |
||
391 | public function createUpdateQuery() |
||
395 | |||
396 | /** |
||
397 | * Create a Delete Query object. |
||
398 | * |
||
399 | * @return \eZ\Publish\Core\Persistence\Database\DeleteQuery |
||
400 | */ |
||
401 | public function createDeleteQuery() |
||
405 | |||
406 | /** |
||
407 | * Creates an alias for $tableName, $columnName in $query. |
||
408 | * |
||
409 | * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query |
||
410 | * @param string $columnName |
||
411 | * @param string|null $tableName |
||
412 | * |
||
413 | * @return string |
||
414 | */ |
||
415 | public function aliasedColumn($query, $columnName, $tableName = null) |
||
425 | |||
426 | /** |
||
427 | * Returns a qualified identifier for $columnName in $tableName. |
||
428 | * |
||
429 | * @param string $columnName |
||
430 | * @param string $tableName |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | public function quoteColumn($columnName, $tableName = null) |
||
440 | |||
441 | /** |
||
442 | * Returns a qualified identifier for $tableName. |
||
443 | * |
||
444 | * @param string $tableName |
||
445 | * |
||
446 | * @return string |
||
447 | */ |
||
448 | public function quoteTable($tableName) |
||
452 | |||
453 | /** |
||
454 | * Custom alias method. |
||
455 | * |
||
456 | * Ignores some properties of identifier quoting, but since we use somehow |
||
457 | * sane table and column names, ourselves, this is fine. |
||
458 | * |
||
459 | * This is an optimization and works around the ezcDB implementation. |
||
460 | * |
||
461 | * @param string $identifier |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | public function alias($name, $alias) |
||
469 | |||
470 | /** |
||
471 | * Custom quote identifier method. |
||
472 | * |
||
473 | * Ignores some properties of identifier quoting, but since we use somehow |
||
474 | * sane table and column names, ourselves, this is fine. |
||
475 | * |
||
476 | * This is an optimization and works around the ezcDB implementation. |
||
477 | * |
||
478 | * @param string $identifier |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | public function quoteIdentifier($identifier) |
||
486 | |||
487 | /** |
||
488 | * Get auto increment value. |
||
489 | * |
||
490 | * Returns the value used for autoincrement tables. Usually this will just |
||
491 | * be null. In case for sequence based RDBMS this method can return a |
||
492 | * proper value for the given column. |
||
493 | * |
||
494 | * @param string $table |
||
495 | * @param string $column |
||
496 | * |
||
497 | * @return mixed |
||
498 | */ |
||
499 | public function getAutoIncrementValue($table, $column) |
||
503 | |||
504 | /** |
||
505 | * Returns the name of the affected sequence. |
||
506 | * |
||
507 | * @param string $table |
||
508 | * @param string $column |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | public function getSequenceName($table, $column) |
||
516 | } |
||
517 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.