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) |
|
| 89 | |||
| 90 | public function driver(): DriverInterface |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Prepare a statement. |
||
| 97 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 98 | * |
||
| 99 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 100 | * @return StatementInterface the prepared statement |
||
| 101 | */ |
||
| 102 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 106 | /** |
||
| 107 | * Test the connection |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | 1 | public function test() : bool |
|
| 140 | /** |
||
| 141 | * Run a query (prepare & execute). |
||
| 142 | * @param string $sql SQL query |
||
| 143 | * @param mixed $par parameters (optional) |
||
| 144 | * @return ResultInterface the result of the execution |
||
| 145 | */ |
||
| 146 | 166 | public function query(string $sql, $par = null) : ResultInterface |
|
| 154 | /** |
||
| 155 | * Run a SELECT query and get an array-like result. |
||
| 156 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 157 | * |
||
| 158 | * @param string $sql SQL query |
||
| 159 | * @param array $par parameters |
||
| 160 | * @param string $key column name to use as the array index |
||
| 161 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 162 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 163 | * |
||
| 164 | * @return Collection the result of the execution |
||
| 165 | */ |
||
| 166 | 160 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
| 196 | /** |
||
| 197 | * Run a SELECT query and get a single row |
||
| 198 | * @param string $sql SQL query |
||
| 199 | * @param array $par parameters |
||
| 200 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 201 | * @return mixed the result of the execution |
||
| 202 | */ |
||
| 203 | 63 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 207 | /** |
||
| 208 | * Run a SELECT query and get an array |
||
| 209 | * @param string $sql SQL query |
||
| 210 | * @param array $par parameters |
||
| 211 | * @param string $key column name to use as the array index |
||
| 212 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 213 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 214 | * @return array the result of the execution |
||
| 215 | */ |
||
| 216 | 6 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 220 | /** |
||
| 221 | * Begin a transaction. |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | 1 | public function begin() : DBInterface |
|
| 231 | /** |
||
| 232 | * Commit a transaction. |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | 1 | public function commit() : DBInterface |
|
| 242 | /** |
||
| 243 | * Rollback a transaction. |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | 1 | public function rollback() : DBInterface |
|
| 253 | /** |
||
| 254 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 255 | * @return string the current driver name |
||
| 256 | */ |
||
| 257 | 16 | public function driverName() : string |
|
| 261 | /** |
||
| 262 | * Get an option from the driver |
||
| 263 | * |
||
| 264 | * @param string $key the option name |
||
| 265 | * @param mixed $default the default value to return if the option key is not defined |
||
| 266 | * @return mixed the option value |
||
| 267 | */ |
||
| 268 | 76 | public function driverOption(string $key, $default = null) |
|
| 272 | |||
| 273 | 156 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 279 | /** |
||
| 280 | * Parse all tables from the database. |
||
| 281 | * @return $this |
||
| 282 | */ |
||
| 283 | public function parseSchema() |
||
| 288 | /** |
||
| 289 | * Get the full schema as an array that you can serialize and store |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function getSchema($asPlainArray = true) |
||
| 322 | /** |
||
| 323 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 324 | * @param array $data the schema definition |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | public function setSchema(array $data) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Initialize a table query |
||
| 359 | * @param string $table the table to query |
||
| 360 | * @return TableQuery |
||
| 361 | */ |
||
| 362 | 156 | public function table(string $table, bool $mapped = false) |
|
| 372 | } |
||
| 373 |