Complex classes like DB 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 DB, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class DB implements DBInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var DriverInterface |
||
| 18 | */ |
||
| 19 | protected $driver; |
||
| 20 | /** |
||
| 21 | * @var Table[] |
||
| 22 | */ |
||
| 23 | protected $tables = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Create an instance. |
||
| 27 | * |
||
| 28 | * @param DriverInterface|string $driver a driver instance or a connection string |
||
| 29 | */ |
||
| 30 | 15 | public function __construct($driver) |
|
| 34 | /** |
||
| 35 | * Create a driver instance from a connection string |
||
| 36 | * @param string $connectionString the connection string |
||
| 37 | * @return DriverInterface |
||
| 38 | */ |
||
| 39 | 15 | public static function getDriver(string $connectionString) |
|
| 40 | { |
||
| 41 | $connection = [ |
||
| 42 | 15 | 'orig' => $connectionString, |
|
| 43 | 'type' => null, |
||
| 44 | 'user' => null, |
||
| 45 | 'pass' => null, |
||
| 46 | 'host' => null, |
||
| 47 | 'port' => null, |
||
| 48 | 'name' => null, |
||
| 49 | 'opts' => [] |
||
| 50 | ]; |
||
| 51 | $aliases = [ |
||
| 52 | 15 | 'mysqli' => 'mysql', |
|
| 53 | 'pg' => 'postgre', |
||
| 54 | 'oci' => 'oracle', |
||
| 55 | 'firebird' => 'ibase' |
||
| 56 | ]; |
||
| 57 | 15 | $connectionString = array_pad(explode('://', $connectionString, 2), 2, ''); |
|
| 58 | 15 | $connection['type'] = $connectionString[0]; |
|
| 59 | 15 | $connectionString = $connectionString[1]; |
|
| 60 | |||
| 61 | 15 | $host = substr($connectionString, 0, strrpos($connectionString, '/')); |
|
| 62 | 15 | $path = substr($connectionString, strrpos($connectionString, '/') + 1); |
|
| 63 | |||
| 64 | 15 | if (strpos($host, '@') !== false) { |
|
| 65 | 9 | $auth = substr($host, 0, strrpos($host, '@')); |
|
| 66 | 9 | $host = substr($host, strrpos($host, '@') + 1); |
|
| 67 | 9 | list($connection['user'], $connection['pass']) = array_pad(explode(':', $auth, 2), 2, ''); |
|
| 68 | } |
||
| 69 | 15 | list($connection['host'], $connection['port']) = array_pad(explode(':', $host, 2), 2, null); |
|
| 70 | |||
| 71 | 15 | if ($pos = strrpos($path, '?')) { |
|
| 72 | 15 | $opt = substr($path, $pos + 1); |
|
| 73 | 15 | parse_str($opt, $connection['opts']); |
|
| 74 | 15 | $connection['name'] = substr($path, 0, $pos); |
|
| 75 | } else { |
||
| 76 | 1 | $connection['name'] = $path; |
|
| 77 | } |
||
| 78 | 15 | $connection['type'] = isset($aliases[$connection['type']]) ? |
|
| 79 | $aliases[$connection['type']] : |
||
| 80 | 15 | $connection['type']; |
|
| 81 | 15 | $tmp = '\\vakata\\database\\driver\\'.strtolower($connection['type']).'\\Driver'; |
|
| 82 | 15 | return new $tmp($connection); |
|
| 83 | } |
||
| 84 | /** |
||
| 85 | * Prepare a statement. |
||
| 86 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 87 | * |
||
| 88 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 89 | * @return StatementInterface the prepared statement |
||
| 90 | */ |
||
| 91 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 92 | { |
||
| 93 | 1 | return $this->driver->prepare($sql); |
|
| 94 | } |
||
| 95 | /** |
||
| 96 | * Test the connection |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | 1 | public function test() : bool |
|
| 104 | 27 | protected function expand(string $sql, $par = null) : array |
|
| 105 | { |
||
| 106 | 27 | $new = ''; |
|
| 129 | /** |
||
| 130 | * Run a query (prepare & execute). |
||
| 131 | * @param string $sql SQL query |
||
| 132 | * @param mixed $par parameters (optional) |
||
| 133 | * @return ResultInterface the result of the execution |
||
| 134 | */ |
||
| 135 | 166 | public function query(string $sql, $par = null) : ResultInterface |
|
| 143 | /** |
||
| 144 | * Run a SELECT query and get an array-like result. |
||
| 145 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 146 | * |
||
| 147 | * @param string $sql SQL query |
||
| 148 | * @param array $par parameters |
||
| 149 | * @param string $key column name to use as the array index |
||
| 150 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 151 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 152 | * |
||
| 153 | * @return Collection the result of the execution |
||
| 154 | */ |
||
| 155 | 160 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
| 185 | /** |
||
| 186 | * Run a SELECT query and get a single row |
||
| 187 | * @param string $sql SQL query |
||
| 188 | * @param array $par parameters |
||
| 189 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 190 | * @return mixed the result of the execution |
||
| 191 | */ |
||
| 192 | 63 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 196 | /** |
||
| 197 | * Run a SELECT query and get an array |
||
| 198 | * @param string $sql SQL query |
||
| 199 | * @param array $par parameters |
||
| 200 | * @param string $key column name to use as the array index |
||
| 201 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 202 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 203 | * @return array the result of the execution |
||
| 204 | */ |
||
| 205 | 6 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 209 | /** |
||
| 210 | * Begin a transaction. |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | 1 | public function begin() : DBInterface |
|
| 220 | /** |
||
| 221 | * Commit a transaction. |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | 1 | public function commit() : DBInterface |
|
| 231 | /** |
||
| 232 | * Rollback a transaction. |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | 1 | public function rollback() : DBInterface |
|
| 242 | /** |
||
| 243 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 244 | * @return string the current driver name |
||
| 245 | */ |
||
| 246 | 16 | public function driverName() : string |
|
| 250 | /** |
||
| 251 | * Get an option from the driver |
||
| 252 | * |
||
| 253 | * @param string $key the option name |
||
| 254 | * @param mixed $default the default value to return if the option key is not defined |
||
| 255 | * @return mixed the option value |
||
| 256 | */ |
||
| 257 | 76 | public function driverOption(string $key, $default = null) |
|
| 261 | |||
| 262 | 156 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 268 | /** |
||
| 269 | * Parse all tables from the database. |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | public function parseSchema() |
||
| 277 | /** |
||
| 278 | * Get the full schema as an array that you can serialize and store |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | public function getSchema($asPlainArray = true) |
||
| 311 | /** |
||
| 312 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 313 | * @param array $data the schema definition |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function setSchema(array $data) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Initialize a table query |
||
| 348 | * @param string $table the table to query |
||
| 349 | * @return TableQuery |
||
| 350 | */ |
||
| 351 | 156 | public function table($table, bool $mapped = false) |
|
| 361 | } |
||
| 362 |