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) |
|
| 83 | |||
| 84 | public function driver(): DriverInterface |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Prepare a statement. |
||
| 91 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 92 | * |
||
| 93 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 94 | * @return StatementInterface the prepared statement |
||
| 95 | */ |
||
| 96 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 100 | /** |
||
| 101 | * Test the connection |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | 1 | public function test() : bool |
|
| 134 | /** |
||
| 135 | * Run a query (prepare & execute). |
||
| 136 | * @param string $sql SQL query |
||
| 137 | * @param mixed $par parameters (optional) |
||
| 138 | * @return ResultInterface the result of the execution |
||
| 139 | */ |
||
| 140 | 166 | public function query(string $sql, $par = null) : ResultInterface |
|
| 148 | /** |
||
| 149 | * Run a SELECT query and get an array-like result. |
||
| 150 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 151 | * |
||
| 152 | * @param string $sql SQL query |
||
| 153 | * @param array $par parameters |
||
| 154 | * @param string $key column name to use as the array index |
||
| 155 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 156 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 157 | * |
||
| 158 | * @return Collection the result of the execution |
||
| 159 | */ |
||
| 160 | 160 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
| 190 | /** |
||
| 191 | * Run a SELECT query and get a single row |
||
| 192 | * @param string $sql SQL query |
||
| 193 | * @param array $par parameters |
||
| 194 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 195 | * @return mixed the result of the execution |
||
| 196 | */ |
||
| 197 | 63 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 201 | /** |
||
| 202 | * Run a SELECT query and get an array |
||
| 203 | * @param string $sql SQL query |
||
| 204 | * @param array $par parameters |
||
| 205 | * @param string $key column name to use as the array index |
||
| 206 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 207 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 208 | * @return array the result of the execution |
||
| 209 | */ |
||
| 210 | 6 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 214 | /** |
||
| 215 | * Begin a transaction. |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | 1 | public function begin() : DBInterface |
|
| 225 | /** |
||
| 226 | * Commit a transaction. |
||
| 227 | * @return $this |
||
| 228 | */ |
||
| 229 | 1 | public function commit() : DBInterface |
|
| 236 | /** |
||
| 237 | * Rollback a transaction. |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | 1 | public function rollback() : DBInterface |
|
| 247 | /** |
||
| 248 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 249 | * @return string the current driver name |
||
| 250 | */ |
||
| 251 | 16 | public function driverName() : string |
|
| 255 | /** |
||
| 256 | * Get an option from the driver |
||
| 257 | * |
||
| 258 | * @param string $key the option name |
||
| 259 | * @param mixed $default the default value to return if the option key is not defined |
||
| 260 | * @return mixed the option value |
||
| 261 | */ |
||
| 262 | 76 | public function driverOption(string $key, $default = null) |
|
| 266 | |||
| 267 | 156 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 273 | /** |
||
| 274 | * Parse all tables from the database. |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | public function parseSchema() |
||
| 282 | /** |
||
| 283 | * Get the full schema as an array that you can serialize and store |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getSchema($asPlainArray = true) |
||
| 316 | /** |
||
| 317 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 318 | * @param array $data the schema definition |
||
| 319 | * @return $this |
||
| 320 | */ |
||
| 321 | public function setSchema(array $data) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Initialize a table query |
||
| 353 | * @param string $table the table to query |
||
| 354 | * @return TableQuery |
||
| 355 | */ |
||
| 356 | 156 | public function table(string $table, bool $mapped = false) |
|
| 366 | } |
||
| 367 |