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( |
||
357 | |||
358 | /** |
||
359 | * Get prepared PDO statement. |
||
360 | * |
||
361 | * @param string $statement Query statement. |
||
362 | * @param string $class Class to represent PDO statement. |
||
363 | * @param array $args Class construction arguments (by default paramaters) |
||
364 | * |
||
365 | * @return \PDOStatement |
||
366 | */ |
||
367 | public function prepare( |
||
378 | |||
379 | /** |
||
380 | * Get id of last inserted row, this method must be called after insert query. Attention, |
||
381 | * such functionality may not work in some DBMS property (Postgres). |
||
382 | * |
||
383 | * @param string|null $sequence Name of the sequence object from which the ID should be |
||
384 | * returned. |
||
385 | * |
||
386 | * @return mixed |
||
387 | */ |
||
388 | public function lastInsertID(string $sequence = null) |
||
394 | |||
395 | /** |
||
396 | * Prepare set of query builder/user parameters to be send to PDO. Must convert DateTime |
||
397 | * instances into valid database timestamps and resolve values of ParameterInterface. |
||
398 | * |
||
399 | * Every value has to wrapped with parameter interface. |
||
400 | * |
||
401 | * @param array $parameters |
||
402 | * |
||
403 | * @return ParameterInterface[] |
||
404 | * |
||
405 | * @throws DriverException |
||
406 | */ |
||
407 | public function flattenParameters(array $parameters): array |
||
461 | |||
462 | /** |
||
463 | * @return array |
||
464 | */ |
||
465 | public function __debugInfo() |
||
475 | |||
476 | /** |
||
477 | * Create instance of configured PDO class. |
||
478 | * |
||
479 | * @return PDO |
||
480 | */ |
||
481 | protected function createPDO(): PDO |
||
490 | |||
491 | /** |
||
492 | * Convert PDO exception into query or integrity exception. |
||
493 | * |
||
494 | * @param \PDOException $exception |
||
495 | * |
||
496 | * @return QueryException |
||
497 | */ |
||
498 | protected function clarifyException(\PDOException $exception): QueryException |
||
503 | |||
504 | /** |
||
505 | * Convert DateTime object into local database representation. Driver will automatically force |
||
506 | * needed timezone. |
||
507 | * |
||
508 | * @param \DateTimeInterface $value |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | protected function normalizeTimestamp(\DateTimeInterface $value): string |
||
520 | |||
521 | /** |
||
522 | * Bind parameters into statement. |
||
523 | * |
||
524 | * @param \PDOStatement $statement |
||
525 | * @param ParameterInterface[] $parameters Named hash of ParameterInterface. |
||
526 | * |
||
527 | * @return \PDOStatement |
||
528 | */ |
||
529 | private function bindParameters(\PDOStatement $statement, array $parameters): \PDOStatement |
||
543 | } |
||
544 |