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) |
||
| 224 | { |
||
| 225 | $fields = '`'.implode('`, `', array_keys($multi ? $line[0] : $line)).'`'; |
||
| 226 | $values = ':'.implode(', :', array_keys($multi ? $line[0] : $line)); |
||
| 227 | $rows = $multi ? $line : [$line]; |
||
| 228 | $id = null; |
||
| 229 | |||
| 230 | if ($rows) { |
||
|
|
|||
| 231 | $this->conn->beginTransaction(); |
||
| 232 | |||
| 233 | $dbh = null; |
||
| 234 | foreach ($rows AS $row) { |
||
| 235 | $dbh = $this->conn->prepare("INSERT INTO {$table} ({$fields}) VALUES ({$values});")->execute($row); |
||
| 236 | } |
||
| 237 | |||
| 238 | $id = $dbh ? $this->conn->lastInsertId() : false; |
||
| 239 | $this->conn->commit(); |
||
| 240 | } |
||
| 241 | |||
| 242 | return $id ?: false; |
||
| 243 | } |
||
| 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 = []) |
||
| 280 | { |
||
| 281 | $keys = []; |
||
| 282 | foreach ($params AS $key => $val) { |
||
| 283 | $keys[] = '`'.$table.'`.`'.$key.'`="'.$val.'"'; |
||
| 284 | } |
||
| 285 | |||
| 286 | $sth = $this->conn->prepare('SELECT * FROM `'.$table.'` WHERE '.implode(' AND ', $keys).' LIMIT 1;'); |
||
| 287 | /** @noinspection PdoApiUsageInspection */ |
||
| 288 | $sth->execute(); |
||
| 289 | |||
| 290 | return (bool)$sth->rowCount(); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @inheritdoc |
||
| 295 | */ |
||
| 296 | public function count($subQuery = '', $table = '') |
||
| 311 | } |
||
| 312 |
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.