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); |
||
26 | abstract class AbstractDriver |
||
27 | extends PDO |
||
28 | implements DriverInterface { |
||
29 | |||
30 | /** |
||
31 | * Reference to the last executed query |
||
32 | * @var PDOStatement |
||
33 | */ |
||
34 | protected $statement; |
||
35 | |||
36 | /** |
||
37 | * Start character to escape identifiers |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $escapeCharOpen = '"'; |
||
41 | |||
42 | /** |
||
43 | * End character to escape identifiers |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $escapeCharClose = '"'; |
||
47 | |||
48 | /** |
||
49 | * Reference to sql class |
||
50 | * @var SQLInterface |
||
51 | */ |
||
52 | protected $sql; |
||
53 | |||
54 | /** |
||
55 | * Reference to util class |
||
56 | * @var AbstractUtil |
||
57 | */ |
||
58 | protected $util; |
||
59 | |||
60 | /** |
||
61 | * Last query executed |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $lastQuery = ''; |
||
65 | |||
66 | /** |
||
67 | * Prefix to apply to table names |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $tablePrefix = ''; |
||
71 | |||
72 | /** |
||
73 | * Whether the driver supports 'TRUNCATE' |
||
74 | * @var boolean |
||
75 | */ |
||
76 | protected $hasTruncate = TRUE; |
||
77 | |||
78 | /** |
||
79 | * PDO constructor wrapper |
||
80 | * |
||
81 | * @param string $dsn |
||
82 | * @param string $username |
||
83 | * @param string $password |
||
84 | * @param array $driverOptions |
||
85 | */ |
||
86 | public function __construct(string $dsn, string $username=NULL, string $password=NULL, array $driverOptions=[]) |
||
94 | |||
95 | /** |
||
96 | * Loads the subclasses for the driver |
||
97 | * |
||
98 | * @return void |
||
99 | */ |
||
100 | protected function _loadSubClasses(): void |
||
113 | |||
114 | /** |
||
115 | * Allow invoke to work on table object |
||
116 | * |
||
117 | * @codeCoverageIgnore |
||
118 | * @param string $name |
||
119 | * @param array $args |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public function __call(string $name, array $args = []) |
||
133 | |||
134 | // -------------------------------------------------------------------------- |
||
135 | // ! Accessors / Mutators |
||
136 | // -------------------------------------------------------------------------- |
||
137 | |||
138 | /** |
||
139 | * Get the last sql query executed |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getLastQuery(): string |
||
147 | |||
148 | /** |
||
149 | * Set the last query sql |
||
150 | * |
||
151 | * @param string $queryString |
||
152 | * @return void |
||
153 | */ |
||
154 | public function setLastQuery(string $queryString): void |
||
158 | |||
159 | /** |
||
160 | * Get the SQL class for the current driver |
||
161 | * |
||
162 | * @return SQLInterface |
||
163 | */ |
||
164 | public function getSql(): SQLInterface |
||
168 | |||
169 | /** |
||
170 | * Get the Util class for the current driver |
||
171 | * |
||
172 | * @return AbstractUtil |
||
173 | */ |
||
174 | public function getUtil(): AbstractUtil |
||
178 | |||
179 | /** |
||
180 | * Set the common table name prefix |
||
181 | * |
||
182 | * @param string $prefix |
||
183 | * @return void |
||
184 | */ |
||
185 | public function setTablePrefix(string $prefix): void |
||
189 | |||
190 | // -------------------------------------------------------------------------- |
||
191 | // ! Concrete functions that can be overridden in child classes |
||
192 | // -------------------------------------------------------------------------- |
||
193 | |||
194 | /** |
||
195 | * Simplifies prepared statements for database queries |
||
196 | * |
||
197 | * @param string $sql |
||
198 | * @param array $data |
||
199 | * @return PDOStatement | FALSE |
||
200 | * @throws InvalidArgumentException |
||
201 | */ |
||
202 | public function prepareQuery(string $sql, array $data): ?PDOStatement |
||
221 | |||
222 | /** |
||
223 | * Create and execute a prepared statement with the provided parameters |
||
224 | * |
||
225 | * @param string $sql |
||
226 | * @param array $params |
||
227 | * @throws InvalidArgumentException |
||
228 | * @return PDOStatement |
||
229 | */ |
||
230 | public function prepareExecute(string $sql, array $params): ?PDOStatement |
||
237 | |||
238 | /** |
||
239 | * Returns number of rows affected by an INSERT, UPDATE, DELETE type query |
||
240 | * |
||
241 | * @return int |
||
242 | */ |
||
243 | public function affectedRows(): int |
||
248 | |||
249 | /** |
||
250 | * Prefixes a table if it is not already prefixed |
||
251 | * @param string $table |
||
252 | * @return string |
||
253 | */ |
||
254 | public function prefixTable(string $table): string |
||
277 | |||
278 | /** |
||
279 | * Quote database table name, and set prefix |
||
280 | * |
||
281 | * @param string $table |
||
282 | * @return string |
||
283 | */ |
||
284 | public function quoteTable($table): string |
||
291 | |||
292 | /** |
||
293 | * Surrounds the string with the databases identifier escape characters |
||
294 | * |
||
295 | * @param mixed $identifier |
||
296 | * @return string|array |
||
297 | */ |
||
298 | public function quoteIdent($identifier) |
||
334 | |||
335 | /** |
||
336 | * Return schemas for databases that list them |
||
337 | * |
||
338 | * @return array |
||
339 | */ |
||
340 | public function getSchemas(): ?array |
||
344 | |||
345 | /** |
||
346 | * Return list of tables for the current database |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | public function getTables(): ?array |
||
356 | |||
357 | /** |
||
358 | * Return list of dbs for the current connection, if possible |
||
359 | * |
||
360 | * @return array |
||
361 | */ |
||
362 | public function getDbs(): array |
||
366 | |||
367 | /** |
||
368 | * Return list of views for the current database |
||
369 | * |
||
370 | * @return array |
||
371 | */ |
||
372 | public function getViews(): ?array |
||
378 | |||
379 | /** |
||
380 | * Return list of sequences for the current database, if they exist |
||
381 | * |
||
382 | * @return array |
||
383 | */ |
||
384 | public function getSequences(): ?array |
||
388 | |||
389 | /** |
||
390 | * Return list of functions for the current database |
||
391 | * |
||
392 | * @return array |
||
393 | */ |
||
394 | public function getFunctions(): ?array |
||
398 | |||
399 | /** |
||
400 | * Return list of stored procedures for the current database |
||
401 | * |
||
402 | * @return array |
||
403 | */ |
||
404 | public function getProcedures(): ?array |
||
408 | |||
409 | /** |
||
410 | * Return list of triggers for the current database |
||
411 | * |
||
412 | * @return array |
||
413 | */ |
||
414 | public function getTriggers(): ?array |
||
418 | |||
419 | /** |
||
420 | * Retrieves an array of non-user-created tables for |
||
421 | * the connection/database |
||
422 | * |
||
423 | * @return array |
||
424 | */ |
||
425 | public function getSystemTables(): ?array |
||
429 | |||
430 | /** |
||
431 | * Retrieve column information for the current database table |
||
432 | * |
||
433 | * @param string $table |
||
434 | * @return array |
||
435 | */ |
||
436 | public function getColumns($table): ?array |
||
440 | |||
441 | /** |
||
442 | * Retrieve foreign keys for the table |
||
443 | * |
||
444 | * @param string $table |
||
445 | * @return array |
||
446 | */ |
||
447 | public function getFks($table): ?array |
||
451 | |||
452 | /** |
||
453 | * Retrieve indexes for the table |
||
454 | * |
||
455 | * @param string $table |
||
456 | * @return array |
||
457 | */ |
||
458 | public function getIndexes($table): ?array |
||
462 | |||
463 | /** |
||
464 | * Retrieve list of data types for the database |
||
465 | * |
||
466 | * @return array |
||
467 | */ |
||
468 | public function getTypes(): ?array |
||
472 | |||
473 | /** |
||
474 | * Method to simplify retrieving db results for meta-data queries |
||
475 | * |
||
476 | * @param string|array|null $query |
||
477 | * @param bool $filteredIndex |
||
478 | * @return array|null |
||
479 | */ |
||
480 | public function driverQuery($query, $filteredIndex=TRUE): ?array |
||
503 | |||
504 | /** |
||
505 | * Return the number of rows returned for a SELECT query |
||
506 | * |
||
507 | * @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110 |
||
508 | * @return int|null |
||
509 | */ |
||
510 | public function numRows(): ?int |
||
523 | |||
524 | /** |
||
525 | * Create sql for batch insert |
||
526 | * |
||
527 | * @param string $table |
||
528 | * @param mixed $data |
||
529 | * @return null|array<string|array|null> |
||
530 | */ |
||
531 | public function insertBatch(string $table, array $data=[]): array |
||
559 | |||
560 | /** |
||
561 | * Creates a batch update, and executes it. |
||
562 | * Returns the number of affected rows |
||
563 | * |
||
564 | * @param string $table The table to update |
||
565 | * @param array $data an array of update values |
||
566 | * @param string $where The where key |
||
567 | * @return array<string,array,int> |
||
568 | */ |
||
569 | public function updateBatch(string $table, array $data, string $where): array |
||
632 | |||
633 | /** |
||
634 | * Empty the passed table |
||
635 | * |
||
636 | * @param string $table |
||
637 | * @return PDOStatement |
||
638 | */ |
||
639 | public function truncate(string $table): ?PDOStatement |
||
650 | |||
651 | /** |
||
652 | * Helper method for quote_ident |
||
653 | * |
||
654 | * @param mixed $str |
||
655 | * @return mixed |
||
656 | */ |
||
657 | public function _quote($str) |
||
671 | |||
672 | /** |
||
673 | * Sets the table prefix on the passed string |
||
674 | * |
||
675 | * @param string $str |
||
676 | * @return string |
||
677 | */ |
||
678 | protected function _prefix(string $str): string |
||
688 | } |
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.