Complex classes like Database 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 Database, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
247 | Class Database { |
||
248 | |||
249 | /** |
||
250 | * Constant to indicate whether the query is a write operation. |
||
251 | * |
||
252 | * @var integer |
||
253 | */ |
||
254 | const __WRITE_OPERATION__ = 0; |
||
255 | |||
256 | /** |
||
257 | * Constant to indicate whether the query is a write operation |
||
258 | * |
||
259 | * @var integer |
||
260 | */ |
||
261 | const __READ_OPERATION__ = 1; |
||
262 | |||
263 | /** |
||
264 | * An instance of the current PDO object |
||
265 | * @var PDO |
||
266 | */ |
||
267 | public $conn = null; |
||
268 | |||
269 | /** |
||
270 | * Sets the current `$_log` to be an empty array |
||
271 | * |
||
272 | * @var array |
||
273 | */ |
||
274 | public $log = array(); |
||
275 | |||
276 | /** |
||
277 | * The number of queries this class has executed, defaults to 0. |
||
278 | * |
||
279 | * @var integer |
||
280 | */ |
||
281 | private $_query_count = 0; |
||
282 | |||
283 | /** |
||
284 | * The table prefix for this connection. Queries to be written using |
||
285 | * a `tbl_table_name` syntax, where `tbl_` will be replaced by this |
||
286 | * variable. By default it is `sym_` but it configured in the configuration |
||
287 | * |
||
288 | * @var string |
||
289 | */ |
||
290 | private $_prefix = 'sym_'; |
||
291 | |||
292 | /** |
||
293 | * Whether query caching is enabled or not. By default this set |
||
294 | * to true which will use SQL_CACHE to cache the results of queries |
||
295 | * |
||
296 | * @var boolean |
||
297 | */ |
||
298 | private $_cache = true; |
||
299 | |||
300 | /** |
||
301 | * Whether to log this query in the internal `$log`. |
||
302 | * Defaults to true |
||
303 | * |
||
304 | * @var boolean |
||
305 | */ |
||
306 | private $_logging = true; |
||
307 | |||
308 | /** |
||
309 | * Resets the result, `$this->_lastResult` and `$this->_lastQuery` to their empty |
||
310 | * values. Called on each query and when the class is destroyed. |
||
311 | */ |
||
312 | public function flush() |
||
319 | |||
320 | /** |
||
321 | * Creates a new Database object given an associative array of configuration |
||
322 | * parameters in `$config`. If `$config` contains a key, `pdo` then this |
||
323 | * `Database` instance will use that PDO connection. Otherwise, `$config` |
||
324 | * should include `driver`, `host`, `port`, `user`, `password` and an optional |
||
325 | * array of PDO options in `options`. |
||
326 | * |
||
327 | * @param array $config |
||
328 | * @return PDO |
||
329 | */ |
||
330 | public function __construct(array $config = array()) |
||
347 | |||
348 | /** |
||
349 | * Magic function that will flush the MySQL log and close the MySQL |
||
350 | * connection when the MySQL class is removed or destroyed. |
||
351 | * |
||
352 | * @link http://php.net/manual/en/language.oop5.decon.php |
||
353 | */ |
||
354 | public function __destruct() |
||
359 | |||
360 | /** |
||
361 | * Creates a PDO connection to the desired database given the parameters. |
||
362 | * This will also set the error mode to be exceptions (handled by this class) |
||
363 | * |
||
364 | * @link http://www.php.net/manual/en/pdo.drivers.php |
||
365 | * @param string $dsn |
||
366 | * @param string $username |
||
367 | * @param string $password |
||
368 | * @param array $options |
||
369 | * @return boolean |
||
370 | */ |
||
371 | public function connect($dsn = null, $username = null, $password = null, array $options = array()) |
||
385 | |||
386 | /** |
||
387 | * Returns the number of queries that has been executed |
||
388 | * |
||
389 | * @return integer |
||
390 | */ |
||
391 | public function queryCount() |
||
395 | |||
396 | /** |
||
397 | * Sets query caching to true, this will prepend all READ_OPERATION |
||
398 | * queries with SQL_CACHE. Symphony be default enables caching. It |
||
399 | * can be turned off by setting the query_cache parameter to 'off' in the |
||
400 | * Symphony config file. |
||
401 | * |
||
402 | * @link http://dev.mysql.com/doc/refman/5.1/en/query-cache.html |
||
403 | */ |
||
404 | public function enableCaching() |
||
408 | |||
409 | /** |
||
410 | * Sets query caching to false, this will prepend all READ_OPERATION |
||
411 | * queries will SQL_NO_CACHE. |
||
412 | */ |
||
413 | public function disableCaching() |
||
417 | |||
418 | /** |
||
419 | * Returns boolean if query caching is enabled or not |
||
420 | * |
||
421 | * @return boolean |
||
422 | */ |
||
423 | public function isCachingEnabled() |
||
427 | |||
428 | /** |
||
429 | * Enables the logging of queries |
||
430 | */ |
||
431 | public function enableLogging() |
||
435 | |||
436 | /** |
||
437 | * Sets logging to false |
||
438 | */ |
||
439 | public function disableLogging() |
||
443 | |||
444 | /** |
||
445 | * Returns boolean if logging is enabled or not |
||
446 | * |
||
447 | * @return boolean |
||
448 | */ |
||
449 | public function isLoggingEnabled() |
||
453 | |||
454 | /** |
||
455 | * Symphony uses a prefix for all it's database tables so it can live peacefully |
||
456 | * on the same database as other applications. By default this is sym_, but it |
||
457 | * can be changed when Symphony is installed. |
||
458 | * |
||
459 | * @param string $prefix |
||
460 | * The table prefix for Symphony, by default this is sym_ |
||
461 | */ |
||
462 | public function setPrefix($prefix) |
||
466 | |||
467 | /** |
||
468 | * Returns the prefix used by Symphony for this Database instance. |
||
469 | * |
||
470 | * @since Symphony 2.4 |
||
471 | * @return string |
||
472 | */ |
||
473 | public function getPrefix() |
||
477 | |||
478 | /** |
||
479 | * Given a string, replace the default table prefixes with the |
||
480 | * table prefix for this database instance. |
||
481 | * |
||
482 | * @param string $query |
||
483 | * @return string |
||
484 | */ |
||
485 | public function replaceTablePrefix($query) |
||
493 | |||
494 | /** |
||
495 | * Function looks over a query to determine if it's a READ or WRITE operation. |
||
496 | * WRITE operations are any query that starts with: SET, CREATE, INSERT, REPLACE |
||
497 | * ALTER, DELETE, UPDATE, OPTIMIZE, TRUNCATE or DROP. All other queries are considered |
||
498 | * READ operations |
||
499 | * |
||
500 | * @param string $query |
||
501 | * @return integer |
||
502 | */ |
||
503 | public function determineQueryType($query) |
||
509 | |||
510 | /** |
||
511 | * @param array $values |
||
512 | * @return string |
||
513 | */ |
||
514 | public static function addPlaceholders(array $values = array()) |
||
523 | |||
524 | /** |
||
525 | * Given a query that has been prepared and an array of values to subsitute |
||
526 | * into the query, the function will return the result. |
||
527 | * |
||
528 | * @param string $query |
||
529 | * @param array $values |
||
530 | * @return PDOStatement |
||
531 | */ |
||
532 | public function insert($query, array $values) |
||
538 | |||
539 | /** |
||
540 | * Returns the last insert ID from the previous query. This is |
||
541 | * the value from an auto_increment field. |
||
542 | * |
||
543 | * @return integer |
||
544 | * The last interested row's ID |
||
545 | */ |
||
546 | public function getInsertID() |
||
550 | |||
551 | /** |
||
552 | * Given a query that has been prepared and an array of values to subsitute |
||
553 | * into the query, the function will return the result. |
||
554 | * |
||
555 | * @param string $query |
||
556 | * @param array $values |
||
557 | * @return PDOStatement |
||
558 | */ |
||
559 | public function update($query, array $values) |
||
565 | |||
566 | /** |
||
567 | * Given a query that has been prepared and an array of values to subsitute |
||
568 | * into the query, the function will return the result. |
||
569 | * |
||
570 | * @param string $query |
||
571 | * @param array $values |
||
572 | * @return PDOStatement |
||
573 | */ |
||
574 | public function delete($query, array $values) |
||
580 | |||
581 | /** |
||
582 | * Given a query that has been prepared and an array of optional |
||
583 | * parameters, this function will return the results of a query |
||
584 | * as an array. |
||
585 | * |
||
586 | * @param string $query |
||
587 | * @param array $params |
||
588 | * - `fetch-type` = 'ASSOC'/'OBJECT' |
||
589 | * Return result as array or an object |
||
590 | * - `index` = 'column_name' |
||
591 | * The name of a column in the table to use it's value to index |
||
592 | * the result by. If this is omitted (and it is by default), an |
||
593 | * array of associative arrays is returned, with the key being the |
||
594 | * column names |
||
595 | * `offset` = `0` |
||
596 | * An integer representing the row to return |
||
597 | * @return array |
||
598 | */ |
||
599 | public function fetch($query = null, array $params = array(), array $values = array()) |
||
623 | |||
624 | /** |
||
625 | * Takes an SQL string and creates a prepared statement. |
||
626 | * |
||
627 | * @link http://php.net/manual/en/pdo.prepare.php |
||
628 | * @param string $query |
||
629 | * @param array $driver_options |
||
630 | * This array holds one or more key=>value pairs to set attribute values |
||
631 | * for the DatabaseStatement object that this method returns. |
||
632 | * @return DatabaseStatement |
||
633 | */ |
||
634 | public function prepare($query, array $driver_options = array()) |
||
640 | |||
641 | /** |
||
642 | * Create a transaction. |
||
643 | * |
||
644 | * @return DatabaseTransaction |
||
645 | */ |
||
646 | public function transaction() |
||
650 | |||
651 | /** |
||
652 | * Given a query that has been prepared and an array of values to subsitute |
||
653 | * into the query, the function will return the result. Unlike `insert` and |
||
654 | * `update`, this function is a bit of a catch all and will be able to populate |
||
655 | * `$this->_lastResult` with an array of data. This function is usually used |
||
656 | * via `fetch()`. |
||
657 | * |
||
658 | * @see fetch() |
||
659 | * @param string $query |
||
660 | * @param array $params |
||
661 | * Supports `fetch-type` and `offset` parameters for the moment |
||
662 | * @param array $values |
||
663 | * If the `$query` has placeholders, this parameter will include the data |
||
664 | * to subsitute into the placeholders |
||
665 | * @return boolean |
||
666 | */ |
||
667 | public function query($query, array $params = array(), array $values = array()) |
||
707 | |||
708 | /** |
||
709 | * This function is actually responsible for subsituting the values into |
||
710 | * the query and logging the query for basic profiling/debugging. |
||
711 | * |
||
712 | * @param string $query |
||
713 | * @param array $values |
||
714 | * @param boolean $close |
||
715 | * If true, once the query is executed, the cursor will be closed, |
||
716 | * otherwise it'll be left open for further manipulation (as done by |
||
717 | * `query()`). Defaults to `true` |
||
718 | * @return PDOStatement |
||
719 | */ |
||
720 | private function q($query, $values, $close = true) |
||
765 | |||
766 | /** |
||
767 | * Given an Exception, or called when an error occurs, this function will |
||
768 | * fire the `QueryExecutionError` delegate and then raise a `DatabaseException` |
||
769 | * |
||
770 | * @uses QueryExecutionError |
||
771 | * @throws DatabaseException |
||
772 | * @param Exception $ex |
||
773 | * The exception thrown while doing something with the Database |
||
774 | * @return void |
||
775 | */ |
||
776 | public function error(Exception $ex = null) |
||
824 | |||
825 | /** |
||
826 | * Throw a new DatabaseException when given an original exception and a query. |
||
827 | * |
||
828 | * @param Exception $error |
||
829 | * @param string $query |
||
830 | * @param string $query_hash |
||
831 | */ |
||
832 | public function throwError(Exception $error, $query, $query_hash) |
||
839 | |||
840 | /** |
||
841 | * Function is called everytime a query is executed to log it for |
||
842 | * basic profiling/debugging purposes |
||
843 | * |
||
844 | * @uses PostQueryExecution |
||
845 | * @param string $query |
||
846 | * @param string $query_hash |
||
847 | * @param integer $stop |
||
848 | */ |
||
849 | public function logQuery($query, $query_hash, $stop) |
||
898 | |||
899 | /** |
||
900 | * Returns all the log entries by type. There are two valid types, |
||
901 | * error and debug. If no type is given, the entire log is returned, |
||
902 | * otherwise only log messages for that type are returned |
||
903 | * |
||
904 | * @return array |
||
905 | * An array of associative array's. Log entries of the error type |
||
906 | * return the query the error occurred on and the error number and |
||
907 | * message from MySQL. Log entries of the debug type return the |
||
908 | * the query and the start/stop time to indicate how long it took |
||
909 | * to run |
||
910 | */ |
||
911 | public function debug($type = null) |
||
917 | |||
918 | /** |
||
919 | * Returns some basic statistics from the MySQL class about the |
||
920 | * number of queries, the time it took to query and any slow queries. |
||
921 | * A slow query is defined as one that took longer than 0.0999 seconds |
||
922 | * This function is used by the Profile devkit |
||
923 | * |
||
924 | * @return array |
||
925 | * An associative array with the number of queries, an array of slow |
||
926 | * queries and the total query time. |
||
927 | */ |
||
928 | public function getStatistics() |
||
945 | } |
||
946 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.