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 |
||
| 18 | abstract class Driver |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The internal PDO connection that is wrapped by this driver. |
||
| 23 | * @var \PDO |
||
| 24 | */ |
||
| 25 | private $pdo; |
||
| 26 | private $logger; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The default schema used in the connection. |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $defaultSchema; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The connection parameters with which this connection was established. |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $config; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * An instance of the descriptor used internally. |
||
| 42 | * @var \ntentan\atiaa\Descriptor |
||
| 43 | */ |
||
| 44 | private $descriptor; |
||
| 45 | private static $transactionCount = 0; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Creates a new instance of the Atiaa driver. This class is usually initiated |
||
| 49 | * through the \ntentan\atiaa\Atiaa::getConnection() method. For example |
||
| 50 | * to create a new instance of a connection to a mysql database. |
||
| 51 | * |
||
| 52 | * ````php |
||
| 53 | * use ntentan\atiaa\Driver; |
||
| 54 | * |
||
| 55 | * \\ This automatically insitatiates the driver class |
||
| 56 | * $driver = Driver::getConnection( |
||
| 57 | * array( |
||
| 58 | * 'driver' => 'mysql', |
||
| 59 | * 'user' => 'root', |
||
| 60 | * 'password' => 'rootpassy', |
||
| 61 | * 'host' => 'localhost', |
||
| 62 | * 'dbname' => 'somedb' |
||
| 63 | * ) |
||
| 64 | * ); |
||
| 65 | * |
||
| 66 | * var_dump($driver->query("SELECT * FROM some_table"); |
||
| 67 | * var_dump($driver->describe()); |
||
| 68 | * ```` |
||
| 69 | * |
||
| 70 | * @param array<string> $config The configuration with which to connect to the database. |
||
| 71 | */ |
||
| 72 | public function __construct(array $config) |
||
| 76 | |||
| 77 | public function connect() |
||
| 90 | |||
| 91 | public function __destruct() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Close a connection to the database server. |
||
| 98 | */ |
||
| 99 | public function disconnect() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the default schema of the current connection. |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getDefaultSchema() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Use the PDO driver to quote a string. |
||
| 116 | * @param type $string |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function quote($string) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * |
||
| 126 | * @param boolean $status |
||
|
|
|||
| 127 | * @param \PDOStatement $result |
||
| 128 | */ |
||
| 129 | private function fetchRows($statement) |
||
| 138 | |||
| 139 | private function prepareQuery($query, $bindData) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Pepare and execute a query, while binding data at the same time. Prevents |
||
| 161 | * the writing of repetitive prepare and execute statements. This method |
||
| 162 | * returns an array which contains the results of the query that was |
||
| 163 | * executed. For queries which do not return any results a null is returned. |
||
| 164 | * |
||
| 165 | * @todo Add a parameter to cache prepared statements so they can be reused easily. |
||
| 166 | * |
||
| 167 | * @param string $query The query to be executed quoted in PDO style |
||
| 168 | * @param false|array<mixed> $bindData The data to be bound to the query object. |
||
| 169 | * @return array<mixed> |
||
| 170 | */ |
||
| 171 | public function query($query, $bindData = []) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Runs a query but ensures that all identifiers are properly quoted by calling |
||
| 194 | * the Driver::quoteQueryIdentifiers method on the query before executing it. |
||
| 195 | * |
||
| 196 | * @param string $query |
||
| 197 | * @param false|array<mixed> $bindData |
||
| 198 | * @return array<mixed> |
||
| 199 | */ |
||
| 200 | public function quotedQuery($query, $bindData = false) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Expands the configuration array into a format that can easily be passed |
||
| 207 | * to PDO. |
||
| 208 | * |
||
| 209 | * @param array $params The query parameters |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | private function expand($params) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * This method provides a system independent way of quoting identifiers in |
||
| 234 | * queries. By default all identifiers can be quoted with double quotes ("). |
||
| 235 | * When a query quoted with double quotes is passed through this method the |
||
| 236 | * output generated has the double quotes replaced with the quoting character |
||
| 237 | * of the target database platform. |
||
| 238 | * |
||
| 239 | * @param string $query |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function quoteQueryIdentifiers($query) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns an array description of the schema represented by the connection. |
||
| 253 | * The description returns contains information about `tables`, `columns`, `keys`, |
||
| 254 | * `constraints`, `views` and `indices`. |
||
| 255 | * |
||
| 256 | * @return array<mixed> |
||
| 257 | */ |
||
| 258 | public function describe() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Returns the description of a database table as an associative array. |
||
| 265 | * |
||
| 266 | * @param string $table |
||
| 267 | * @return array<mixed> |
||
| 268 | */ |
||
| 269 | public function describeTable($table) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * A wrapper arround PDO's beginTransaction method which uses a static reference |
||
| 284 | * counter to implement nested transactions. |
||
| 285 | */ |
||
| 286 | public function beginTransaction() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * A wrapper around PDO's commit transaction method which uses a static reference |
||
| 295 | * counter to implement nested transactions. |
||
| 296 | */ |
||
| 297 | public function commit() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * A wrapper around PDO's rollback transaction methd which rolls back all |
||
| 306 | * activities performed since the first call to begin transaction. |
||
| 307 | * Unfortunately, transactions cannot be rolled back in a nested fashion. |
||
| 308 | */ |
||
| 309 | public function rollback() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Return the underlying PDO object. |
||
| 317 | * @return \PDO |
||
| 318 | */ |
||
| 319 | public function getPDO() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns an instance of a descriptor for a given driver. |
||
| 326 | * @return \atiaa\Descriptor |
||
| 327 | */ |
||
| 328 | private function getDescriptor() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * A wrapper around PDO's lastInsertId() method. |
||
| 339 | * @return mixed |
||
| 340 | */ |
||
| 341 | public function getLastInsertId() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Specify the default schema to use in cases where a schema is not provided |
||
| 348 | * as part of the table reference. |
||
| 349 | * @param string $defaultSchema |
||
| 350 | */ |
||
| 351 | public function setDefaultSchema($defaultSchema) |
||
| 355 | |||
| 356 | abstract protected function getDriverName(); |
||
| 357 | |||
| 358 | abstract public function quoteIdentifier($identifier); |
||
| 359 | |||
| 360 | public function setCleanDefaults($cleanDefaults) |
||
| 364 | |||
| 365 | } |
||
| 366 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.