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 |
||
| 27 | class Db |
||
| 28 | { |
||
| 29 | use Options; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * PDO connection settings |
||
| 33 | * @var array |
||
| 34 | * @link http://php.net/manual/en/pdo.construct.php |
||
| 35 | */ |
||
| 36 | protected $connect = array( |
||
| 37 | "type" => "mysql", |
||
| 38 | "host" => "localhost", |
||
| 39 | "name" => "", |
||
| 40 | "user" => "root", |
||
| 41 | "pass" => "", |
||
| 42 | "options" => array() |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * PDO connection flags |
||
| 47 | * @var array |
||
| 48 | * @link http://php.net/manual/en/pdo.setattribute.php |
||
| 49 | */ |
||
| 50 | protected $attributes = array( |
||
| 51 | \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION |
||
| 52 | ); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \PDO PDO instance |
||
| 56 | */ |
||
| 57 | protected $handler; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var float part time |
||
| 61 | */ |
||
| 62 | protected $timer; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Setup connection |
||
| 66 | * |
||
| 67 | * Just save connection settings |
||
| 68 | * <code> |
||
| 69 | * $db->setConnect(array( |
||
| 70 | * 'type' => 'mysql', |
||
| 71 | * 'host' => 'localhost', |
||
| 72 | * 'name' => 'db name', |
||
| 73 | * 'user' => 'root', |
||
| 74 | * 'pass' => '' |
||
| 75 | * )); |
||
| 76 | * </code> |
||
| 77 | * |
||
| 78 | * @param array $connect options |
||
| 79 | * @return Db |
||
| 80 | * @throws DbException |
||
| 81 | */ |
||
| 82 | 22 | public function setConnect(array $connect) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Check connection options |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | * @throws ConfigurationException |
||
| 94 | */ |
||
| 95 | 22 | private function checkConnect() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Setup attributes for PDO connect |
||
| 111 | * |
||
| 112 | * @param array $attributes |
||
| 113 | * @return Db |
||
| 114 | */ |
||
| 115 | public function setAttributes(array $attributes) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Connect to Db |
||
| 123 | * |
||
| 124 | * @return Db |
||
| 125 | * @throws DbException |
||
| 126 | */ |
||
| 127 | 16 | public function connect() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Return PDO handler |
||
| 154 | * |
||
| 155 | * @return \PDO |
||
| 156 | */ |
||
| 157 | 26 | public function handler() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Prepare SQL query and return PDO Statement |
||
| 167 | * |
||
| 168 | * @param string $sql SQL query with placeholders |
||
| 169 | * @param array $params params for query placeholders |
||
| 170 | * @return \PDOStatement |
||
| 171 | */ |
||
| 172 | 15 | 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 | * @return string |
||
| 192 | */ |
||
| 193 | 1 | public function quote($value) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Quote a string so it can be safely used as a table or column name |
||
| 200 | * |
||
| 201 | * @param string $identifier |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | 13 | public function quoteIdentifier($identifier) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Execute SQL query |
||
| 219 | * |
||
| 220 | * Example of usage |
||
| 221 | * <code> |
||
| 222 | * $db->query("SET NAMES 'utf8'"); |
||
| 223 | * </code> |
||
| 224 | * |
||
| 225 | * @param string $sql SQL query with placeholders |
||
| 226 | * "UPDATE users SET name = :name WHERE id = :id" |
||
| 227 | * @param array $params params for query placeholders (optional) |
||
| 228 | * array (':name' => 'John', ':id' => '123') |
||
| 229 | * @param array $types Types of params (optional) |
||
| 230 | * array (':name' => \PDO::PARAM_STR, ':id' => \PDO::PARAM_INT) |
||
| 231 | * @return integer the number of rows |
||
| 232 | */ |
||
| 233 | 10 | public function query($sql, $params = array(), $types = array()) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Create new query select builder |
||
| 251 | * |
||
| 252 | * @param array|string ...$select The selection expressions |
||
| 253 | * @return Query\Select |
||
| 254 | */ |
||
| 255 | 1 | public function select(...$select) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Create new query insert builder |
||
| 264 | * |
||
| 265 | * @param string $table |
||
| 266 | * @return Query\Insert |
||
| 267 | */ |
||
| 268 | 1 | public function insert($table) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Create new query update builder |
||
| 277 | * |
||
| 278 | * @param string $table |
||
| 279 | * @return Query\Update |
||
| 280 | */ |
||
| 281 | 1 | public function update($table) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Create new query update builder |
||
| 290 | * |
||
| 291 | * @param string $table |
||
| 292 | * @return Query\Delete |
||
| 293 | */ |
||
| 294 | 9 | public function delete($table) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Return first field from first element from the result set |
||
| 303 | * |
||
| 304 | * Example of usage |
||
| 305 | * <code> |
||
| 306 | * $db->fetchOne("SELECT COUNT(*) FROM users"); |
||
| 307 | * </code> |
||
| 308 | * |
||
| 309 | * @param string $sql SQL query with placeholders |
||
| 310 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 311 | * @param array $params params for query placeholders (optional) |
||
| 312 | * array (':name' => 'John', ':pass' => '123456') |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 3 | public function fetchOne($sql, $params = array()) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Returns an array containing first row from the result set |
||
| 326 | * |
||
| 327 | * Example of usage |
||
| 328 | * <code> |
||
| 329 | * $db->fetchRow("SELECT name, email FROM users WHERE id = ". $db->quote($id)); |
||
| 330 | * $db->fetchRow("SELECT name, email FROM users WHERE id = ?", array($id)); |
||
| 331 | * $db->fetchRow("SELECT name, email FROM users WHERE id = :id", array(':id'=>$id)); |
||
| 332 | * </code> |
||
| 333 | * |
||
| 334 | * @param string $sql SQL query with placeholders |
||
| 335 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 336 | * @param array $params params for query placeholders (optional) |
||
| 337 | * array (':name' => 'John', ':pass' => '123456') |
||
| 338 | * @return array array ('name' => 'John', 'email' => '[email protected]') |
||
| 339 | */ |
||
| 340 | 1 | public function fetchRow($sql, $params = array()) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Returns an array containing all of the result set rows |
||
| 351 | * |
||
| 352 | * Example of usage |
||
| 353 | * <code> |
||
| 354 | * $db->fetchAll("SELECT * FROM users WHERE ip = ?", array('192.168.1.1')); |
||
| 355 | * </code> |
||
| 356 | * |
||
| 357 | * @param string $sql SQL query with placeholders |
||
| 358 | * "SELECT * FROM users WHERE ip = :ip" |
||
| 359 | * @param array $params params for query placeholders (optional) |
||
| 360 | * array (':ip' => '127.0.0.1') |
||
| 361 | * @return array[] |
||
| 362 | */ |
||
| 363 | 3 | public function fetchAll($sql, $params = array()) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Returns an array containing one column from the result set rows |
||
| 374 | * |
||
| 375 | * @param string $sql SQL query with placeholders |
||
| 376 | * "SELECT id FROM users WHERE ip = :ip" |
||
| 377 | * @param array $params params for query placeholders (optional) |
||
| 378 | * array (':ip' => '127.0.0.1') |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | 1 | public function fetchColumn($sql, $params = array()) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Returns an array containing all of the result set rows |
||
| 392 | * |
||
| 393 | * Group by first column |
||
| 394 | * |
||
| 395 | * <code> |
||
| 396 | * $db->fetchGroup("SELECT ip, COUNT(id) FROM users GROUP BY ip", array()); |
||
| 397 | * </code> |
||
| 398 | * |
||
| 399 | * @param string $sql SQL query with placeholders |
||
| 400 | * "SELECT ip, id FROM users" |
||
| 401 | * @param array $params params for query placeholders (optional) |
||
| 402 | * @param mixed $object |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | 1 | public function fetchGroup($sql, $params = array(), $object = null) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Returns an array containing all of the result set rows |
||
| 421 | * |
||
| 422 | * Group by first column |
||
| 423 | * |
||
| 424 | * @param string $sql SQL query with placeholders |
||
| 425 | * "SELECT ip, id FROM users" |
||
| 426 | * @param array $params params for query placeholders (optional) |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | 1 | public function fetchColumnGroup($sql, $params = array()) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Returns a key-value array |
||
| 440 | * |
||
| 441 | * @param string $sql SQL query with placeholders |
||
| 442 | * "SELECT id, username FROM users WHERE ip = :ip" |
||
| 443 | * @param array $params params for query placeholders (optional) |
||
| 444 | * array (':ip' => '127.0.0.1') |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | 1 | public function fetchPairs($sql, $params = array()) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Returns an object containing first row from the result set |
||
| 458 | * |
||
| 459 | * Example of usage |
||
| 460 | * <code> |
||
| 461 | * // Fetch object to stdClass |
||
| 462 | * $stdClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', array($id)); |
||
| 463 | * // Fetch object to new Some object |
||
| 464 | * $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', array($id), 'Some'); |
||
| 465 | * // Fetch object to exists instance of Some object |
||
| 466 | * $someClass = $db->fetchObject('SELECT * FROM some_table WHERE id = ?', array($id), $someClass); |
||
| 467 | * </code> |
||
| 468 | * |
||
| 469 | * @param string $sql SQL query with placeholders |
||
| 470 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 471 | * @param array $params params for query placeholders (optional) |
||
| 472 | * array (':name' => 'John', ':pass' => '123456') |
||
| 473 | * @param mixed $object |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | 3 | public function fetchObject($sql, $params = array(), $object = 'stdClass') |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Returns an array of objects containing the result set |
||
| 496 | * |
||
| 497 | * @param string $sql SQL query with placeholders |
||
| 498 | * "SELECT * FROM users WHERE name = :name AND pass = :pass" |
||
| 499 | * @param array $params params for query placeholders (optional) |
||
| 500 | * array (':name' => 'John', ':pass' => '123456') |
||
| 501 | * @param mixed $object Class name or instance |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | 3 | public function fetchObjects($sql, $params = array(), $object = null) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Returns an array of linked objects containing the result set |
||
| 523 | * |
||
| 524 | * @param string $sql SQL query with placeholders |
||
| 525 | * "SELECT '__users', u.*, '__users_profile', up.* |
||
| 526 | * FROM users u |
||
| 527 | * LEFT JOIN users_profile up ON up.userId = u.id |
||
| 528 | * WHERE u.name = :name" |
||
| 529 | * @param array $params params for query placeholders (optional) |
||
| 530 | * array (':name' => 'John') |
||
| 531 | * @return array |
||
| 532 | */ |
||
| 533 | public function fetchRelations($sql, $params = array()) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Transaction wrapper |
||
| 549 | * |
||
| 550 | * Example of usage |
||
| 551 | * <code> |
||
| 552 | * $db->transaction(function() use ($db) { |
||
| 553 | * $db->query("INSERT INTO `table` ..."); |
||
| 554 | * $db->query("UPDATE `table` ..."); |
||
| 555 | * $db->query("DELETE FROM `table` ..."); |
||
| 556 | * }) |
||
| 557 | * </code> |
||
| 558 | * |
||
| 559 | * @param callable $process callable structure - closure function or class with __invoke() method |
||
| 560 | * @return bool |
||
| 561 | * @throws DbException |
||
| 562 | */ |
||
| 563 | 5 | public function transaction($process) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Setup timer |
||
| 581 | * |
||
| 582 | * @return void |
||
| 583 | */ |
||
| 584 | 26 | protected function ok() |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Log queries by Application |
||
| 592 | * |
||
| 593 | * @param string $sql SQL query for logs |
||
| 594 | * @param array $context |
||
| 595 | * @return void |
||
| 596 | */ |
||
| 597 | 26 | protected function log($sql, array $context = []) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Disconnect PDO and clean default adapter |
||
| 612 | * |
||
| 613 | * @return void |
||
| 614 | */ |
||
| 615 | 21 | public function disconnect() |
|
| 621 | } |
||
| 622 |