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 configuration array |
||
| 31 | * |
||
| 32 | * @result void |
||
| 33 | * @throws Exception |
||
| 34 | */ |
||
| 35 | public function __construct(array $config = []) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Destructor for this class |
||
| 59 | * |
||
| 60 | * @access public |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | public function __destruct() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @inheritdoc |
||
| 70 | */ |
||
| 71 | public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model') |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @inheritdoc |
||
| 93 | */ |
||
| 94 | public function listDatabases() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @inheritdoc |
||
| 108 | */ |
||
| 109 | public function infoDatabase($dbName) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @inheritdoc |
||
| 130 | */ |
||
| 131 | public function tableExists($table) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @inheritdoc |
||
| 138 | */ |
||
| 139 | public function listTables() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @inheritdoc |
||
| 146 | */ |
||
| 147 | public function createTable($name, array $elements = [], $params = '') |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @inheritdoc |
||
| 156 | */ |
||
| 157 | public function clearTable($name) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @inheritdoc |
||
| 164 | */ |
||
| 165 | public function removeTable($name) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @inheritdoc |
||
| 172 | */ |
||
| 173 | public function fieldExists($field, $table) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @inheritdoc |
||
| 186 | */ |
||
| 187 | public function listFields($table) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @inheritdoc |
||
| 208 | */ |
||
| 209 | public function fieldInfo($field, $table) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @inheritdoc |
||
| 218 | */ |
||
| 219 | public function switchDatabase($dbName) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @inheritdoc |
||
| 230 | */ |
||
| 231 | public function insert($table, array $line = [], $multi = false) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @inheritdoc |
||
| 259 | */ |
||
| 260 | public function update($table, array $elements = [], $conditions = '') |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @inheritdoc |
||
| 284 | */ |
||
| 285 | public function delete($table, $conditions, array $ph = []) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @inheritdoc |
||
| 294 | */ |
||
| 295 | public function exists($table, array $params = []) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @inheritdoc |
||
| 312 | */ |
||
| 313 | public function count($subQuery = '', $table = '') |
||
| 328 | } |
||
| 329 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.