Complex classes like Db 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 Db, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Db |
||
| 27 | { |
||
| 28 | use Options; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * PDO connection settings |
||
| 32 | * @var array |
||
| 33 | * @link http://php.net/manual/en/pdo.construct.php |
||
| 34 | */ |
||
| 35 | protected $connect = [ |
||
| 36 | "type" => "mysql", |
||
| 37 | "host" => "localhost", |
||
| 38 | "name" => "", |
||
| 39 | "user" => "root", |
||
| 40 | "pass" => "", |
||
| 41 | "options" => [] |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * PDO connection flags |
||
| 46 | * @var array |
||
| 47 | * @link http://php.net/manual/en/pdo.setattribute.php |
||
| 48 | */ |
||
| 49 | protected $attributes = [ |
||
| 50 | \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \PDO PDO instance |
||
| 55 | */ |
||
| 56 | protected $handler; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Setup connection |
||
| 60 | * |
||
| 61 | * Just save connection settings |
||
| 62 | * <code> |
||
| 63 | * $db->setConnect([ |
||
| 64 | * 'type' => 'mysql', |
||
| 65 | * 'host' => 'localhost', |
||
| 66 | * 'name' => 'db name', |
||
| 67 | * 'user' => 'root', |
||
| 68 | * 'pass' => '' |
||
| 69 | * ]); |
||
| 70 | * </code> |
||
| 71 | * |
||
| 72 | * @param array $connect options |
||
| 73 | * @return Db |
||
| 74 | * @throws DbException |
||
| 75 | */ |
||
| 76 | 22 | public function setConnect(array $connect) |
|
| 77 | { |
||
| 78 | 22 | $this->connect = array_merge($this->connect, $connect); |
|
| 79 | 22 | $this->checkConnect(); |
|
| 80 | 22 | return $this; |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Check connection options |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | * @throws ConfigurationException |
||
| 88 | */ |
||
| 89 | 22 | private function checkConnect() |
|
| 90 | { |
||
| 91 | 22 | if (empty($this->connect['type']) or |
|
| 92 | 22 | empty($this->connect['host']) or |
|
| 93 | 22 | empty($this->connect['name']) or |
|
| 94 | 22 | empty($this->connect['user']) |
|
| 95 | ) { |
||
| 96 | 1 | throw new ConfigurationException( |
|
| 97 | 'Database adapter is not configured. |
||
| 98 | 1 | Please check `db` configuration section: required type, host, db name and user' |
|
| 99 | ); |
||
| 100 | } |
||
| 101 | 22 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Setup attributes for PDO connect |
||
| 105 | * |
||
| 106 | * @param array $attributes |
||
| 107 | * @return Db |
||
| 108 | */ |
||
| 109 | public function setAttributes(array $attributes) |
||
| 110 | { |
||
| 111 | $this->attributes = $attributes; |
||
| 112 | return $this; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Connect to Db |
||
| 117 | * |
||
| 118 | * @return Db |
||
| 119 | * @throws DbException |
||
| 120 | */ |
||
| 121 | 16 | public function connect() |
|
| 122 | { |
||
| 123 | 16 | if (empty($this->handler)) { |
|
| 124 | try { |
||
| 125 | 16 | $this->checkConnect(); |
|
| 126 | 16 | $this->log("Connect to " . $this->connect['host']); |
|
| 127 | 16 | $this->handler = new \PDO( |
|
| 128 | 16 | $this->connect['type'] . ":host=" . $this->connect['host'] . ";dbname=" . $this->connect['name'], |
|
| 129 | 16 | $this->connect['user'], |
|
| 130 | 16 | $this->connect['pass'], |
|
| 131 | 16 | $this->connect['options'] |
|
| 132 | ); |
||
| 133 | |||
| 134 | 16 | foreach ($this->attributes as $attribute => $value) { |
|
| 135 | 16 | $this->handler->setAttribute($attribute, $value); |
|
| 136 | } |
||
| 137 | |||
| 138 | 16 | $this->ok(); |
|
| 139 | } catch (\Exception $e) { |
||
| 140 | throw new DbException("Attempt connection to database is failed: {$e->getMessage()}"); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | 16 | return $this; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return PDO handler |
||
| 148 | * |
||
| 149 | * @return \PDO |
||
| 150 | */ |
||
| 151 | 36 | public function handler() |
|
| 152 | { |
||
| 153 | 36 | if (empty($this->handler)) { |
|
| 154 | 15 | $this->connect(); |
|
| 155 | } |
||
| 156 | 36 | return $this->handler; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Prepare SQL query and return PDO Statement |
||
| 161 | * |
||
| 162 | * @param string $sql SQL query with placeholders |
||
| 163 | * @param array $params params for query placeholders |
||
| 164 | * @return \PDOStatement |
||
| 165 | */ |
||
| 166 | 23 | protected function prepare($sql, $params) |
|
| 167 | { |
||
| 168 | 23 | $stmt = $this->handler()->prepare($sql); |
|
| 169 | 23 | $stmt->execute($params); |
|
| 170 | |||
| 171 | 23 | $this->log($sql, $params); |
|
| 172 | |||
| 173 | 23 | return $stmt; |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Quotes a string for use in a query |
||
| 178 | * |
||
| 179 | * Example of usage |
||
| 180 | * <code> |
||
| 181 | * $db->quote($_GET['id']) |
||
| 182 | * </code> |
||
| 183 | * |
||
| 184 | * @param string $value |
||
| 185 | * @param int $type |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 1 | public function quote($value, $type = \PDO::PARAM_STR) |
|
| 189 | { |
||
| 190 | 1 | return $this->handler()->quote($value, $type); |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Quote a string so it can be safely used as a table or column name |
||
| 195 | * |
||
| 196 | * @param string $identifier |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 16 | public function quoteIdentifier($identifier) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Execute SQL query |
||
| 214 | * |
||
| 215 | * Example of usage |
||
| 216 | * <code> |
||
| 217 | * $db->query("SET NAMES 'utf8'"); |
||
| 218 | * </code> |
||
| 219 | * |
||
| 220 | * @param string $sql SQL query with placeholders |
||
| 221 | * "UPDATE users SET name = :name WHERE id = :id" |
||
| 222 | * @param array $params params for query placeholders (optional) |
||
| 223 | * array (':name' => 'John', ':id' => '123') |
||
| 224 | * @param array $types Types of params (optional) |
||
| 225 | * array (':name' => \PDO::PARAM_STR, ':id' => \PDO::PARAM_INT) |
||
| 226 | * @return integer the number of rows |
||
| 227 | */ |
||
| 228 | 20 | public function query($sql, $params = [], $types = []) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Create new query select builder |
||
| 246 | * |
||
| 247 | * @param array|string ...$select The selection expressions |
||
| 248 | * @return Query\Select |
||
| 249 | */ |
||
| 250 | 1 | public function select(...$select) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Create new query insert builder |
||
| 259 | * |
||
| 260 | * @param string $table |
||
| 261 | * @return Query\Insert |
||
| 262 | */ |
||
| 263 | 1 | public function insert($table) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Create new query update builder |
||
| 272 | * |
||
| 273 | * @param string $table |
||
| 274 | * @return Query\Update |
||
| 275 | */ |
||
| 276 | 1 | public function update($table) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Create new query update builder |
||
| 285 | * |
||
| 286 | * @param string $table |
||
| 287 | * @return Query\Delete |
||
| 288 | */ |
||
| 289 | 9 | public function delete($table) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Return first field from first element from the result set |
||
| 298 | * |
||
| 299 | * Example of usage |
||
| 300 | * <code> |
||
| 301 | * $db->fetchOne("SELECT COUNT(*) FROM users"); |
||
| 302 | * </code> |
||
| 303 | * |
||
| 304 | * @param string $sql SQL query with placeholders |
||
| 305 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 306 | * @param array $params params for query placeholders (optional) |
||
| 307 | * array (':name' => 'John', ':pass' => '123456') |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | 4 | public function fetchOne($sql, $params = []) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Returns an array containing first row from the result set |
||
| 321 | * |
||
| 322 | * Example of usage |
||
| 323 | * <code> |
||
| 324 | * $db->fetchRow("SELECT name, email FROM users WHERE id = ". $db->quote($id)); |
||
| 325 | * $db->fetchRow("SELECT name, email FROM users WHERE id = ?", [$id]); |
||
| 326 | * $db->fetchRow("SELECT name, email FROM users WHERE id = :id", [':id'=>$id]); |
||
| 327 | * </code> |
||
| 328 | * |
||
| 329 | * @param string $sql SQL query with placeholders |
||
| 330 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 331 | * @param array $params params for query placeholders (optional) |
||
| 332 | * array (':name' => 'John', ':pass' => '123456') |
||
| 333 | * @return array array ('name' => 'John', 'email' => '[email protected]') |
||
| 334 | */ |
||
| 335 | 1 | public function fetchRow($sql, $params = []) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Returns an array containing all of the result set rows |
||
| 346 | * |
||
| 347 | * Example of usage |
||
| 348 | * <code> |
||
| 349 | * $db->fetchAll("SELECT * FROM users WHERE ip = ?", ['192.168.1.1']); |
||
| 350 | * </code> |
||
| 351 | * |
||
| 352 | * @param string $sql SQL query with placeholders |
||
| 353 | * "SELECT * FROM users WHERE ip = :ip" |
||
| 354 | * @param array $params params for query placeholders (optional) |
||
| 355 | * array (':ip' => '127.0.0.1') |
||
| 356 | * @return array[] |
||
| 357 | */ |
||
| 358 | 3 | public function fetchAll($sql, $params = []) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Returns an array containing one column from the result set rows |
||
| 369 | * |
||
| 370 | * @param string $sql SQL query with placeholders |
||
| 371 | * "SELECT id FROM users WHERE ip = :ip" |
||
| 372 | * @param array $params params for query placeholders (optional) |
||
| 373 | * array (':ip' => '127.0.0.1') |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | 2 | public function fetchColumn($sql, $params = []) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Returns an array containing all of the result set rows |
||
| 387 | * |
||
| 388 | * Group by first column |
||
| 389 | * |
||
| 390 | * <code> |
||
| 391 | * $db->fetchGroup("SELECT ip, COUNT(id) FROM users GROUP BY ip", []); |
||
| 392 | * </code> |
||
| 393 | * |
||
| 394 | * @param string $sql SQL query with placeholders |
||
| 395 | * "SELECT ip, id FROM users" |
||
| 396 | * @param array $params params for query placeholders (optional) |
||
| 397 | * @param mixed $object |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | 1 | public function fetchGroup($sql, $params = [], $object = null) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Returns an array containing all of the result set rows |
||
| 416 | * |
||
| 417 | * Group by first column |
||
| 418 | * |
||
| 419 | * @param string $sql SQL query with placeholders |
||
| 420 | * "SELECT ip, id FROM users" |
||
| 421 | * @param array $params params for query placeholders (optional) |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | 1 | public function fetchColumnGroup($sql, $params = []) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Returns a key-value array |
||
| 435 | * |
||
| 436 | * @param string $sql SQL query with placeholders |
||
| 437 | * "SELECT id, username FROM users WHERE ip = :ip" |
||
| 438 | * @param array $params params for query placeholders (optional) |
||
| 439 | * array (':ip' => '127.0.0.1') |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | 1 | public function fetchPairs($sql, $params = []) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Returns an object containing first row from the result set |
||
| 453 | * |
||
| 454 | * Example of usage |
||
| 455 | * <code> |
||
| 456 | * // Fetch object to stdClass |
||
| 457 | * $stdClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id]); |
||
| 458 | * // Fetch object to new Some object |
||
| 459 | * $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id], 'Some'); |
||
| 460 | * // Fetch object to exists instance of Some object |
||
| 461 | * $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id], $someClass); |
||
| 462 | * </code> |
||
| 463 | * |
||
| 464 | * @param string $sql SQL query with placeholders |
||
| 465 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 466 | * @param array $params params for query placeholders (optional) |
||
| 467 | * array (':name' => 'John', ':pass' => '123456') |
||
| 468 | * @param mixed $object |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | 3 | public function fetchObject($sql, $params = [], $object = 'stdClass') |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Returns an array of objects containing the result set |
||
| 491 | * |
||
| 492 | * @param string $sql SQL query with placeholders |
||
| 493 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 494 | * @param array $params params for query placeholders (optional) |
||
| 495 | * array (':name' => 'John', ':pass' => '123456') |
||
| 496 | * @param mixed $object Class name or instance |
||
| 497 | * @return array |
||
| 498 | */ |
||
| 499 | 10 | public function fetchObjects($sql, $params = [], $object = null) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Returns an array of linked objects containing the result set |
||
| 518 | * |
||
| 519 | * @param string $sql SQL query with placeholders |
||
| 520 | * "SELECT '__users', u.*, '__users_profile', up.* |
||
| 521 | * FROM users u |
||
| 522 | * LEFT JOIN users_profile up ON up.userId = u.id |
||
| 523 | * WHERE u.name = :name" |
||
| 524 | * @param array $params params for query placeholders (optional) |
||
| 525 | * array (':name' => 'John') |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | public function fetchRelations($sql, $params = []) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Transaction wrapper |
||
| 544 | * |
||
| 545 | * Example of usage |
||
| 546 | * <code> |
||
| 547 | * $db->transaction(function() use ($db) { |
||
| 548 | * $db->query("INSERT INTO `table` ..."); |
||
| 549 | * $db->query("UPDATE `table` ..."); |
||
| 550 | * $db->query("DELETE FROM `table` ..."); |
||
| 551 | * }) |
||
| 552 | * </code> |
||
| 553 | * |
||
| 554 | * @param callable $process callable structure - closure function or class with __invoke() method |
||
| 555 | * @return bool |
||
| 556 | * @throws DbException |
||
| 557 | */ |
||
| 558 | 5 | public function transaction(callable $process) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Setup timer |
||
| 573 | * |
||
| 574 | * @return void |
||
| 575 | */ |
||
| 576 | 36 | protected function ok() |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Log queries by Application |
||
| 583 | * |
||
| 584 | * @param string $sql SQL query for logs |
||
| 585 | * @param array $context |
||
| 586 | * @return void |
||
| 587 | */ |
||
| 588 | 36 | protected function log($sql, array $context = []) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Disconnect PDO and clean default adapter |
||
| 601 | * |
||
| 602 | * @return void |
||
| 603 | */ |
||
| 604 | 21 | public function disconnect() |
|
| 610 | } |
||
| 611 |