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 */ |
||
| 20 | class DbConnection extends Connection |
||
| 21 | { |
||
| 22 | /** @var \PDO|null $conn Connection to DB */ |
||
| 23 | protected $conn; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * Construct for this class |
||
| 28 | * |
||
| 29 | * @access public |
||
| 30 | * |
||
| 31 | * @param IContainer $container |
||
| 32 | * @param array $config |
||
| 33 | * @param array $options |
||
| 34 | * |
||
| 35 | * @result void |
||
| 36 | * @throws Exception |
||
| 37 | */ |
||
| 38 | public function __construct(IContainer $container, array $config = [], array $options = []) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Destructor for this class |
||
| 58 | * |
||
| 59 | * @access public |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | public function __destruct() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdoc |
||
| 69 | */ |
||
| 70 | public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model') |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @inheritdoc |
||
| 92 | */ |
||
| 93 | public function listDatabases() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @inheritdoc |
||
| 107 | */ |
||
| 108 | public function infoDatabase($dbName) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @inheritdoc |
||
| 129 | */ |
||
| 130 | public function tableExists($table) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritdoc |
||
| 137 | */ |
||
| 138 | public function listTables() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @inheritdoc |
||
| 145 | */ |
||
| 146 | public function createTable($name, array $elements = [], $params = '') |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @inheritdoc |
||
| 155 | */ |
||
| 156 | public function clearTable($name) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @inheritdoc |
||
| 163 | */ |
||
| 164 | public function removeTable($name) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @inheritdoc |
||
| 171 | */ |
||
| 172 | public function fieldExists($field, $table) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @inheritdoc |
||
| 185 | */ |
||
| 186 | public function listFields($table) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @inheritdoc |
||
| 207 | */ |
||
| 208 | public function fieldInfo($field, $table) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @inheritdoc |
||
| 217 | */ |
||
| 218 | public function switchDatabase($dbName) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @inheritdoc |
||
| 229 | */ |
||
| 230 | public function insert($table, array $line = [], $multi = false) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @inheritdoc |
||
| 258 | */ |
||
| 259 | public function update($table, array $elements = [], $conditions = '') |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @inheritdoc |
||
| 283 | */ |
||
| 284 | public function delete($table, $conditions, array $ph = []) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @inheritdoc |
||
| 293 | */ |
||
| 294 | public function exists($table, array $params = []) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @inheritdoc |
||
| 311 | */ |
||
| 312 | public function count($subQuery = '', $table = '') |
||
| 327 | } |
||
| 328 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.