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 query. |
||
| 157 | * @param string $sql SQL query |
||
| 158 | * @return mixed the result of the execution |
||
| 159 | */ |
||
| 160 | public function raw(string $sql) |
||
| 164 | /** |
||
| 165 | * Run a SELECT query and get an array-like result. |
||
| 166 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 167 | * |
||
| 168 | * @param string $sql SQL query |
||
| 169 | * @param array $par parameters |
||
| 170 | * @param string $key column name to use as the array index |
||
| 171 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 172 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 173 | * @param bool $buff should the results be buffered (defaults to `false`) |
||
| 174 | * |
||
| 175 | * @return Collection the result of the execution |
||
| 176 | */ |
||
| 177 | 160 | public function get( |
|
| 213 | /** |
||
| 214 | * Run a SELECT query and get a single row |
||
| 215 | * @param string $sql SQL query |
||
| 216 | * @param array $par parameters |
||
| 217 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 218 | * @return mixed the result of the execution |
||
| 219 | */ |
||
| 220 | 63 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 224 | /** |
||
| 225 | * Run a SELECT query and get an array |
||
| 226 | * @param string $sql SQL query |
||
| 227 | * @param array $par parameters |
||
| 228 | * @param string $key column name to use as the array index |
||
| 229 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 230 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 231 | * @return array the result of the execution |
||
| 232 | */ |
||
| 233 | 6 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 246 | /** |
||
| 247 | * Begin a transaction. |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | 1 | public function begin() : DBInterface |
|
| 257 | /** |
||
| 258 | * Commit a transaction. |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | 1 | public function commit() : DBInterface |
|
| 268 | /** |
||
| 269 | * Rollback a transaction. |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | 1 | public function rollback() : DBInterface |
|
| 279 | /** |
||
| 280 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 281 | * @return string the current driver name |
||
| 282 | */ |
||
| 283 | 16 | public function driverName() : string |
|
| 287 | /** |
||
| 288 | * Get an option from the driver |
||
| 289 | * |
||
| 290 | * @param string $key the option name |
||
| 291 | * @param mixed $default the default value to return if the option key is not defined |
||
| 292 | * @return mixed the option value |
||
| 293 | */ |
||
| 294 | 76 | public function driverOption(string $key, $default = null) |
|
| 298 | |||
| 299 | 156 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 305 | /** |
||
| 306 | * Parse all tables from the database. |
||
| 307 | * @return $this |
||
| 308 | */ |
||
| 309 | public function parseSchema() |
||
| 314 | /** |
||
| 315 | * Get the full schema as an array that you can serialize and store |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function getSchema($asPlainArray = true) |
||
| 348 | /** |
||
| 349 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 350 | * @param array $data the schema definition |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | public function setSchema(array $data) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Initialize a table query |
||
| 385 | * @param string $table the table to query |
||
| 386 | * @return TableQuery |
||
| 387 | */ |
||
| 388 | 156 | public function table(string $table, bool $mapped = false) |
|
| 398 | } |
||
| 399 |