Complex classes like PDODriver 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 PDODriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class PDODriver extends Component implements LoggerAwareInterface |
||
| 29 | { |
||
| 30 | use LoggerTrait, BenchmarkTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * One of DatabaseInterface types, must be set on implementation. |
||
| 34 | */ |
||
| 35 | const TYPE = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * DateTime format to be used to perform automatic conversion of DateTime objects. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | const DATETIME = 'Y-m-d H:i:s'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Driver name. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $name = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var PDO|null |
||
| 53 | */ |
||
| 54 | private $pdo = null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Connection configuration described in DBAL config file. Any driver can be used as data source |
||
| 58 | * for multiple databases as table prefix and quotation defined on Database instance level. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $defaultOptions = [ |
||
| 63 | 'profiling' => false, |
||
| 64 | |||
| 65 | //All datetime objects will be converted relative to this timezone |
||
| 66 | 'timezone' => 'UTC', |
||
| 67 | |||
| 68 | //DSN |
||
| 69 | 'connection' => '', |
||
| 70 | 'username' => '', |
||
| 71 | 'password' => '', |
||
| 72 | 'options' => [], |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * PDO connection options set. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $options = [ |
||
| 81 | PDO::ATTR_CASE => PDO::CASE_NATURAL, |
||
| 82 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
| 83 | PDO::ATTR_STRINGIFY_FETCHES => true, |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $name |
||
| 88 | * @param array $options |
||
| 89 | * |
||
| 90 | * @throws ScopeException |
||
| 91 | */ |
||
| 92 | public function __construct(string $name, array $options) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Source name, can include database name or database file. |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getName(): string |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get driver source database or file name. |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | * |
||
| 119 | * @throws DriverException |
||
| 120 | */ |
||
| 121 | public function getSource(): string |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Database type driver linked to. |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getType(): string |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Connection specific timezone, at this moment locked to UTC. |
||
| 143 | * |
||
| 144 | * @return \DateTimeZone |
||
| 145 | */ |
||
| 146 | public function getTimezone(): \DateTimeZone |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Enabled profiling will raise set of log messages and benchmarks associated with PDO queries. |
||
| 153 | * |
||
| 154 | * @param bool $enabled Enable or disable driver profiling. |
||
| 155 | * |
||
| 156 | * @return self |
||
| 157 | */ |
||
| 158 | public function setProfiling(bool $enabled = true): PDODriver |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Check if profiling mode is enabled. |
||
| 167 | * |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public function isProfiling(): bool |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Force driver to connect. |
||
| 177 | * |
||
| 178 | * @return PDO |
||
| 179 | * |
||
| 180 | * @throws DriverException |
||
| 181 | */ |
||
| 182 | public function connect(): PDO |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Disconnect driver. |
||
| 200 | * |
||
| 201 | * @return self |
||
| 202 | */ |
||
| 203 | public function disconnect(): PDODriver |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Check if driver already connected. |
||
| 212 | * |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | public function isConnected(): bool |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Change PDO instance associated with driver. Returns new copy of driver. |
||
| 222 | * |
||
| 223 | * @param PDO $pdo |
||
| 224 | * |
||
| 225 | * @return self |
||
| 226 | */ |
||
| 227 | public function withPDO(PDO $pdo): PDODriver |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get associated PDO connection. Will automatically connect if such connection does not exists. |
||
| 237 | * |
||
| 238 | * @return PDO |
||
| 239 | */ |
||
| 240 | public function getPDO(): PDO |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Driver specific database/table identifier quotation. |
||
| 251 | * |
||
| 252 | * @param string $identifier |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function identifier(string $identifier): string |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Quote value using PDO. |
||
| 263 | * |
||
| 264 | * @param mixed $value |
||
| 265 | * @param int $type Parameter type. |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function quote($value, int $type = PDO::PARAM_STR): string |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Wraps PDO query method with custom representation class. |
||
| 280 | * |
||
| 281 | * @param string $statement |
||
| 282 | * @param array $parameters |
||
| 283 | * |
||
| 284 | * @return PDOResult |
||
| 285 | */ |
||
| 286 | public function query(string $statement, array $parameters = []): PDOResult |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Create instance of PDOStatement using provided SQL query and set of parameters and execute |
||
| 299 | * it. |
||
| 300 | * |
||
| 301 | * @param string $query |
||
| 302 | * @param array $parameters Parameters to be binded into query. |
||
| 303 | * @param string $class Class to be used to represent results. |
||
| 304 | * @param array $args Class construction arguments (by default filtered parameters) |
||
| 305 | * |
||
| 306 | * @return \PDOStatement |
||
| 307 | * |
||
| 308 | * @throws QueryException |
||
| 309 | */ |
||
| 310 | public function statement( |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get prepared PDO statement. |
||
| 361 | * |
||
| 362 | * @param string $statement Query statement. |
||
| 363 | * @param string $class Class to represent PDO statement. |
||
| 364 | * @param array $args Class construction arguments (by default paramaters) |
||
| 365 | * |
||
| 366 | * @return \PDOStatement |
||
| 367 | */ |
||
| 368 | public function prepare( |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get id of last inserted row, this method must be called after insert query. Attention, |
||
| 382 | * such functionality may not work in some DBMS property (Postgres). |
||
| 383 | * |
||
| 384 | * @param string|null $sequence Name of the sequence object from which the ID should be |
||
| 385 | * returned. |
||
| 386 | * |
||
| 387 | * @return mixed |
||
| 388 | */ |
||
| 389 | public function lastInsertID(string $sequence = null) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Prepare set of query builder/user parameters to be send to PDO. Must convert DateTime |
||
| 398 | * instances into valid database timestamps and resolve values of ParameterInterface. |
||
| 399 | * |
||
| 400 | * Every value has to wrapped with parameter interface. |
||
| 401 | * |
||
| 402 | * @param array $parameters |
||
| 403 | * |
||
| 404 | * @return ParameterInterface[] |
||
| 405 | * |
||
| 406 | * @throws DriverException |
||
| 407 | */ |
||
| 408 | public function flattenParameters(array $parameters): array |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | public function __debugInfo() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Create instance of configured PDO class. |
||
| 479 | * |
||
| 480 | * @return PDO |
||
| 481 | */ |
||
| 482 | protected function createPDO(): PDO |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Convert PDO exception into query or integrity exception. |
||
| 494 | * |
||
| 495 | * @param \PDOException $exception |
||
| 496 | * |
||
| 497 | * @return QueryException |
||
| 498 | */ |
||
| 499 | protected function clarifyException(\PDOException $exception): QueryException |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Convert DateTime object into local database representation. Driver will automatically force |
||
| 507 | * needed timezone. |
||
| 508 | * |
||
| 509 | * @param \DateTimeInterface $value |
||
| 510 | * |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | protected function normalizeTimestamp(\DateTimeInterface $value): string |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Bind parameters into statement. |
||
| 524 | * |
||
| 525 | * @param \PDOStatement $statement |
||
| 526 | * @param ParameterInterface[] $parameters Named hash of ParameterInterface. |
||
| 527 | * |
||
| 528 | * @return \PDOStatement |
||
| 529 | */ |
||
| 530 | private function bindParameters(\PDOStatement $statement, array $parameters): \PDOStatement |
||
| 544 | } |
||
| 545 |