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 | /** @var string $tableSchema */ |
||
| 24 | protected $tableSchema = 'public'; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Construct for this class |
||
| 29 | * |
||
| 30 | * @access public |
||
| 31 | * |
||
| 32 | * @param array $config |
||
| 33 | * @param array $options |
||
| 34 | * |
||
| 35 | * @result void |
||
| 36 | * @throws Exception |
||
| 37 | */ |
||
| 38 | public function __construct(array $config = [], array $options = []) |
||
| 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() |
||
| 149 | |||
| 150 | public function getDriverType() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @inheritdoc |
||
| 157 | */ |
||
| 158 | public function createTable($name, array $elements = [], $params = '') |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritdoc |
||
| 167 | */ |
||
| 168 | public function clearTable($name) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @inheritdoc |
||
| 175 | */ |
||
| 176 | public function removeTable($name) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @inheritdoc |
||
| 183 | */ |
||
| 184 | public function fieldExists($field, $table) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @inheritdoc |
||
| 197 | */ |
||
| 198 | public function listFields($table) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @inheritdoc |
||
| 219 | */ |
||
| 220 | public function fieldInfo($field, $table) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @inheritdoc |
||
| 227 | */ |
||
| 228 | public function switchDatabase($dbName) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @inheritdoc |
||
| 239 | */ |
||
| 240 | public function insert($table, array $line = [], $multi = false) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @inheritdoc |
||
| 264 | */ |
||
| 265 | public function update($table, array $elements = [], $conditions = '') |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @inheritdoc |
||
| 287 | */ |
||
| 288 | public function delete($table, $conditions, array $ph = []) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @inheritdoc |
||
| 295 | */ |
||
| 296 | public function exists($table, array $params = []) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @inheritdoc |
||
| 312 | */ |
||
| 313 | public function count($subQuery = '', $table = '') |
||
| 328 | } |
||
| 329 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.