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 | * |
||
| 33 | * @var array |
||
| 34 | * @link http://php.net/manual/en/pdo.construct.php |
||
| 35 | */ |
||
| 36 | protected $connect = [ |
||
| 37 | 'type' => 'mysql', |
||
| 38 | 'host' => 'localhost', |
||
| 39 | 'name' => '', |
||
| 40 | 'user' => 'root', |
||
| 41 | 'pass' => '', |
||
| 42 | 'options' => [] |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * PDO connection flags |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | * @link http://php.net/manual/en/pdo.setattribute.php |
||
| 50 | */ |
||
| 51 | protected $attributes = [ |
||
| 52 | \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \PDO PDO instance |
||
| 57 | */ |
||
| 58 | protected $handler; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Setup connection |
||
| 62 | * |
||
| 63 | * Just save connection settings |
||
| 64 | * <code> |
||
| 65 | * $db->setConnect([ |
||
| 66 | * 'type' => 'mysql', |
||
| 67 | * 'host' => 'localhost', |
||
| 68 | * 'name' => 'db name', |
||
| 69 | * 'user' => 'root', |
||
| 70 | * 'pass' => '' |
||
| 71 | * ]); |
||
| 72 | * </code> |
||
| 73 | * |
||
| 74 | * @param array $connect options |
||
| 75 | * |
||
| 76 | * @throws ConfigurationException |
||
| 77 | * @return Db |
||
| 78 | * @throws DbException |
||
| 79 | */ |
||
| 80 | 46 | public function setConnect(array $connect) |
|
| 81 | { |
||
| 82 | 46 | $this->connect = array_merge($this->connect, $connect); |
|
| 83 | 46 | $this->checkConnect(); |
|
| 84 | 46 | return $this; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Check connection options |
||
| 89 | * |
||
| 90 | * @return void |
||
| 91 | * @throws ConfigurationException |
||
| 92 | */ |
||
| 93 | 46 | private function checkConnect() |
|
| 94 | { |
||
| 95 | 46 | if (empty($this->connect['type']) || |
|
| 96 | 46 | empty($this->connect['host']) || |
|
| 97 | 46 | empty($this->connect['name']) || |
|
| 98 | 46 | empty($this->connect['user']) |
|
| 99 | ) { |
||
| 100 | 1 | throw new ConfigurationException( |
|
| 101 | 1 | 'Database adapter is not configured. |
|
| 102 | Please check `db` configuration section: required type, host, db name and user' |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | 46 | } |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Setup attributes for PDO connect |
||
| 109 | * |
||
| 110 | * @param array $attributes |
||
| 111 | * |
||
| 112 | * @return Db |
||
| 113 | */ |
||
| 114 | public function setAttributes(array $attributes) |
||
| 115 | { |
||
| 116 | $this->attributes = $attributes; |
||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Connect to Db |
||
| 122 | * |
||
| 123 | * @return Db |
||
| 124 | * @throws DbException |
||
| 125 | */ |
||
| 126 | 38 | public function connect() |
|
| 127 | { |
||
| 128 | 38 | if (null ===$this->handler) { |
|
| 129 | try { |
||
| 130 | 38 | $this->checkConnect(); |
|
| 131 | 38 | $this->log('Connect to ' . $this->connect['host']); |
|
| 132 | 38 | $this->handler = new \PDO( |
|
| 133 | 38 | $this->connect['type'] . ':host=' . $this->connect['host'] . ';dbname=' . $this->connect['name'], |
|
| 134 | 38 | $this->connect['user'], |
|
| 135 | 38 | $this->connect['pass'], |
|
| 136 | 38 | $this->connect['options'] |
|
| 137 | ); |
||
| 138 | |||
| 139 | 38 | foreach ($this->attributes as $attribute => $value) { |
|
| 140 | 38 | $this->handler->setAttribute($attribute, $value); |
|
| 141 | } |
||
| 142 | |||
| 143 | 38 | $this->ok(); |
|
| 144 | } catch (\Exception $e) { |
||
| 145 | throw new DbException("Attempt connection to database is failed: {$e->getMessage()}"); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | 38 | return $this; |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Return PDO handler |
||
| 153 | * |
||
| 154 | * @return \PDO |
||
| 155 | */ |
||
| 156 | 38 | public function handler() |
|
| 157 | { |
||
| 158 | 38 | if (null ===$this->handler) { |
|
| 159 | 37 | $this->connect(); |
|
| 160 | } |
||
| 161 | 38 | return $this->handler; |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Prepare SQL query and return PDO Statement |
||
| 166 | * |
||
| 167 | * @param string $sql SQL query with placeholders |
||
| 168 | * @param array $params params for query placeholders |
||
| 169 | * |
||
| 170 | * @return \PDOStatement |
||
| 171 | */ |
||
| 172 | 25 | protected function prepare($sql, $params) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Quotes a string for use in a query |
||
| 184 | * |
||
| 185 | * Example of usage |
||
| 186 | * <code> |
||
| 187 | * $db->quote($_GET['id']) |
||
| 188 | * </code> |
||
| 189 | * |
||
| 190 | * @param string $value |
||
| 191 | * @param int $type |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | 1 | public function quote($value, $type = \PDO::PARAM_STR) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Quote a string so it can be safely used as a table or column name |
||
| 202 | * |
||
| 203 | * @param string $identifier |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | 16 | public function quoteIdentifier($identifier) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Execute SQL query |
||
| 222 | * |
||
| 223 | * Example of usage |
||
| 224 | * <code> |
||
| 225 | * $db->query("SET NAMES 'utf8'"); |
||
| 226 | * </code> |
||
| 227 | * |
||
| 228 | * @param string $sql SQL query with placeholders |
||
| 229 | * "UPDATE users SET name = :name WHERE id = :id" |
||
| 230 | * @param array $params params for query placeholders (optional) |
||
| 231 | * array (':name' => 'John', ':id' => '123') |
||
| 232 | * @param array $types Types of params (optional) |
||
| 233 | * array (':name' => \PDO::PARAM_STR, ':id' => \PDO::PARAM_INT) |
||
| 234 | * |
||
| 235 | * @return integer the number of rows |
||
| 236 | */ |
||
| 237 | 22 | public function query($sql, $params = [], $types = []) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Create new query select builder |
||
| 255 | * |
||
| 256 | * @param array|string ...$select The selection expressions |
||
| 257 | * |
||
| 258 | * @return Query\Select |
||
| 259 | */ |
||
| 260 | 1 | public function select(...$select) : Query\Select |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Create new query insert builder |
||
| 269 | * |
||
| 270 | * @param string $table |
||
| 271 | * |
||
| 272 | * @return Query\Insert |
||
| 273 | */ |
||
| 274 | 1 | public function insert($table) : Query\Insert |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Create new query update builder |
||
| 283 | * |
||
| 284 | * @param string $table |
||
| 285 | * |
||
| 286 | * @return Query\Update |
||
| 287 | */ |
||
| 288 | 1 | public function update($table) : Query\Update |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Create new query update builder |
||
| 297 | * |
||
| 298 | * @param string $table |
||
| 299 | * |
||
| 300 | * @return Query\Delete |
||
| 301 | */ |
||
| 302 | 9 | public function delete($table) : Query\Delete |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Return first field from first element from the result set |
||
| 311 | * |
||
| 312 | * Example of usage |
||
| 313 | * <code> |
||
| 314 | * $db->fetchOne("SELECT COUNT(*) FROM users"); |
||
| 315 | * </code> |
||
| 316 | * |
||
| 317 | * @param string $sql SQL query with placeholders |
||
| 318 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 319 | * @param array $params params for query placeholders (optional) |
||
| 320 | * array (':name' => 'John', ':pass' => '123456') |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | 5 | public function fetchOne($sql, $params = []) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Returns an array containing first row from the result set |
||
| 335 | * |
||
| 336 | * Example of usage |
||
| 337 | * <code> |
||
| 338 | * $db->fetchRow("SELECT name, email FROM users WHERE id = ". $db->quote($id)); |
||
| 339 | * $db->fetchRow("SELECT name, email FROM users WHERE id = ?", [$id]); |
||
| 340 | * $db->fetchRow("SELECT name, email FROM users WHERE id = :id", [':id'=>$id]); |
||
| 341 | * </code> |
||
| 342 | * |
||
| 343 | * @param string $sql SQL query with placeholders |
||
| 344 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 345 | * @param array $params params for query placeholders (optional) |
||
| 346 | * array (':name' => 'John', ':pass' => '123456') |
||
| 347 | * |
||
| 348 | * @return array array ('name' => 'John', 'email' => '[email protected]') |
||
| 349 | */ |
||
| 350 | 1 | public function fetchRow($sql, $params = []) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Returns an array containing all of the result set rows |
||
| 361 | * |
||
| 362 | * Example of usage |
||
| 363 | * <code> |
||
| 364 | * $db->fetchAll("SELECT * FROM users WHERE ip = ?", ['192.168.1.1']); |
||
| 365 | * </code> |
||
| 366 | * |
||
| 367 | * @param string $sql SQL query with placeholders |
||
| 368 | * "SELECT * FROM users WHERE ip = :ip" |
||
| 369 | * @param array $params params for query placeholders (optional) |
||
| 370 | * array (':ip' => '127.0.0.1') |
||
| 371 | * |
||
| 372 | * @return array[] |
||
| 373 | */ |
||
| 374 | 3 | public function fetchAll($sql, $params = []) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Returns an array containing one column from the result set rows |
||
| 385 | * |
||
| 386 | * @param string $sql SQL query with placeholders |
||
| 387 | * "SELECT id FROM users WHERE ip = :ip" |
||
| 388 | * @param array $params params for query placeholders (optional) |
||
| 389 | * array (':ip' => '127.0.0.1') |
||
| 390 | * |
||
| 391 | * @return array |
||
| 392 | */ |
||
| 393 | 2 | public function fetchColumn($sql, $params = []) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Returns an array containing all of the result set rows |
||
| 404 | * |
||
| 405 | * Group by first column |
||
| 406 | * |
||
| 407 | * <code> |
||
| 408 | * $db->fetchGroup("SELECT ip, COUNT(id) FROM users GROUP BY ip", []); |
||
| 409 | * </code> |
||
| 410 | * |
||
| 411 | * @param string $sql SQL query with placeholders |
||
| 412 | * "SELECT ip, id FROM users" |
||
| 413 | * @param array $params params for query placeholders (optional) |
||
| 414 | * @param mixed $object |
||
| 415 | * |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | 1 | public function fetchGroup($sql, $params = [], $object = null) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Returns an array containing all of the result set rows |
||
| 434 | * |
||
| 435 | * Group by first column |
||
| 436 | * |
||
| 437 | * @param string $sql SQL query with placeholders |
||
| 438 | * "SELECT ip, id FROM users" |
||
| 439 | * @param array $params params for query placeholders (optional) |
||
| 440 | * |
||
| 441 | * @return array |
||
| 442 | */ |
||
| 443 | 1 | public function fetchColumnGroup($sql, $params = []) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Returns a key-value array |
||
| 454 | * |
||
| 455 | * @param string $sql SQL query with placeholders |
||
| 456 | * "SELECT id, username FROM users WHERE ip = :ip" |
||
| 457 | * @param array $params params for query placeholders (optional) |
||
| 458 | * array (':ip' => '127.0.0.1') |
||
| 459 | * |
||
| 460 | * @return array |
||
| 461 | */ |
||
| 462 | 1 | public function fetchPairs($sql, $params = []) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Returns an object containing first row from the result set |
||
| 473 | * |
||
| 474 | * Example of usage |
||
| 475 | * <code> |
||
| 476 | * // Fetch object to stdClass |
||
| 477 | * $stdClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id]); |
||
| 478 | * // Fetch object to new Some object |
||
| 479 | * $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id], 'Some'); |
||
| 480 | * // Fetch object to exists instance of Some object |
||
| 481 | * $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', [$id], $someClass); |
||
| 482 | * </code> |
||
| 483 | * |
||
| 484 | * @param string $sql SQL query with placeholders |
||
| 485 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 486 | * @param array $params params for query placeholders (optional) |
||
| 487 | * array (':name' => 'John', ':pass' => '123456') |
||
| 488 | * @param mixed $object |
||
| 489 | * |
||
| 490 | * @return array |
||
| 491 | */ |
||
| 492 | 3 | public function fetchObject($sql, $params = [], $object = 'stdClass') |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Returns an array of objects containing the result set |
||
| 512 | * |
||
| 513 | * @param string $sql SQL query with placeholders |
||
| 514 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 515 | * @param array $params params for query placeholders (optional) |
||
| 516 | * array (':name' => 'John', ':pass' => '123456') |
||
| 517 | * @param mixed $object Class name or instance |
||
| 518 | * |
||
| 519 | * @return array |
||
| 520 | */ |
||
| 521 | 12 | public function fetchObjects($sql, $params = [], $object = null) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Returns an array of linked objects containing the result set |
||
| 540 | * |
||
| 541 | * @param string $sql SQL query with placeholders |
||
| 542 | * "SELECT '__users', u.*, '__users_profile', up.* |
||
| 543 | * FROM users u |
||
| 544 | * LEFT JOIN users_profile up ON up.userId = u.id |
||
| 545 | * WHERE u.name = :name" |
||
| 546 | * @param array $params params for query placeholders (optional) |
||
| 547 | * array (':name' => 'John') |
||
| 548 | * |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | public function fetchRelations($sql, $params = []) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Transaction wrapper |
||
| 567 | * |
||
| 568 | * Example of usage |
||
| 569 | * <code> |
||
| 570 | * $db->transaction(function() use ($db) { |
||
| 571 | * $db->query("INSERT INTO `table` ..."); |
||
| 572 | * $db->query("UPDATE `table` ..."); |
||
| 573 | * $db->query("DELETE FROM `table` ..."); |
||
| 574 | * }) |
||
| 575 | * </code> |
||
| 576 | * |
||
| 577 | * @param callable $process callable structure - closure function or class with __invoke() method |
||
| 578 | * |
||
| 579 | * @return mixed|bool |
||
| 580 | * @throws DbException |
||
| 581 | */ |
||
| 582 | 6 | public function transaction(callable $process) |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Setup timer |
||
| 598 | * |
||
| 599 | * @return void |
||
| 600 | */ |
||
| 601 | 38 | protected function ok() |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Log queries by Application |
||
| 608 | * |
||
| 609 | * @param string $sql SQL query for logs |
||
| 610 | * @param array $context |
||
| 611 | * |
||
| 612 | * @return void |
||
| 613 | */ |
||
| 614 | 38 | protected function log($sql, array $context = []) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Disconnect PDO and clean default adapter |
||
| 627 | * |
||
| 628 | * @return void |
||
| 629 | */ |
||
| 630 | 21 | public function disconnect() |
|
| 636 | } |
||
| 637 |