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 | * @param bool $buff should the results be buffered (defaults to true) |
||
| 145 | * @return ResultInterface the result of the execution |
||
| 146 | */ |
||
| 147 | 166 | public function query(string $sql, $par = null, bool $buff = true) : ResultInterface |
|
| 155 | /** |
||
| 156 | * Run a SELECT query and get an array-like result. |
||
| 157 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 158 | * |
||
| 159 | * @param string $sql SQL query |
||
| 160 | * @param array $par parameters |
||
| 161 | * @param string $key column name to use as the array index |
||
| 162 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 163 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 164 | * @param bool $buff should the results be buffered (defaults to `false`) |
||
| 165 | * |
||
| 166 | * @return Collection the result of the execution |
||
| 167 | */ |
||
| 168 | 160 | public function get( |
|
| 204 | /** |
||
| 205 | * Run a SELECT query and get a single row |
||
| 206 | * @param string $sql SQL query |
||
| 207 | * @param array $par parameters |
||
| 208 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 209 | * @return mixed the result of the execution |
||
| 210 | */ |
||
| 211 | 63 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 215 | /** |
||
| 216 | * Run a SELECT query and get an array |
||
| 217 | * @param string $sql SQL query |
||
| 218 | * @param array $par parameters |
||
| 219 | * @param string $key column name to use as the array index |
||
| 220 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 221 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 222 | * @return array the result of the execution |
||
| 223 | */ |
||
| 224 | 6 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 237 | /** |
||
| 238 | * Begin a transaction. |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | 1 | public function begin() : DBInterface |
|
| 248 | /** |
||
| 249 | * Commit a transaction. |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | 1 | public function commit() : DBInterface |
|
| 259 | /** |
||
| 260 | * Rollback a transaction. |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | 1 | public function rollback() : DBInterface |
|
| 270 | /** |
||
| 271 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 272 | * @return string the current driver name |
||
| 273 | */ |
||
| 274 | 16 | public function driverName() : string |
|
| 278 | /** |
||
| 279 | * Get an option from the driver |
||
| 280 | * |
||
| 281 | * @param string $key the option name |
||
| 282 | * @param mixed $default the default value to return if the option key is not defined |
||
| 283 | * @return mixed the option value |
||
| 284 | */ |
||
| 285 | 76 | public function driverOption(string $key, $default = null) |
|
| 289 | |||
| 290 | 156 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 296 | /** |
||
| 297 | * Parse all tables from the database. |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | public function parseSchema() |
||
| 305 | /** |
||
| 306 | * Get the full schema as an array that you can serialize and store |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function getSchema($asPlainArray = true) |
||
| 339 | /** |
||
| 340 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 341 | * @param array $data the schema definition |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function setSchema(array $data) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Initialize a table query |
||
| 376 | * @param string $table the table to query |
||
| 377 | * @return TableQuery |
||
| 378 | */ |
||
| 379 | 156 | public function table(string $table, bool $mapped = false) |
|
| 389 | } |
||
| 390 |