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 | 15 | 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 | 15 | public static function getDriver(string $connectionString) |
|
88 | /** |
||
89 | * Prepare a statement. |
||
90 | * Use only if you need a single query to be performed multiple times with different parameters. |
||
91 | * |
||
92 | * @param string $sql the query to prepare - use `?` for arguments |
||
93 | * @return StatementInterface the prepared statement |
||
94 | */ |
||
95 | 1 | public function prepare(string $sql) : StatementInterface |
|
99 | /** |
||
100 | * Test the connection |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | 1 | public function test() : bool |
|
133 | /** |
||
134 | * Run a query (prepare & execute). |
||
135 | * @param string $sql SQL query |
||
136 | * @param array|null $par parameters (optional) |
||
137 | * @return ResultInterface the result of the execution |
||
138 | */ |
||
139 | 166 | public function query(string $sql, $par = null) : ResultInterface |
|
147 | /** |
||
148 | * Run a SELECT query and get an array-like result. |
||
149 | * When using `get` the data is kept in the database client and fetched as needed (not in PHP memory as with `all`) |
||
150 | * |
||
151 | * @param string $sql SQL query |
||
152 | * @param array $par parameters |
||
153 | * @param string $key column name to use as the array index |
||
154 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
155 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
156 | * |
||
157 | * @return Collection the result of the execution |
||
158 | */ |
||
159 | 160 | public function get(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true): Collection |
|
182 | /** |
||
183 | * Run a SELECT query and get a single row |
||
184 | * @param string $sql SQL query |
||
185 | * @param array $par parameters |
||
186 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
187 | * @return mixed the result of the execution |
||
188 | */ |
||
189 | 63 | public function one(string $sql, $par = null, bool $opti = true) |
|
193 | /** |
||
194 | * Run a SELECT query and get an array |
||
195 | * @param string $sql SQL query |
||
196 | * @param array $par parameters |
||
197 | * @param string $key column name to use as the array index |
||
198 | * @param bool $skip do not include the column used as index in the value (defaults to `false`) |
||
199 | * @param bool $opti if a single column is returned - do not use an array wrapper (defaults to `true`) |
||
200 | * @return Collection the result of the execution |
||
201 | */ |
||
202 | 6 | public function all(string $sql, $par = null, string $key = null, bool $skip = false, bool $opti = true) : array |
|
206 | /** |
||
207 | * Begin a transaction. |
||
208 | * @return $this |
||
209 | */ |
||
210 | 1 | public function begin() : DBInterface |
|
217 | /** |
||
218 | * Commit a transaction. |
||
219 | * @return $this |
||
220 | */ |
||
221 | 1 | public function commit() : DBInterface |
|
228 | /** |
||
229 | * Rollback a transaction. |
||
230 | * @return $this |
||
231 | */ |
||
232 | 1 | public function rollback() : DBInterface |
|
239 | /** |
||
240 | * Get the current driver name (`"mysql"`, `"postgre"`, etc). |
||
241 | * @return string the current driver name |
||
242 | */ |
||
243 | 16 | public function driverName() : string |
|
247 | /** |
||
248 | * Get an option from the driver |
||
249 | * |
||
250 | * @param string $key the option name |
||
251 | * @param mixed $default the default value to return if the option key is not defined |
||
252 | * @return mixed the option value |
||
253 | */ |
||
254 | 76 | public function driverOption(string $key, $default = null) |
|
258 | |||
259 | 156 | public function definition(string $table, bool $detectRelations = true) : Table |
|
265 | /** |
||
266 | * Parse all tables from the database. |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function parseSchema() |
||
274 | /** |
||
275 | * Get the full schema as an array that you can serialize and store |
||
276 | * @return array |
||
277 | */ |
||
278 | public function getSchema($asPlainArray = true) |
||
307 | /** |
||
308 | * Load the schema data from a schema definition array (obtained from getSchema) |
||
309 | * @param array $data the schema definition |
||
310 | * @return $this |
||
311 | */ |
||
312 | public function setSchema(array $data) |
||
341 | |||
342 | /** |
||
343 | * Initialize a table query |
||
344 | * @param string $table the table to query |
||
345 | * @return TableQuery |
||
346 | */ |
||
347 | 156 | public function table($table, bool $mapped = false) |
|
357 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.