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