Complex classes like Driver 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 Driver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Driver |
||
| 6 | { |
||
| 7 | protected $id = 0; |
||
| 8 | protected static $drivers = array(); |
||
| 9 | protected static $logs = array(); |
||
| 10 | protected $connection; |
||
| 11 | private $construct; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Either implement an established PDO instance, or set up a lazy database connection that we only connect to if and when you actually use it. Every $dsn string with a '**dbname**' is saved in a databases.yml file so that you only need to spell everything out once, and then just refer to the 'dbname' in your code. |
||
| 15 | * |
||
| 16 | * @param string|object $dsn Either a PDO Instance, a DSN string that contains the information required to connect to the database, or the 'dbname' saved in the databases.yml file. Some examples are: |
||
| 17 | * |
||
| 18 | * - [MySQL](http://php.net/manual/en/ref.pdo-mysql.connection.php "mysql") |
||
| 19 | * - mysql:host=[name];port=[number];dbname=[database];unix_socket=[instead of host or port];charset=[utf-8] |
||
| 20 | * - MySQL v. <= 4.1 not supported in PHP v. >= 5.4.0 |
||
| 21 | * - charset is ignored in PHP v. < 5.3.6 |
||
| 22 | * - [SQLite](http://php.net/manual/en/ref.pdo-sqlite.connection.php "sqlite") |
||
| 23 | * - sqlite:[filepath OR ':memory:'] |
||
| 24 | * - [PostgreSQL](http://php.net/manual/en/ref.pdo-pgsql.connection.php "pgsql") |
||
| 25 | * - pgsql:host=[name];port=[number];dbname=[database];user=[name];password=[secret] |
||
| 26 | * - [Oracle](http://php.net/manual/en/ref.pdo-oci.connection.php "oci") |
||
| 27 | * - oci:dbname=[database];charset=[utf-8] |
||
| 28 | * - [MSSQL on Windows](http://php.net/manual/en/ref.pdo-sqlsrv.connection.php "sqlsrv") |
||
| 29 | * - sqlsrv:Server=[name,port];Database=[name] ... and a bunch of other options |
||
| 30 | * - [MSSQL on Linux / Unix](http://php.net/manual/en/ref.pdo-dblib.connection.php "dblib") |
||
| 31 | * - sybase:host=[name];dbname=[database];charset=[utf-8];appname=[application];secure=[currently unused] |
||
| 32 | * - [Sybase](http://php.net/manual/en/ref.pdo-dblib.connection.php "sybase") |
||
| 33 | * - sybase:host=[name];dbname=[database];charset=[utf-8];appname=[application];secure=[currently unused] |
||
| 34 | * - [Microsoft SQL Server](http://php.net/manual/en/ref.pdo-dblib.connection.php "mssql") |
||
| 35 | * - mssql:host=[name];dbname=[database];charset=[utf-8];appname=[application];secure=[currently unused] |
||
| 36 | * - [Cubrid](http://php.net/manual/en/ref.pdo-cubrid.connection.php "cubrid") |
||
| 37 | * - cubrid:host=[name];port=[number];dbname=[database]; |
||
| 38 | * |
||
| 39 | * @param string|null $username The user name for the DSN string. This parameter is optional for some PDO drivers. |
||
| 40 | * @param string|null $password The password for the DSN string. This parameter is optional for some PDO drivers. |
||
| 41 | * @param array $options An ``array('key'=>'value', ...)`` of driver-specific connection options. |
||
| 42 | * @param array $exec Queries you would like to execute upon connecting to the database. |
||
| 43 | * |
||
| 44 | * @see http://php.net/manual/en/pdo.construct.php |
||
| 45 | */ |
||
| 46 | 5 | public function __construct($dsn, $username = null, $password = null, array $options = array(), array $exec = array()) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @return integer The current database's id. |
||
| 87 | */ |
||
| 88 | 1 | public function id() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $name Pass a value to set the database driver's name. |
||
| 95 | * |
||
| 96 | * @return string|null The current database driver's name. |
||
| 97 | */ |
||
| 98 | 53 | public function driver($name = null) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @param integer $id To only return the data for a specific database connection. |
||
| 112 | * |
||
| 113 | * @return array Debug, error, and profile data for all of your database queries. |
||
| 114 | */ |
||
| 115 | 1 | public static function logs($id = null) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @return array All of the errors generated from all of your database connections. |
||
| 143 | */ |
||
| 144 | 1 | public static function errors() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @return object The database connection. This is how we create lazy connections. |
||
| 170 | */ |
||
| 171 | 79 | public function connection() |
|
| 187 | |||
| 188 | 20 | protected function dbPrepare($query) |
|
| 192 | |||
| 193 | 5 | protected function dbPrepareError() |
|
| 197 | |||
| 198 | 20 | protected function dbExecute($stmt, array $values, $reference) |
|
| 202 | |||
| 203 | 1 | protected function dbExecuteError($stmt) |
|
| 207 | |||
| 208 | 17 | protected function dbStyle($fetch) |
|
| 228 | |||
| 229 | 17 | protected function dbFetch($stmt, $style, $reference) |
|
| 233 | |||
| 234 | 4 | protected function dbInserted() |
|
| 238 | |||
| 239 | 5 | protected function dbAffected($stmt) |
|
| 243 | |||
| 244 | 20 | protected function dbClose($stmt, $reference) |
|
| 248 | |||
| 249 | 1 | protected function dbEscape($string) |
|
| 253 | } |
||
| 254 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: