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 | 10 | public function __construct($driver) |
|
34 | /** |
||
35 | * Create a driver instance from a connection string |
||
36 | * @param string $connectionString the connection string |
||
37 | * @return DriverInterface |
||
38 | */ |
||
39 | 10 | public static function getDriver(string $connectionString) |
|
92 | /** |
||
93 | * Prepare a statement. |
||
94 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
95 | * |
||
96 | * @param string $sql the query to prepare - use `?` for arguments |
||
97 | * @return StatementInterface the prepared statement |
||
98 | */ |
||
99 | public function prepare(string $sql) : StatementInterface |
||
103 | /** |
||
104 | * Test the connection |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | 1 | public function test() : bool |
|
137 | /** |
||
138 | * Run a query (prepare & execute). |
||
139 | * @param string $sql SQL query |
||
140 | * @param mixed $par parameters (optional) |
||
141 | * @return ResultInterface the result of the execution |
||
142 | */ |
||
143 | 114 | public function query(string $sql, $par = null) : ResultInterface |
|
151 | /** |
||
152 | * Run a SELECT query and get an array-like result. |
||
153 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
154 | * |
||
155 | * @param string $sql SQL query |
||
156 | * @param array $par parameters |
||
157 | * @param string $key column name to use as the array index |
||
158 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
159 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
160 | * |
||
161 | * @return Collection the result of the execution |
||
162 | */ |
||
163 | 114 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
193 | /** |
||
194 | * Run a SELECT query and get a single row |
||
195 | * @param string $sql SQL query |
||
196 | * @param array $par parameters |
||
197 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
198 | * @return mixed the result of the execution |
||
199 | */ |
||
200 | 45 | public function one(string $sql, $par = null, bool $opti = true) |
|
204 | /** |
||
205 | * Run a SELECT query and get an array |
||
206 | * @param string $sql SQL query |
||
207 | * @param array $par parameters |
||
208 | * @param string $key column name to use as the array index |
||
209 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
210 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
211 | * @return array the result of the execution |
||
212 | */ |
||
213 | 3 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
217 | /** |
||
218 | * Begin a transaction. |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function begin() : DBInterface |
||
228 | /** |
||
229 | * Commit a transaction. |
||
230 | * @return $this |
||
231 | */ |
||
232 | public function commit() : DBInterface |
||
239 | /** |
||
240 | * Rollback a transaction. |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function rollback() : DBInterface |
||
250 | /** |
||
251 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
252 | * @return string the current driver name |
||
253 | */ |
||
254 | 12 | public function driverName() : string |
|
258 | /** |
||
259 | * Get an option from the driver |
||
260 | * |
||
261 | * @param string $key the option name |
||
262 | * @param mixed $default the default value to return if the option key is not defined |
||
263 | * @return mixed the option value |
||
264 | */ |
||
265 | 57 | public function driverOption(string $key, $default = null) |
|
269 | |||
270 | 117 | public function definition(string $table, bool $detectRelations = true) : Table |
|
276 | /** |
||
277 | * Parse all tables from the database. |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function parseSchema() |
||
285 | /** |
||
286 | * Get the full schema as an array that you can serialize and store |
||
287 | * @return array |
||
288 | */ |
||
289 | public function getSchema($asPlainArray = true) |
||
319 | /** |
||
320 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
321 | * @param array $data the schema definition |
||
322 | * @return $this |
||
323 | */ |
||
324 | public function setSchema(array $data) |
||
353 | |||
354 | /** |
||
355 | * Initialize a table query |
||
356 | * @param string $table the table to query |
||
357 | * @return TableQuery |
||
358 | */ |
||
359 | 117 | public function table($table, bool $mapped = false) |
|
369 | } |
||
370 |