Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Connection implements ConnectionInterface |
||
| 23 | { |
||
| 24 | use ManagesTransactions; |
||
| 25 | /** |
||
| 26 | * The active PDO connection. |
||
| 27 | * |
||
| 28 | * @var \PDO|\Closure |
||
| 29 | */ |
||
| 30 | protected $pdo; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The active PDO connection used for reads. |
||
| 34 | * |
||
| 35 | * @var \PDO|\Closure |
||
| 36 | */ |
||
| 37 | protected $readPdo; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The name of the connected database. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $database; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The table prefix for the connection. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $tablePrefix = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The database connection configuration options. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $config = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The reconnector instance for the connection. |
||
| 62 | * |
||
| 63 | * @var callable |
||
| 64 | */ |
||
| 65 | protected $reconnector; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The query grammar implementation. |
||
| 69 | * |
||
| 70 | * @var \Childish\query\QueryGrammar |
||
| 71 | */ |
||
| 72 | protected $queryGrammar; |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * The query post processor implementation. |
||
| 77 | * |
||
| 78 | * @var \childish\query\Processor |
||
| 79 | */ |
||
| 80 | protected $postProcessor; |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * The default fetch mode of the connection. |
||
| 85 | * |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | protected $fetchMode = PDO::FETCH_OBJ; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The number of active transactions. |
||
| 92 | * |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | protected $transactions = 0; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Indicates if changes have been made to the database. |
||
| 99 | * |
||
| 100 | * @var int |
||
| 101 | */ |
||
| 102 | protected $recordsModified = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * All of the queries run against the connection. |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | protected $queryLog = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Indicates whether queries are being logged. |
||
| 113 | * |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $loggingQueries = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Indicates if the connection is in a "dry run". |
||
| 120 | * |
||
| 121 | * @var bool |
||
| 122 | */ |
||
| 123 | protected $pretending = false; |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * The connection resolvers. |
||
| 128 | * |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | protected static $resolvers = []; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Create a new database connection instance. |
||
| 135 | * |
||
| 136 | * @param \PDO|\Closure $pdo |
||
| 137 | * @param string $database |
||
| 138 | * @param string $tablePrefix |
||
| 139 | * @param array $config |
||
| 140 | * @return void |
||
|
|
|||
| 141 | */ |
||
| 142 | public function __construct($pdo, $database = '', $tablePrefix = '', array $config = []) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Set the query post processor to the default implementation. |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | public function useDefaultPostProcessor() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the default post processor instance. |
||
| 176 | * |
||
| 177 | * @return \Childish\query\Processor |
||
| 178 | */ |
||
| 179 | protected function getDefaultPostProcessor() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set the query grammar to the default implementation. |
||
| 186 | * |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | public function useDefaultQueryGrammar() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get the default query grammar instance. |
||
| 196 | * |
||
| 197 | * @return \Childish\query\QueryGrammar |
||
| 198 | */ |
||
| 199 | protected function getDefaultQueryGrammar() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Begin a fluent query against a database table. |
||
| 206 | * |
||
| 207 | * @param string $table |
||
| 208 | * @return \Childish\query\Builder |
||
| 209 | */ |
||
| 210 | public function table($table) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get a new query builder instance. |
||
| 217 | * |
||
| 218 | * @return \Childish\query\Builder |
||
| 219 | */ |
||
| 220 | public function query() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Run a select statement and return a single result. |
||
| 229 | * |
||
| 230 | * @param string $query |
||
| 231 | * @param array $bindings |
||
| 232 | * @param bool $useReadPdo |
||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | public function selectOne($query, $bindings = [], $useReadPdo = true) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Run a select statement against the database. |
||
| 244 | * |
||
| 245 | * @param string $query |
||
| 246 | * @param array $bindings |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public function selectFromWriteConnection($query, $bindings = []) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Run a select statement against the database. |
||
| 256 | * |
||
| 257 | * @param string $query |
||
| 258 | * @param array $bindings |
||
| 259 | * @param bool $useReadPdo |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function select($query, $bindings = [], $useReadPdo = true) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Run a select statement against the database and returns a generator. |
||
| 285 | * |
||
| 286 | * @param string $query |
||
| 287 | * @param array $bindings |
||
| 288 | * @param bool $useReadPdo |
||
| 289 | * @return \Generator |
||
| 290 | */ |
||
| 291 | public function cursor($query, $bindings = [], $useReadPdo = true) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Configure the PDO prepared statement. |
||
| 323 | * |
||
| 324 | * @param \PDOStatement $statement |
||
| 325 | * @return \PDOStatement |
||
| 326 | */ |
||
| 327 | protected function prepared($statement) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get the PDO connection to use for a select query. |
||
| 336 | * |
||
| 337 | * @param bool $useReadPdo |
||
| 338 | * @return \PDO |
||
| 339 | */ |
||
| 340 | protected function getPdoForSelect($useReadPdo = true) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Run an insert statement against the database. |
||
| 347 | * |
||
| 348 | * @param string $query |
||
| 349 | * @param array $bindings |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function insert($query, $bindings = []) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Run an update statement against the database. |
||
| 359 | * |
||
| 360 | * @param string $query |
||
| 361 | * @param array $bindings |
||
| 362 | * @return int |
||
| 363 | */ |
||
| 364 | public function update($query, $bindings = []) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Run a delete statement against the database. |
||
| 371 | * |
||
| 372 | * @param string $query |
||
| 373 | * @param array $bindings |
||
| 374 | * @return int |
||
| 375 | */ |
||
| 376 | public function delete($query, $bindings = []) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Execute an SQL statement and return the boolean result. |
||
| 383 | * |
||
| 384 | * @param string $query |
||
| 385 | * @param array $bindings |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function statement($query, $bindings = []) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Run an SQL statement and get the number of rows affected. |
||
| 407 | * |
||
| 408 | * @param string $query |
||
| 409 | * @param array $bindings |
||
| 410 | * @return int |
||
| 411 | */ |
||
| 412 | public function affectingStatement($query, $bindings = []) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Run a raw, unprepared query against the PDO connection. |
||
| 438 | * |
||
| 439 | * @param string $query |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function unprepared($query) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Execute the given callback in "dry run" mode. |
||
| 459 | * |
||
| 460 | * @param \Closure $callback |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function pretend(Closure $callback) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Execute the given callback in "dry run" mode. |
||
| 481 | * |
||
| 482 | * @param \Closure $callback |
||
| 483 | * @return array |
||
| 484 | */ |
||
| 485 | protected function withFreshQueryLog($callback) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Bind values to their parameters in the given statement. |
||
| 508 | * |
||
| 509 | * @param \PDOStatement $statement |
||
| 510 | * @param array $bindings |
||
| 511 | * @return void |
||
| 512 | */ |
||
| 513 | public function bindValues($statement, $bindings) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Prepare the query bindings for execution. |
||
| 525 | * |
||
| 526 | * @param array $bindings |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | public function prepareBindings(array $bindings) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Run a SQL statement and log its execution context. |
||
| 545 | * |
||
| 546 | * @param string $query |
||
| 547 | * @param array $bindings |
||
| 548 | * @param \Closure $callback |
||
| 549 | * @return mixed |
||
| 550 | * @throws \childish\ChildishException |
||
| 551 | */ |
||
| 552 | protected function run($query, $bindings, Closure $callback) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Run a SQL statement. |
||
| 581 | * |
||
| 582 | * @param string $query |
||
| 583 | * @param array $bindings |
||
| 584 | * @param \Closure $callback |
||
| 585 | * @return mixed |
||
| 586 | * @throws \Childish\ChildishException |
||
| 587 | */ |
||
| 588 | protected function runQueryCallback($query, $bindings, Closure $callback) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Log a query in the connection's query log. |
||
| 612 | * |
||
| 613 | * @param string $query |
||
| 614 | * @param array $bindings |
||
| 615 | * @param float|null $time |
||
| 616 | * @return void |
||
| 617 | */ |
||
| 618 | public function logQuery($query, $bindings, $time = null) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Get the elapsed time since a given starting point. |
||
| 627 | * |
||
| 628 | * @param int $start |
||
| 629 | * @return float |
||
| 630 | */ |
||
| 631 | protected function getElapsedTime($start) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Handle a query exception. |
||
| 638 | * |
||
| 639 | * @param \Exception $e |
||
| 640 | * @param string $query |
||
| 641 | * @param array $bindings |
||
| 642 | * @param \Closure $callback |
||
| 643 | * @return mixed |
||
| 644 | * @throws \Exception |
||
| 645 | */ |
||
| 646 | protected function handleQueryException($e, $query, $bindings, Closure $callback) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Handle a query exception that occurred during query execution. |
||
| 659 | * |
||
| 660 | * @param \Childish\ChildishException $e |
||
| 661 | * @param $query |
||
| 662 | * @param $bindings |
||
| 663 | * @param \Closure $callback |
||
| 664 | * @return mixed |
||
| 665 | */ |
||
| 666 | protected function tryAgainIfCausedByLostConnection(ChildishException $e, $query, $bindings, Closure $callback) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Reconnect to the database. |
||
| 679 | * |
||
| 680 | * @return void |
||
| 681 | * @throws \LogicException |
||
| 682 | */ |
||
| 683 | public function reconnect() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Reconnect to the database if a PDO connection is missing. |
||
| 694 | * |
||
| 695 | * @return void |
||
| 696 | */ |
||
| 697 | protected function reconnectIfMissingConnection() |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Disconnect from the underlying PDO connection. |
||
| 706 | * |
||
| 707 | * @return void |
||
| 708 | */ |
||
| 709 | public function disconnect() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Fire the given event if possible. |
||
| 716 | * |
||
| 717 | * @param mixed $event |
||
| 718 | * @return void |
||
| 719 | */ |
||
| 720 | protected function event($event) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Indicate if any records have been modified. |
||
| 729 | * |
||
| 730 | * @param bool $value |
||
| 731 | * @return void |
||
| 732 | */ |
||
| 733 | public function recordsHaveBeenModified($value = true) |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Get the current PDO connection. |
||
| 742 | * |
||
| 743 | * @return \PDO |
||
| 744 | */ |
||
| 745 | public function getPdo() |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Get the current PDO connection used for reading. |
||
| 756 | * |
||
| 757 | * @return \PDO |
||
| 758 | */ |
||
| 759 | public function getReadPdo() |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Set the PDO connection. |
||
| 778 | * |
||
| 779 | * @param \PDO|\Closure|null $pdo |
||
| 780 | * @return $this |
||
| 781 | */ |
||
| 782 | public function setPdo($pdo) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Set the PDO connection used for reading. |
||
| 793 | * |
||
| 794 | * @param \PDO||\Closure|null $pdo |
||
| 795 | * @return $this |
||
| 796 | */ |
||
| 797 | public function setReadPdo($pdo) |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Set the reconnect instance on the connection. |
||
| 806 | * |
||
| 807 | * @param callable $reconnector |
||
| 808 | * @return $this |
||
| 809 | */ |
||
| 810 | public function setReconnector(callable $reconnector) |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Get the database connection name. |
||
| 819 | * |
||
| 820 | * @return string|null |
||
| 821 | */ |
||
| 822 | public function getName() |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Get an option from the configuration options. |
||
| 829 | * |
||
| 830 | * @param string|null $option |
||
| 831 | * @return mixed |
||
| 832 | */ |
||
| 833 | public function getConfig($option = null) |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Get the PDO driver name. |
||
| 840 | * |
||
| 841 | * @return string |
||
| 842 | */ |
||
| 843 | public function getDriverName() |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Get the query grammar used by the connection. |
||
| 850 | * |
||
| 851 | * @return \Childish\query\QueryGrammar |
||
| 852 | */ |
||
| 853 | public function getQueryGrammar() |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Set the query grammar used by the connection. |
||
| 860 | * |
||
| 861 | * @param \Childish\query\QueryGrammar $grammar |
||
| 862 | * @return void |
||
| 863 | */ |
||
| 864 | public function setQueryGrammar(QueryGrammar $grammar) |
||
| 868 | |||
| 869 | |||
| 870 | /** |
||
| 871 | * Get the query post processor used by the connection. |
||
| 872 | * |
||
| 873 | * @return \childish\query\Processor |
||
| 874 | */ |
||
| 875 | public function getPostProcessor() |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Set the query post processor used by the connection. |
||
| 882 | * |
||
| 883 | * @param \childish\query\Processor $processor |
||
| 884 | * @return void |
||
| 885 | */ |
||
| 886 | public function setPostProcessor(Processor $processor) |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Determine if the connection in a "dry run". |
||
| 893 | * |
||
| 894 | * @return bool |
||
| 895 | */ |
||
| 896 | public function pretending() |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Get the connection query log. |
||
| 903 | * |
||
| 904 | * @return array |
||
| 905 | */ |
||
| 906 | public function getQueryLog() |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Clear the query log. |
||
| 913 | * |
||
| 914 | * @return void |
||
| 915 | */ |
||
| 916 | public function flushQueryLog() |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Enable the query log on the connection. |
||
| 923 | * |
||
| 924 | * @return void |
||
| 925 | */ |
||
| 926 | public function enableQueryLog() |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Disable the query log on the connection. |
||
| 933 | * |
||
| 934 | * @return void |
||
| 935 | */ |
||
| 936 | public function disableQueryLog() |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Determine whether we're logging queries. |
||
| 943 | * |
||
| 944 | * @return bool |
||
| 945 | */ |
||
| 946 | public function logging() |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Get the name of the connected database. |
||
| 953 | * |
||
| 954 | * @return string |
||
| 955 | */ |
||
| 956 | public function getDatabaseName() |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Set the name of the connected database. |
||
| 963 | * |
||
| 964 | * @param string $database |
||
| 965 | * @return string |
||
| 966 | */ |
||
| 967 | public function setDatabaseName($database) |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Get the table prefix for the connection. |
||
| 974 | * |
||
| 975 | * @return string |
||
| 976 | */ |
||
| 977 | public function getTablePrefix() |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Set the table prefix in use by the connection. |
||
| 984 | * |
||
| 985 | * @param string $prefix |
||
| 986 | * @return void |
||
| 987 | */ |
||
| 988 | public function setTablePrefix($prefix) |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Set the table prefix and return the grammar. |
||
| 997 | * |
||
| 998 | * @param \Childish\query\QueryGrammar $grammar |
||
| 999 | * @return \Childish\query\QueryGrammar |
||
| 1000 | */ |
||
| 1001 | public function withTablePrefix(QueryGrammar $grammar) |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Register a connection resolver. |
||
| 1010 | * |
||
| 1011 | * @param string $driver |
||
| 1012 | * @param \Closure $callback |
||
| 1013 | * @return void |
||
| 1014 | */ |
||
| 1015 | public static function resolverFor($driver, Closure $callback) |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Get the connection resolver for the given driver. |
||
| 1022 | * |
||
| 1023 | * @param string $driver |
||
| 1024 | * @return mixed |
||
| 1025 | */ |
||
| 1026 | public static function getResolver($driver) |
||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Call the given Closure with the given value then return the value. |
||
| 1033 | * |
||
| 1034 | * @param mixed $value |
||
| 1035 | * @param callable|null $callback |
||
| 1036 | * @return mixed |
||
| 1037 | */ |
||
| 1038 | public function higherOrderTap($value, $callback = null) |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Determine if the given exception was caused by a lost connection. |
||
| 1048 | * |
||
| 1049 | * @param \Exception $e |
||
| 1050 | * @return bool |
||
| 1051 | */ |
||
| 1052 | protected function causedByLostConnection(\Exception $e) |
||
| 1071 | } |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.