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 |
||
| 13 | class DB implements DBInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var DriverInterface |
||
| 17 | */ |
||
| 18 | protected $driver; |
||
| 19 | /** |
||
| 20 | * @var Table[] |
||
| 21 | */ |
||
| 22 | protected $tables = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create an instance. |
||
| 26 | * |
||
| 27 | * @param DriverInterface|string $driver a driver instance or a connection string |
||
| 28 | */ |
||
| 29 | 5 | public function __construct($driver) { |
|
| 32 | /** |
||
| 33 | * Create a driver instance from a connection string |
||
| 34 | * @param string $connectionString the connection string |
||
| 35 | * @return DriverInterface |
||
| 36 | */ |
||
| 37 | 5 | public static function getDriver(string $connectionString) |
|
| 82 | /** |
||
| 83 | * Prepare a statement. |
||
| 84 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
| 85 | * |
||
| 86 | * @param string $sql the query to prepare - use `?` for arguments |
||
| 87 | * @return StatementInterface the prepared statement |
||
| 88 | */ |
||
| 89 | 1 | public function prepare(string $sql) : StatementInterface |
|
| 93 | /** |
||
| 94 | * Test the connection |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | 1 | public function test() : bool |
|
| 127 | /** |
||
| 128 | * Run a query (prepare & execute). |
||
| 129 | * @param string $sql SQL query |
||
| 130 | * @param array|null $par parameters (optional) |
||
| 131 | * @return ResultInterface the result of the execution |
||
| 132 | */ |
||
| 133 | 31 | public function query(string $sql, $par = null) : ResultInterface |
|
| 141 | /** |
||
| 142 | * Run a SELECT query and get an array-like result. |
||
| 143 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
| 144 | * |
||
| 145 | * @param string $sql SQL query |
||
| 146 | * @param array $par parameters |
||
| 147 | * @param string $key column name to use as the array index |
||
| 148 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 149 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 150 | * |
||
| 151 | * @return Collection the result of the execution |
||
| 152 | */ |
||
| 153 | 25 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
| 176 | /** |
||
| 177 | * Run a SELECT query and get a single row |
||
| 178 | * @param string $sql SQL query |
||
| 179 | * @param array $par parameters |
||
| 180 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 181 | * @return Collection the result of the execution |
||
| 182 | */ |
||
| 183 | 10 | public function one(string $sql, $par = null, bool $opti = true) |
|
| 187 | /** |
||
| 188 | * Run a SELECT query and get an array |
||
| 189 | * @param string $sql SQL query |
||
| 190 | * @param array $par parameters |
||
| 191 | * @param string $key column name to use as the array index |
||
| 192 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
| 193 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
| 194 | * @return Collection the result of the execution |
||
| 195 | */ |
||
| 196 | 2 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
| 200 | /** |
||
| 201 | * Begin a transaction. |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | 1 | public function begin() : DBInterface |
|
| 211 | /** |
||
| 212 | * Commit a transaction. |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | 1 | public function commit() : DBInterface |
|
| 222 | /** |
||
| 223 | * Rollback a transaction. |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | 1 | public function rollback() : DBInterface |
|
| 233 | /** |
||
| 234 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
| 235 | * @return string the current driver name |
||
| 236 | */ |
||
| 237 | 3 | public function driverName() : string |
|
| 241 | /** |
||
| 242 | * Get an option from the driver |
||
| 243 | * |
||
| 244 | * @param string $key the option name |
||
| 245 | * @param mixed $default the default value to return if the option key is not defined |
||
| 246 | * @return mixed the option value |
||
| 247 | */ |
||
| 248 | 9 | public function driverOption(string $key, $default = null) |
|
| 249 | { |
||
| 250 | 9 | return $this->driver->option($key, $default); |
|
| 251 | } |
||
| 252 | |||
| 253 | 18 | public function definition(string $table, bool $detectRelations = true) : Table |
|
| 259 | /** |
||
| 260 | * Parse all tables from the database. |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | public function parseSchema() |
||
| 268 | /** |
||
| 269 | * Get the full schema as an array that you can serialize and store |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | public function getSchema($asPlainArray = true) |
||
| 301 | /** |
||
| 302 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
| 303 | * @param array $data the schema definition |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | public function setSchema(array $data) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Initialize a table query |
||
| 338 | * @param string $table the table to query |
||
| 339 | * @return TableQuery |
||
| 340 | */ |
||
| 341 | 18 | public function table($table) |
|
| 349 | } |