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); |
||
28 | abstract class AbstractDriver |
||
29 | extends PDO |
||
30 | 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(string $dsn, string $username=NULL, string $password=NULL, array $driverOptions=[]) |
||
96 | |||
97 | /** |
||
98 | * Loads the subclasses for the driver |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | protected function _loadSubClasses(): void |
||
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): void |
||
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(string $prefix): void |
||
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(string $sql, array $data): PDOStatement |
||
223 | |||
224 | /** |
||
225 | * Create and execute a prepared statement with the provided parameters |
||
226 | * |
||
227 | * @param string $sql |
||
228 | * @param array $params |
||
229 | * @throws InvalidArgumentException |
||
230 | * @return PDOStatement |
||
231 | */ |
||
232 | public function prepareExecute(string $sql, array $params): PDOStatement |
||
239 | |||
240 | /** |
||
241 | * Returns number of rows affected by an INSERT, UPDATE, DELETE type query |
||
242 | * |
||
243 | * @return int |
||
244 | */ |
||
245 | public function affectedRows(): int |
||
250 | |||
251 | /** |
||
252 | * Prefixes a table if it is not already prefixed |
||
253 | * @param string $table |
||
254 | * @return string |
||
255 | */ |
||
256 | public function prefixTable(string $table): string |
||
279 | |||
280 | /** |
||
281 | * Quote database table name, and set prefix |
||
282 | * |
||
283 | * @param string $table |
||
284 | * @return string |
||
285 | */ |
||
286 | public function quoteTable($table): string |
||
293 | |||
294 | /** |
||
295 | * Surrounds the string with the databases identifier escape characters |
||
296 | * |
||
297 | * @param mixed $identifier |
||
298 | * @return string|array |
||
299 | */ |
||
300 | public function quoteIdent($identifier) |
||
336 | |||
337 | /** |
||
338 | * Return schemas for databases that list them |
||
339 | * |
||
340 | * @return array |
||
341 | */ |
||
342 | public function getSchemas(): ?array |
||
346 | |||
347 | /** |
||
348 | * Return list of tables for the current database |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | public function getTables(): ?array |
||
358 | |||
359 | /** |
||
360 | * Return list of dbs for the current connection, if possible |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | public function getDbs(): array |
||
368 | |||
369 | /** |
||
370 | * Return list of views for the current database |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | public function getViews(): ?array |
||
380 | |||
381 | /** |
||
382 | * Return list of sequences for the current database, if they exist |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | public function getSequences(): ?array |
||
390 | |||
391 | /** |
||
392 | * Return list of functions for the current database |
||
393 | * |
||
394 | * @return array |
||
395 | */ |
||
396 | public function getFunctions(): ?array |
||
400 | |||
401 | /** |
||
402 | * Return list of stored procedures for the current database |
||
403 | * |
||
404 | * @return array |
||
405 | */ |
||
406 | public function getProcedures(): ?array |
||
410 | |||
411 | /** |
||
412 | * Return list of triggers for the current database |
||
413 | * |
||
414 | * @return array |
||
415 | */ |
||
416 | public function getTriggers(): ?array |
||
420 | |||
421 | /** |
||
422 | * Retrieves an array of non-user-created tables for |
||
423 | * the connection/database |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | public function getSystemTables(): ?array |
||
431 | |||
432 | /** |
||
433 | * Retrieve column information for the current database table |
||
434 | * |
||
435 | * @param string $table |
||
436 | * @return array |
||
437 | */ |
||
438 | public function getColumns($table): ?array |
||
442 | |||
443 | /** |
||
444 | * Retrieve foreign keys for the table |
||
445 | * |
||
446 | * @param string $table |
||
447 | * @return array |
||
448 | */ |
||
449 | public function getFks($table): ?array |
||
453 | |||
454 | /** |
||
455 | * Retrieve indexes for the table |
||
456 | * |
||
457 | * @param string $table |
||
458 | * @return array |
||
459 | */ |
||
460 | public function getIndexes($table): ?array |
||
464 | |||
465 | /** |
||
466 | * Retrieve list of data types for the database |
||
467 | * |
||
468 | * @return array |
||
469 | */ |
||
470 | public function getTypes(): ?array |
||
474 | |||
475 | /** |
||
476 | * Method to simplify retrieving db results for meta-data queries |
||
477 | * |
||
478 | * @param string|array|null $query |
||
479 | * @param bool $filteredIndex |
||
480 | * @return array|null |
||
481 | */ |
||
482 | public function driverQuery($query, $filteredIndex=TRUE): ?array |
||
505 | |||
506 | /** |
||
507 | * Return the number of rows returned for a SELECT query |
||
508 | * |
||
509 | * @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110 |
||
510 | * @return int|null |
||
511 | */ |
||
512 | public function numRows(): ?int |
||
525 | |||
526 | /** |
||
527 | * Create sql for batch insert |
||
528 | * |
||
529 | * @param string $table |
||
530 | * @param mixed $data |
||
531 | * @return array<string|array|null> |
||
532 | */ |
||
533 | public function insertBatch(string $table, array $data=[]): array |
||
562 | |||
563 | /** |
||
564 | * Creates a batch update, and executes it. |
||
565 | * Returns the number of affected rows |
||
566 | * |
||
567 | * @param string $table The table to update |
||
568 | * @param array $data an array of update values |
||
569 | * @param string $where The where key |
||
570 | * @return array<string,array,int> |
||
571 | */ |
||
572 | public function updateBatch(string $table, array $data, string $where): array |
||
635 | |||
636 | /** |
||
637 | * Empty the passed table |
||
638 | * |
||
639 | * @param string $table |
||
640 | * @return PDOStatement |
||
641 | */ |
||
642 | public function truncate(string $table): PDOStatement |
||
653 | |||
654 | /** |
||
655 | * Helper method for quote_ident |
||
656 | * |
||
657 | * @param mixed $str |
||
658 | * @return mixed |
||
659 | */ |
||
660 | public function _quote($str) |
||
674 | |||
675 | /** |
||
676 | * Sets the table prefix on the passed string |
||
677 | * |
||
678 | * @param string $str |
||
679 | * @return string |
||
680 | */ |
||
681 | protected function _prefix(string $str): string |
||
691 | } |
||
692 |
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.