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 string $connectionString a driver instance or a connection string  | 
            ||
| 29 | */  | 
            ||
| 30 | 15 | public function __construct(string $connectionString)  | 
            |
| 31 |     { | 
            ||
| 32 | $connection = [  | 
            ||
| 33 | 15 | 'orig' => $connectionString,  | 
            |
| 34 | 'type' => null,  | 
            ||
| 35 | 'user' => null,  | 
            ||
| 36 | 'pass' => null,  | 
            ||
| 37 | 'host' => null,  | 
            ||
| 38 | 'port' => null,  | 
            ||
| 39 | 'name' => null,  | 
            ||
| 40 | 'opts' => []  | 
            ||
| 41 | ];  | 
            ||
| 42 | $aliases = [  | 
            ||
| 43 | 15 | 'my' => 'mysql',  | 
            |
| 44 | 'mysqli' => 'mysql',  | 
            ||
| 45 | 'pg' => 'postgre',  | 
            ||
| 46 | 'oci' => 'oracle',  | 
            ||
| 47 | 'firebird' => 'ibase'  | 
            ||
| 48 | ];  | 
            ||
| 49 | 15 | $temp = parse_url($connectionString);  | 
            |
| 50 | 15 |         if ($temp === false || (isset($temp['query']) && strpos($temp['query'], 'regexparser=1') !== false)) { | 
            |
| 51 | 6 | if (!preg_match(  | 
            |
| 52 | 6 | '(^  | 
            |
| 53 | (?<scheme>.*?)://  | 
            ||
| 54 | (?:(?<user>.*?)(?:\:(?<pass>.*))?@)?  | 
            ||
| 55 | (?<host>[a-zа-я.\-_0-9=();:]+?) # added =();: for oracle and pdo configs  | 
            ||
| 56 | (?:\:(?<port>\d+))?  | 
            ||
| 57 | (?<path>/.+?)? # path is optional for oracle and pdo configs  | 
            ||
| 58 | (?:\?(?<query>.*))?  | 
            ||
| 59 | $)xui',  | 
            ||
| 60 | 6 | $connectionString,  | 
            |
| 61 | 6 | $temp  | 
            |
| 62 |             )) { | 
            ||
| 63 |                 throw new DBException('Could not parse connection string'); | 
            ||
| 64 | }  | 
            ||
| 65 | }  | 
            ||
| 66 | 15 | $connection['type'] = isset($temp['scheme']) && strlen($temp['scheme']) ? $temp['scheme'] : null;  | 
            |
| 67 | 15 | $connection['user'] = isset($temp['user']) && strlen($temp['user']) ? $temp['user'] : null;  | 
            |
| 68 | 15 | $connection['pass'] = isset($temp['pass']) && strlen($temp['pass']) ? $temp['pass'] : null;  | 
            |
| 69 | 15 | $connection['host'] = isset($temp['host']) && strlen($temp['host']) ? $temp['host'] : null;  | 
            |
| 70 | 15 | $connection['name'] = isset($temp['path']) && strlen($temp['path']) ? trim($temp['path'], '/') : null;  | 
            |
| 71 | 15 | $connection['port'] = isset($temp['port']) && (int)$temp['port'] ? (int)$temp['port'] : null;  | 
            |
| 72 | 15 |         if (isset($temp['query']) && strlen($temp['query'])) { | 
            |
| 73 | 15 | parse_str($temp['query'], $connection['opts']);  | 
            |
| 74 | }  | 
            ||
| 75 | // create the driver  | 
            ||
| 76 | 15 | $connection['type'] = $aliases[$connection['type']] ?? $connection['type'];  | 
            |
| 77 | 15 | $tmp = '\\vakata\\database\\driver\\'.strtolower($connection['type']).'\\Driver';  | 
            |
| 78 | 15 |         if (!class_exists($tmp)) { | 
            |
| 79 |             throw new DBException('Unknown DB backend'); | 
            ||
| 80 | }  | 
            ||
| 81 | 15 | $this->driver = new $tmp($connection);  | 
            |
| 82 | 15 | }  | 
            |
| 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 |     { | 
            ||
| 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(string $table, bool $mapped = false)  | 
            |
| 361 | }  | 
            ||
| 362 |