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 DriverInterface|string $driver a driver instance or a connection string |
||
| 29 | */ |
||
| 30 | 7 | public function __construct($driver) { |
|
| 33 | /** |
||
| 34 | * Create a driver instance from a connection string |
||
| 35 | * @param string $connectionString the connection string |
||
| 36 | * @return DriverInterface |
||
| 37 | */ |
||
| 38 | 7 | public static function getDriver(string $connectionString) |
|
| 83 | /** |
||
| 84 | * Prepare a statement. |
||
| 85 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 86 | * |
||
| 87 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 88 | * @return StatementInterface the prepared statement |
||
| 89 | */ |
||
| 90 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 94 | /** |
||
| 95 | * Test the connection |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | 1 | public function test() : bool |
|
| 128 | /** |
||
| 129 | * Run a query (prepare & execute). |
||
| 130 | * @param string $sql SQL query |
||
| 131 | * @param array|null $par parameters (optional) |
||
| 132 | * @return ResultInterface the result of the execution |
||
| 133 | */ |
||
| 134 | 62 | public function query(string $sql, $par = null) : ResultInterface |
|
| 142 | /** |
||
| 143 | * Run a SELECT query and get an array-like result. |
||
| 144 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 145 | * |
||
| 146 | * @param string $sql SQL query |
||
| 147 | * @param array $par parameters |
||
| 148 | * @param string $key column name to use as the array index |
||
| 149 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 150 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 151 | * |
||
| 152 | * @return Collection the result of the execution |
||
| 153 | */ |
||
| 154 | 56 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
| 177 | /** |
||
| 178 | * Run a SELECT query and get a single row |
||
| 179 | * @param string $sql SQL query |
||
| 180 | * @param array $par parameters |
||
| 181 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 182 | * @return mixed the result of the execution |
||
| 183 | */ |
||
| 184 | 21 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 188 | /** |
||
| 189 | * Run a SELECT query and get an array |
||
| 190 | * @param string $sql SQL query |
||
| 191 | * @param array $par parameters |
||
| 192 | * @param string $key column name to use as the array index |
||
| 193 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 194 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 195 | * @return Collection the result of the execution |
||
| 196 | */ |
||
| 197 | 2 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 201 | /** |
||
| 202 | * Begin a transaction. |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | 1 | public function begin() : DBInterface |
|
| 212 | /** |
||
| 213 | * Commit a transaction. |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | 1 | public function commit() : DBInterface |
|
| 223 | /** |
||
| 224 | * Rollback a transaction. |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | 1 | public function rollback() : DBInterface |
|
| 234 | /** |
||
| 235 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 236 | * @return string the current driver name |
||
| 237 | */ |
||
| 238 | 6 | public function driverName() : string |
|
| 242 | /** |
||
| 243 | * Get an option from the driver |
||
| 244 | * |
||
| 245 | * @param string $key the option name |
||
| 246 | * @param mixed $default the default value to return if the option key is not defined |
||
| 247 | * @return mixed the option value |
||
| 248 | */ |
||
| 249 | 22 | public function driverOption(string $key, $default = null) |
|
| 253 | |||
| 254 | 50 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 260 | /** |
||
| 261 | * Parse all tables from the database. |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | public function parseSchema() |
||
| 269 | /** |
||
| 270 | * Get the full schema as an array that you can serialize and store |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function getSchema($asPlainArray = true) |
||
| 302 | /** |
||
| 303 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 304 | * @param array $data the schema definition |
||
| 305 | * @return $this |
||
| 306 | */ |
||
| 307 | public function setSchema(array $data) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Initialize a table query |
||
| 339 | * @param string $table the table to query |
||
| 340 | * @return TableQuery |
||
| 341 | */ |
||
| 342 | 50 | public function table($table, bool $mapped = false) |
|
| 352 | } |