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 |
||
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 $escape_char_open = '"'; |
||
43 | |||
44 | /** |
||
45 | * End character to escape identifiers |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $escape_char_close = '"'; |
||
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 $last_query; |
||
67 | |||
68 | /** |
||
69 | * Prefix to apply to table names |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $table_prefix = ''; |
||
73 | |||
74 | /** |
||
75 | * Whether the driver supports 'TRUNCATE' |
||
76 | * @var boolean |
||
77 | */ |
||
78 | protected $has_truncate = TRUE; |
||
79 | |||
80 | /** |
||
81 | * PDO constructor wrapper |
||
82 | * |
||
83 | * @param string $dsn |
||
84 | * @param string $username |
||
85 | * @param string $password |
||
86 | * @param array $driver_options |
||
87 | */ |
||
88 | public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options=[]) |
||
96 | |||
97 | // -------------------------------------------------------------------------- |
||
98 | |||
99 | /** |
||
100 | * Loads the subclasses for the driver |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | protected function _load_sub_classes() |
||
117 | |||
118 | // -------------------------------------------------------------------------- |
||
119 | |||
120 | /** |
||
121 | * Allow invoke to work on table object |
||
122 | * |
||
123 | * @codeCoverageIgnore |
||
124 | * @param string $name |
||
125 | * @param array $args |
||
126 | * @return mixed |
||
127 | */ |
||
128 | public function __call($name, $args = []) |
||
139 | |||
140 | // -------------------------------------------------------------------------- |
||
141 | // ! Accessors / Mutators |
||
142 | // -------------------------------------------------------------------------- |
||
143 | |||
144 | /** |
||
145 | * Get the last sql query executed |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | public function get_last_query() |
||
153 | |||
154 | // -------------------------------------------------------------------------- |
||
155 | |||
156 | /** |
||
157 | * Set the last query sql |
||
158 | * |
||
159 | * @param string $query_string |
||
160 | * @return void |
||
161 | */ |
||
162 | public function set_last_query($query_string) |
||
166 | |||
167 | // -------------------------------------------------------------------------- |
||
168 | |||
169 | /** |
||
170 | * Get the SQL class for the current driver |
||
171 | * |
||
172 | * @return SQLInterface |
||
173 | */ |
||
174 | public function get_sql() |
||
178 | |||
179 | // -------------------------------------------------------------------------- |
||
180 | |||
181 | /** |
||
182 | * Get the Util class for the current driver |
||
183 | * |
||
184 | * @return AbstractUtil |
||
185 | */ |
||
186 | public function get_util() |
||
190 | |||
191 | // -------------------------------------------------------------------------- |
||
192 | |||
193 | /** |
||
194 | * Set the common table name prefix |
||
195 | * |
||
196 | * @param string $prefix |
||
197 | * @return void |
||
198 | */ |
||
199 | public function set_table_prefix($prefix) |
||
203 | |||
204 | // -------------------------------------------------------------------------- |
||
205 | // ! Concrete functions that can be overridden in child classes |
||
206 | // -------------------------------------------------------------------------- |
||
207 | |||
208 | /** |
||
209 | * Simplifies prepared statements for database queries |
||
210 | * |
||
211 | * @param string $sql |
||
212 | * @param array $data |
||
213 | * @return PDOStatement | FALSE |
||
214 | * @throws InvalidArgumentException |
||
215 | */ |
||
216 | public function prepare_query($sql, $data) |
||
240 | |||
241 | // ------------------------------------------------------------------------- |
||
242 | |||
243 | /** |
||
244 | * Create and execute a prepared statement with the provided parameters |
||
245 | * |
||
246 | * @param string $sql |
||
247 | * @param array $params |
||
248 | * @return PDOStatement |
||
249 | */ |
||
250 | public function prepare_execute($sql, $params) |
||
257 | |||
258 | // ------------------------------------------------------------------------- |
||
259 | |||
260 | /** |
||
261 | * Returns number of rows affected by an INSERT, UPDATE, DELETE type query |
||
262 | * |
||
263 | * @return int |
||
264 | */ |
||
265 | public function affected_rows() |
||
270 | |||
271 | // -------------------------------------------------------------------------- |
||
272 | |||
273 | /** |
||
274 | * Prefixes a table if it is not already prefixed |
||
275 | * @param string $table |
||
276 | * @return string |
||
277 | */ |
||
278 | public function prefix_table($table) |
||
301 | |||
302 | // -------------------------------------------------------------------------- |
||
303 | |||
304 | /** |
||
305 | * Quote database table name, and set prefix |
||
306 | * |
||
307 | * @param string $table |
||
308 | * @return string |
||
309 | */ |
||
310 | public function quote_table($table) |
||
317 | |||
318 | // -------------------------------------------------------------------------- |
||
319 | |||
320 | /** |
||
321 | * Surrounds the string with the databases identifier escape characters |
||
322 | * |
||
323 | * @param mixed $identifier |
||
324 | * @return string |
||
325 | */ |
||
326 | public function quote_ident($identifier) |
||
363 | |||
364 | // ------------------------------------------------------------------------- |
||
365 | |||
366 | /** |
||
367 | * Return schemas for databases that list them |
||
368 | * |
||
369 | * @return array |
||
370 | */ |
||
371 | public function get_schemas() |
||
375 | |||
376 | // ------------------------------------------------------------------------- |
||
377 | |||
378 | /** |
||
379 | * Return list of tables for the current database |
||
380 | * |
||
381 | * @return array |
||
382 | */ |
||
383 | public function get_tables() |
||
389 | |||
390 | // ------------------------------------------------------------------------- |
||
391 | |||
392 | /** |
||
393 | * Return list of dbs for the current connection, if possible |
||
394 | * |
||
395 | * @return array |
||
396 | */ |
||
397 | public function get_dbs() |
||
401 | |||
402 | // ------------------------------------------------------------------------- |
||
403 | |||
404 | /** |
||
405 | * Return list of views for the current database |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | public function get_views() |
||
415 | |||
416 | // ------------------------------------------------------------------------- |
||
417 | |||
418 | /** |
||
419 | * Return list of sequences for the current database, if they exist |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | public function get_sequences() |
||
427 | |||
428 | // ------------------------------------------------------------------------- |
||
429 | |||
430 | /** |
||
431 | * Return list of functions for the current database |
||
432 | * |
||
433 | * @return array |
||
434 | */ |
||
435 | public function get_functions() |
||
439 | |||
440 | // ------------------------------------------------------------------------- |
||
441 | |||
442 | /** |
||
443 | * Return list of stored procedures for the current database |
||
444 | * |
||
445 | * @return array |
||
446 | */ |
||
447 | public function get_procedures() |
||
451 | |||
452 | // ------------------------------------------------------------------------- |
||
453 | |||
454 | /** |
||
455 | * Return list of triggers for the current database |
||
456 | * |
||
457 | * @return array |
||
458 | */ |
||
459 | public function get_triggers() |
||
463 | |||
464 | // ------------------------------------------------------------------------- |
||
465 | |||
466 | /** |
||
467 | * Retrieves an array of non-user-created tables for |
||
468 | * the connection/database |
||
469 | * |
||
470 | * @return array |
||
471 | */ |
||
472 | public function get_system_tables() |
||
476 | |||
477 | // -------------------------------------------------------------------------- |
||
478 | |||
479 | /** |
||
480 | * Retrieve column information for the current database table |
||
481 | * |
||
482 | * @param string $table |
||
483 | * @return array |
||
484 | */ |
||
485 | public function get_columns($table) |
||
489 | |||
490 | // -------------------------------------------------------------------------- |
||
491 | |||
492 | /** |
||
493 | * Retrieve foreign keys for the table |
||
494 | * |
||
495 | * @param string $table |
||
496 | * @return array |
||
497 | */ |
||
498 | public function get_fks($table) |
||
502 | |||
503 | // -------------------------------------------------------------------------- |
||
504 | |||
505 | /** |
||
506 | * Retrieve indexes for the table |
||
507 | * |
||
508 | * @param string $table |
||
509 | * @return array |
||
510 | */ |
||
511 | public function get_indexes($table) |
||
515 | |||
516 | // -------------------------------------------------------------------------- |
||
517 | |||
518 | /** |
||
519 | * Retrieve list of data types for the database |
||
520 | * |
||
521 | * @return array |
||
522 | */ |
||
523 | public function get_types() |
||
527 | |||
528 | // ------------------------------------------------------------------------- |
||
529 | |||
530 | /** |
||
531 | * Method to simplify retrieving db results for meta-data queries |
||
532 | * |
||
533 | * @param string|array|null $query |
||
534 | * @param bool $filtered_index |
||
535 | * @return array |
||
536 | */ |
||
537 | public function driver_query($query, $filtered_index=TRUE) |
||
560 | |||
561 | // -------------------------------------------------------------------------- |
||
562 | |||
563 | /** |
||
564 | * Return the number of rows returned for a SELECT query |
||
565 | * |
||
566 | * @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110 |
||
567 | * @return int|null |
||
568 | */ |
||
569 | public function num_rows() |
||
582 | |||
583 | // -------------------------------------------------------------------------- |
||
584 | |||
585 | /** |
||
586 | * Create sql for batch insert |
||
587 | * |
||
588 | * @param string $table |
||
589 | * @param array|object $data |
||
590 | * @return null|array<string|array|null> |
||
591 | */ |
||
592 | public function insert_batch($table, $data=[]) |
||
624 | |||
625 | // -------------------------------------------------------------------------- |
||
626 | |||
627 | /** |
||
628 | * Creates a batch update, and executes it. |
||
629 | * Returns the number of affected rows |
||
630 | * |
||
631 | * @param string $table |
||
632 | * @param array|object $data |
||
633 | * @param string $where |
||
634 | * @return int|null |
||
635 | */ |
||
636 | public function update_batch($table, $data, $where) |
||
641 | |||
642 | // -------------------------------------------------------------------------- |
||
643 | |||
644 | /** |
||
645 | * Helper method for quote_ident |
||
646 | * |
||
647 | * @param mixed $str |
||
648 | * @return mixed |
||
649 | */ |
||
650 | public function _quote($str) |
||
664 | |||
665 | // -------------------------------------------------------------------------- |
||
666 | |||
667 | /** |
||
668 | * Sets the table prefix on the passed string |
||
669 | * |
||
670 | * @param string $str |
||
671 | * @return string |
||
672 | */ |
||
673 | protected function _prefix($str) |
||
683 | |||
684 | // ------------------------------------------------------------------------- |
||
685 | |||
686 | /** |
||
687 | * Empty the passed table |
||
688 | * |
||
689 | * @param string $table |
||
690 | * @return PDOStatement |
||
691 | */ |
||
692 | public function truncate($table) |
||
703 | |||
704 | } |
||
705 | // End of db_pdo.php |
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.