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) { |
|
| 33 | /** |
||
| 34 | * Create a driver instance from a connection string |
||
| 35 | * @param string $connectionString the connection string |
||
| 36 | * @return DriverInterface |
||
| 37 | */ |
||
| 38 | 15 | public static function getDriver(string $connectionString) |
|
| 39 | { |
||
| 40 | $connection = [ |
||
| 41 | 15 | 'orig' => $connectionString, |
|
| 42 | 'type' => null, |
||
| 43 | 'user' => null, |
||
| 44 | 'pass' => null, |
||
| 45 | 'host' => null, |
||
| 46 | 'port' => null, |
||
| 47 | 'name' => null, |
||
| 48 | 'opts' => [] |
||
| 49 | ]; |
||
| 50 | $aliases = [ |
||
| 51 | 15 | 'mysqli' => 'mysql', |
|
| 52 | 'pg' => 'postgre', |
||
| 53 | 'oci' => 'oracle', |
||
| 54 | 'firebird' => 'ibase' |
||
| 55 | ]; |
||
| 56 | 15 | $connectionString = array_pad(explode('://', $connectionString, 2), 2, ''); |
|
| 57 | 15 | $connection['type'] = $connectionString[0]; |
|
| 58 | 15 | $connectionString = $connectionString[1]; |
|
| 59 | |||
| 60 | 15 | $host = substr($connectionString, 0, strrpos($connectionString, '/')); |
|
| 61 | 15 | $path = substr($connectionString, strrpos($connectionString, '/') + 1); |
|
| 62 | |||
| 63 | 15 | if (strpos($host, '@') !== false) { |
|
| 64 | 9 | $auth = substr($host, 0, strrpos($host, '@')); |
|
| 65 | 9 | $host = substr($host, strrpos($host, '@') + 1); |
|
| 66 | 9 | list($connection['user'], $connection['pass']) = array_pad(explode(':', $auth, 2), 2, ''); |
|
| 67 | } |
||
| 68 | 15 | list($connection['host'], $connection['port']) = array_pad(explode(':', $host, 2), 2, null); |
|
| 69 | |||
| 70 | 15 | if ($pos = strrpos($path, '?')) { |
|
| 71 | 15 | $opt = substr($path, $pos + 1); |
|
| 72 | 15 | parse_str($opt, $connection['opts']); |
|
| 73 | 15 | $connection['name'] = substr($path, 0, $pos); |
|
| 74 | } else { |
||
| 75 | 1 | $connection['name'] = $path; |
|
| 76 | } |
||
| 77 | 15 | $connection['type'] = isset($aliases[$connection['type']]) ? |
|
| 78 | $aliases[$connection['type']] : |
||
| 79 | 15 | $connection['type']; |
|
| 80 | 15 | $tmp = '\\vakata\\database\\driver\\'.strtolower($connection['type']).'\\Driver'; |
|
| 81 | 15 | return new $tmp($connection); |
|
| 82 | } |
||
| 83 | /** |
||
| 84 | * Prepare a statement. |
||
| 85 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 86 | * |
||
| 87 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 88 | * @return StatementInterface the prepared statement |
||
| 89 | */ |
||
| 90 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 94 | /** |
||
| 95 | * Test the connection |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | 1 | public function test() : bool |
|
| 128 | /** |
||
| 129 | * Run a query (prepare & execute). |
||
| 130 | * @param string $sql SQL query |
||
| 131 | * @param array|null $par parameters (optional) |
||
| 132 | * @return ResultInterface the result of the execution |
||
| 133 | */ |
||
| 134 | 166 | public function query(string $sql, $par = null) : ResultInterface |
|
| 142 | /** |
||
| 143 | * Run a SELECT query and get an array-like result. |
||
| 144 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 145 | * |
||
| 146 | * @param string $sql SQL query |
||
| 147 | * @param array $par parameters |
||
| 148 | * @param string $key column name to use as the array index |
||
| 149 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 150 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 151 | * |
||
| 152 | * @return Collection the result of the execution |
||
| 153 | */ |
||
| 154 | 160 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
| 177 | /** |
||
| 178 | * Run a SELECT query and get a single row |
||
| 179 | * @param string $sql SQL query |
||
| 180 | * @param array $par parameters |
||
| 181 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 182 | * @return mixed the result of the execution |
||
| 183 | */ |
||
| 184 | 63 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 188 | /** |
||
| 189 | * Run a SELECT query and get an array |
||
| 190 | * @param string $sql SQL query |
||
| 191 | * @param array $par parameters |
||
| 192 | * @param string $key column name to use as the array index |
||
| 193 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 194 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 195 | * @return Collection the result of the execution |
||
| 196 | */ |
||
| 197 | 6 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 201 | /** |
||
| 202 | * Begin a transaction. |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | 1 | public function begin() : DBInterface |
|
| 212 | /** |
||
| 213 | * Commit a transaction. |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | 1 | public function commit() : DBInterface |
|
| 223 | /** |
||
| 224 | * Rollback a transaction. |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | 1 | public function rollback() : DBInterface |
|
| 234 | /** |
||
| 235 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 236 | * @return string the current driver name |
||
| 237 | */ |
||
| 238 | 16 | public function driverName() : string |
|
| 242 | /** |
||
| 243 | * Get an option from the driver |
||
| 244 | * |
||
| 245 | * @param string $key the option name |
||
| 246 | * @param mixed $default the default value to return if the option key is not defined |
||
| 247 | * @return mixed the option value |
||
| 248 | */ |
||
| 249 | 76 | public function driverOption(string $key, $default = null) |
|
| 253 | |||
| 254 | 156 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 260 | /** |
||
| 261 | * Parse all tables from the database. |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | public function parseSchema() |
||
| 269 | /** |
||
| 270 | * Get the full schema as an array that you can serialize and store |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function getSchema($asPlainArray = true) |
||
| 302 | /** |
||
| 303 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 304 | * @param array $data the schema definition |
||
| 305 | * @return $this |
||
| 306 | */ |
||
| 307 | public function setSchema(array $data) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Initialize a table query |
||
| 339 | * @param string $table the table to query |
||
| 340 | * @return TableQuery |
||
| 341 | */ |
||
| 342 | 156 | public function table($table, bool $mapped = false) |
|
| 352 | } |