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 | 3 | 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 | 3 | 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 |
|
118 | /** |
||
119 | * Run a query (prepare & execute). |
||
120 | * @param string $sql SQL query |
||
121 | * @param array|null $par parameters (optional) |
||
122 | * @return ResultInterface the result of the execution |
||
123 | */ |
||
124 | 27 | public function query(string $sql, $par = null) : ResultInterface |
|
132 | /** |
||
133 | * Run a SELECT query and get an array-like result. |
||
134 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
135 | * |
||
136 | * @param string $sql SQL query |
||
137 | * @param array $par parameters |
||
138 | * @param string $key column name to use as the array index |
||
139 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
140 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
141 | * |
||
142 | * @return Collection the result of the execution |
||
143 | */ |
||
144 | 21 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
167 | /** |
||
168 | * Run a SELECT query and get a single row |
||
169 | * @param string $sql SQL query |
||
170 | * @param array $par parameters |
||
171 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
172 | * @return Collection the result of the execution |
||
173 | */ |
||
174 | 7 | public function one(string $sql, $par = null, bool $opti = true) |
|
178 | /** |
||
179 | * Run a SELECT query and get an array |
||
180 | * @param string $sql SQL query |
||
181 | * @param array $par parameters |
||
182 | * @param string $key column name to use as the array index |
||
183 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
184 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
185 | * @return Collection the result of the execution |
||
186 | */ |
||
187 | 2 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
191 | /** |
||
192 | * Begin a transaction. |
||
193 | * @return $this |
||
194 | */ |
||
195 | 1 | public function begin() : DBInterface |
|
202 | /** |
||
203 | * Commit a transaction. |
||
204 | * @return $this |
||
205 | */ |
||
206 | 1 | public function commit() : DBInterface |
|
213 | /** |
||
214 | * Rollback a transaction. |
||
215 | * @return $this |
||
216 | */ |
||
217 | 1 | public function rollback() : DBInterface |
|
224 | /** |
||
225 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
226 | * @return string the current driver name |
||
227 | */ |
||
228 | 2 | public function driverName() : string |
|
232 | /** |
||
233 | * Get an option from the driver |
||
234 | * |
||
235 | * @param string $key the option name |
||
236 | * @param mixed $default the default value to return if the option key is not defined |
||
237 | * @return mixed the option value |
||
238 | */ |
||
239 | public function driverOption(string $key, $default = null) |
||
243 | |||
244 | 13 | public function definition(string $table, bool $detectRelations = true) : Table |
|
250 | /** |
||
251 | * Parse all tables from the database. |
||
252 | * @return $this |
||
253 | */ |
||
254 | public function parseSchema() |
||
259 | /** |
||
260 | * Get the full schema as an array that you can serialize and store |
||
261 | * @return array |
||
262 | */ |
||
263 | 1 | public function getSchema($asPlainArray = true) |
|
291 | /** |
||
292 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
293 | * @param array $data the schema definition |
||
294 | * @return $this |
||
295 | */ |
||
296 | public function setSchema(array $data) |
||
325 | |||
326 | /** |
||
327 | * Initialize a table query |
||
328 | * @param string $table the table to query |
||
329 | * @return TableQuery |
||
330 | */ |
||
331 | 13 | public function table($table) |
|
339 | } |