Complex classes like DbConnection 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 DbConnection, and based on these observations, apply Extract Interface, too.
1 | <?php /** MicroDataBaseConnection */ |
||
19 | class DbConnection extends Connection |
||
20 | { |
||
21 | /** @var \PDO|null $conn Connection to DB */ |
||
22 | protected $conn; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Construct for this class |
||
27 | * |
||
28 | * @access public |
||
29 | * |
||
30 | * @param array $config |
||
31 | * @param array $options |
||
32 | * |
||
33 | * @result void |
||
34 | * @throws Exception |
||
35 | */ |
||
36 | public function __construct(array $config = [], array $options = []) |
||
50 | |||
51 | /** |
||
52 | * Destructor for this class |
||
53 | * |
||
54 | * @access public |
||
55 | * @return void |
||
56 | */ |
||
57 | public function __destruct() |
||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model') |
||
84 | |||
85 | /** |
||
86 | * @inheritdoc |
||
87 | */ |
||
88 | public function listDatabases() |
||
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | */ |
||
103 | public function infoDatabase($dbName) |
||
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | public function tableExists($table) |
||
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | public function listTables() |
||
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | public function createTable($name, array $elements = [], $params = '') |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | public function clearTable($name) |
||
155 | |||
156 | /** |
||
157 | * @inheritdoc |
||
158 | */ |
||
159 | public function removeTable($name) |
||
163 | |||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | public function fieldExists($field, $table) |
||
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | public function listFields($table) |
||
199 | |||
200 | /** |
||
201 | * @inheritdoc |
||
202 | */ |
||
203 | public function fieldInfo($field, $table) |
||
207 | |||
208 | /** |
||
209 | * @inheritdoc |
||
210 | */ |
||
211 | public function switchDatabase($dbName) |
||
219 | |||
220 | /** |
||
221 | * @inheritdoc |
||
222 | */ |
||
223 | public function insert($table, array $line = [], $multi = false) |
||
244 | |||
245 | /** |
||
246 | * @inheritdoc |
||
247 | */ |
||
248 | public function update($table, array $elements = [], $conditions = '') |
||
267 | |||
268 | /** |
||
269 | * @inheritdoc |
||
270 | */ |
||
271 | public function delete($table, $conditions, array $ph = []) |
||
275 | |||
276 | /** |
||
277 | * @inheritdoc |
||
278 | */ |
||
279 | public function exists($table, array $params = []) |
||
291 | |||
292 | /** |
||
293 | * @inheritdoc |
||
294 | */ |
||
295 | public function count($subQuery = '', $table = '') |
||
310 | } |
||
311 |
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.