Complex classes like AbstractDriver 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 AbstractDriver, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
30 | abstract class AbstractDriver extends PDO implements DriverInterface { |
||
31 | |||
32 | /** |
||
33 | * Reference to the last executed query |
||
34 | * @var PDOStatement |
||
35 | */ |
||
36 | protected $statement; |
||
37 | |||
38 | /** |
||
39 | * Start character to escape identifiers |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $escapeCharOpen = '"'; |
||
43 | |||
44 | /** |
||
45 | * End character to escape identifiers |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $escapeCharClose = '"'; |
||
49 | |||
50 | /** |
||
51 | * Reference to sql class |
||
52 | * @var SQLInterface |
||
53 | */ |
||
54 | protected $sql; |
||
55 | |||
56 | /** |
||
57 | * Reference to util class |
||
58 | * @var AbstractUtil |
||
59 | */ |
||
60 | protected $util; |
||
61 | |||
62 | /** |
||
63 | * Last query executed |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $lastQuery = ''; |
||
67 | |||
68 | /** |
||
69 | * Prefix to apply to table names |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $tablePrefix = ''; |
||
73 | |||
74 | /** |
||
75 | * Whether the driver supports 'TRUNCATE' |
||
76 | * @var boolean |
||
77 | */ |
||
78 | protected $hasTruncate = TRUE; |
||
79 | |||
80 | /** |
||
81 | * PDO constructor wrapper |
||
82 | * |
||
83 | * @param string $dsn |
||
84 | * @param string $username |
||
85 | * @param string $password |
||
86 | * @param array $driverOptions |
||
87 | */ |
||
88 | public function __construct($dsn, $username=NULL, $password=NULL, array $driverOptions=[]) |
||
96 | |||
97 | /** |
||
98 | * Loads the subclasses for the driver |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | protected function _loadSubClasses() |
||
115 | |||
116 | /** |
||
117 | * Allow invoke to work on table object |
||
118 | * |
||
119 | * @codeCoverageIgnore |
||
120 | * @param string $name |
||
121 | * @param array $args |
||
122 | * @return mixed |
||
123 | */ |
||
124 | public function __call(string $name, array $args = []) |
||
135 | |||
136 | // -------------------------------------------------------------------------- |
||
137 | // ! Accessors / Mutators |
||
138 | // -------------------------------------------------------------------------- |
||
139 | |||
140 | /** |
||
141 | * Get the last sql query executed |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getLastQuery(): string |
||
149 | |||
150 | /** |
||
151 | * Set the last query sql |
||
152 | * |
||
153 | * @param string $queryString |
||
154 | * @return void |
||
155 | */ |
||
156 | public function setLastQuery(string $queryString) |
||
160 | |||
161 | /** |
||
162 | * Get the SQL class for the current driver |
||
163 | * |
||
164 | * @return SQLInterface |
||
165 | */ |
||
166 | public function getSql(): SQLInterface |
||
170 | |||
171 | /** |
||
172 | * Get the Util class for the current driver |
||
173 | * |
||
174 | * @return AbstractUtil |
||
175 | */ |
||
176 | public function getUtil(): AbstractUtil |
||
180 | |||
181 | /** |
||
182 | * Set the common table name prefix |
||
183 | * |
||
184 | * @param string $prefix |
||
185 | * @return void |
||
186 | */ |
||
187 | public function setTablePrefix($prefix) |
||
191 | |||
192 | // -------------------------------------------------------------------------- |
||
193 | // ! Concrete functions that can be overridden in child classes |
||
194 | // -------------------------------------------------------------------------- |
||
195 | |||
196 | /** |
||
197 | * Simplifies prepared statements for database queries |
||
198 | * |
||
199 | * @param string $sql |
||
200 | * @param array $data |
||
201 | * @return PDOStatement | FALSE |
||
202 | * @throws InvalidArgumentException |
||
203 | */ |
||
204 | public function prepareQuery($sql, $data) |
||
228 | |||
229 | /** |
||
230 | * Create and execute a prepared statement with the provided parameters |
||
231 | * |
||
232 | * @param string $sql |
||
233 | * @param array $params |
||
234 | * @return PDOStatement |
||
235 | */ |
||
236 | public function prepareExecute($sql, $params) |
||
243 | |||
244 | /** |
||
245 | * Returns number of rows affected by an INSERT, UPDATE, DELETE type query |
||
246 | * |
||
247 | * @return int |
||
248 | */ |
||
249 | public function affectedRows() |
||
254 | |||
255 | /** |
||
256 | * Prefixes a table if it is not already prefixed |
||
257 | * @param string $table |
||
258 | * @return string |
||
259 | */ |
||
260 | public function prefixTable($table) |
||
283 | |||
284 | /** |
||
285 | * Quote database table name, and set prefix |
||
286 | * |
||
287 | * @param string $table |
||
288 | * @return string |
||
289 | */ |
||
290 | public function quoteTable($table) |
||
297 | |||
298 | /** |
||
299 | * Surrounds the string with the databases identifier escape characters |
||
300 | * |
||
301 | * @param mixed $identifier |
||
302 | * @return string |
||
303 | */ |
||
304 | public function quoteIdent($identifier) |
||
341 | |||
342 | /** |
||
343 | * Return schemas for databases that list them |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | public function getSchemas() |
||
351 | |||
352 | /** |
||
353 | * Return list of tables for the current database |
||
354 | * |
||
355 | * @return array |
||
356 | */ |
||
357 | public function getTables() |
||
363 | |||
364 | /** |
||
365 | * Return list of dbs for the current connection, if possible |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | public function getDbs() |
||
373 | |||
374 | /** |
||
375 | * Return list of views for the current database |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | public function getViews() |
||
385 | |||
386 | /** |
||
387 | * Return list of sequences for the current database, if they exist |
||
388 | * |
||
389 | * @return array |
||
390 | */ |
||
391 | public function getSequences() |
||
395 | |||
396 | /** |
||
397 | * Return list of functions for the current database |
||
398 | * |
||
399 | * @return array |
||
400 | */ |
||
401 | public function getFunctions() |
||
405 | |||
406 | /** |
||
407 | * Return list of stored procedures for the current database |
||
408 | * |
||
409 | * @return array |
||
410 | */ |
||
411 | public function getProcedures() |
||
415 | |||
416 | /** |
||
417 | * Return list of triggers for the current database |
||
418 | * |
||
419 | * @return array |
||
420 | */ |
||
421 | public function getTriggers() |
||
425 | |||
426 | /** |
||
427 | * Retrieves an array of non-user-created tables for |
||
428 | * the connection/database |
||
429 | * |
||
430 | * @return array |
||
431 | */ |
||
432 | public function getSystemTables() |
||
436 | |||
437 | /** |
||
438 | * Retrieve column information for the current database table |
||
439 | * |
||
440 | * @param string $table |
||
441 | * @return array |
||
442 | */ |
||
443 | public function getColumns($table) |
||
447 | |||
448 | /** |
||
449 | * Retrieve foreign keys for the table |
||
450 | * |
||
451 | * @param string $table |
||
452 | * @return array |
||
453 | */ |
||
454 | public function getFks($table) |
||
458 | |||
459 | /** |
||
460 | * Retrieve indexes for the table |
||
461 | * |
||
462 | * @param string $table |
||
463 | * @return array |
||
464 | */ |
||
465 | public function getIndexes($table) |
||
469 | |||
470 | /** |
||
471 | * Retrieve list of data types for the database |
||
472 | * |
||
473 | * @return array |
||
474 | */ |
||
475 | public function getTypes() |
||
479 | |||
480 | /** |
||
481 | * Method to simplify retrieving db results for meta-data queries |
||
482 | * |
||
483 | * @param string|array|null $query |
||
484 | * @param bool $filteredIndex |
||
485 | * @return array |
||
486 | */ |
||
487 | public function driverQuery($query, $filteredIndex=TRUE) |
||
510 | |||
511 | /** |
||
512 | * Return the number of rows returned for a SELECT query |
||
513 | * |
||
514 | * @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110 |
||
515 | * @return int|null |
||
516 | */ |
||
517 | public function numRows() |
||
530 | |||
531 | /** |
||
532 | * Create sql for batch insert |
||
533 | * |
||
534 | * @param string $table |
||
535 | * @param array|object $data |
||
536 | * @return null|array<string|array|null> |
||
537 | */ |
||
538 | public function insertBatch($table, $data=[]) |
||
570 | |||
571 | /** |
||
572 | * Creates a batch update, and executes it. |
||
573 | * Returns the number of affected rows |
||
574 | * |
||
575 | * @param string $table |
||
576 | * @param array|object $data |
||
577 | * @param string $where |
||
578 | * @return int|null |
||
579 | */ |
||
580 | public function updateBatch($table, $data, $where) |
||
585 | |||
586 | /** |
||
587 | * Helper method for quote_ident |
||
588 | * |
||
589 | * @param mixed $str |
||
590 | * @return mixed |
||
591 | */ |
||
592 | public function _quote($str) |
||
606 | |||
607 | /** |
||
608 | * Sets the table prefix on the passed string |
||
609 | * |
||
610 | * @param string $str |
||
611 | * @return string |
||
612 | */ |
||
613 | protected function _prefix($str) |
||
623 | |||
624 | /** |
||
625 | * Empty the passed table |
||
626 | * |
||
627 | * @param string $table |
||
628 | * @return PDOStatement |
||
629 | */ |
||
630 | public function truncate($table) |
||
641 | |||
642 | } |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.