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