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 |
||
| 4 | class Connection{ |
||
| 5 | public $db = null; |
||
| 6 | public $dbs = array(); |
||
| 7 | public $latest_schema = 26; |
||
| 8 | |||
| 9 | public function __construct($dbc = null,$dbname = null,$user = null,$pass = null) { |
||
| 36 | |||
| 37 | public function db() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Creates the database connection |
||
| 51 | * |
||
| 52 | * @return Boolean of the database connection |
||
| 53 | * |
||
| 54 | */ |
||
| 55 | |||
| 56 | public function createDBConnection($DBname = null, $user = null, $pass = null) |
||
| 57 | { |
||
| 58 | global $globalDBdriver, $globalDBhost, $globalDBuser, $globalDBpass, $globalDBname, $globalDebug, $globalDB, $globalDBport, $globalDBTimeOut, $globalDBretry, $globalDBPersistent; |
||
| 59 | if ($DBname === null) { |
||
| 60 | if ($user === null && $pass === null) { |
||
| 61 | $DBname = 'default'; |
||
| 62 | $globalDBSdriver = $globalDBdriver; |
||
| 63 | $globalDBShost = $globalDBhost; |
||
| 64 | $globalDBSname = $globalDBname; |
||
| 65 | $globalDBSuser = $globalDBuser; |
||
| 66 | $globalDBSpass = $globalDBpass; |
||
| 67 | if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') $globalDBSport = 3306; |
||
| 68 | else $globalDBSport = $globalDBport; |
||
| 69 | } else { |
||
| 70 | $DBname = 'default'; |
||
| 71 | $globalDBSdriver = $globalDBdriver; |
||
| 72 | $globalDBShost = $globalDBhost; |
||
| 73 | $globalDBSname = $globalDBname; |
||
| 74 | $globalDBSuser = $user; |
||
| 75 | $globalDBSpass = $pass; |
||
| 76 | if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') $globalDBSport = 3306; |
||
| 77 | else $globalDBSport = $globalDBport; |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | $globalDBSdriver = $globalDB[$DBname]['driver']; |
||
| 81 | $globalDBShost = $globalDB[$DBname]['host']; |
||
| 82 | $globalDBSname = $globalDB[$DBname]['name']; |
||
| 83 | $globalDBSuser = $globalDB[$DBname]['user']; |
||
| 84 | $globalDBSpass = $globalDB[$DBname]['pass']; |
||
| 85 | if (isset($globalDB[$DBname]['port'])) $globalDBSport = $globalDB[$DBname]['port']; |
||
| 86 | else $globalDBSport = 3306; |
||
| 87 | } |
||
| 88 | // Set number of try to connect to DB |
||
| 89 | if (!isset($globalDBretry) || $globalDBretry == '' || $globalDBretry === NULL) $globalDBretry = 5; |
||
| 90 | $i = 0; |
||
| 91 | while (true) { |
||
| 92 | try { |
||
| 93 | if ($globalDBSdriver == 'mysql') { |
||
| 94 | $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;charset=utf8", $globalDBSuser, $globalDBSpass); |
||
| 95 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
||
| 96 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 97 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
||
| 98 | if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,500); |
||
| 99 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
||
| 100 | if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
||
| 101 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
||
| 102 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
||
| 103 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
||
| 104 | // Workaround against "ONLY_FULL_GROUP_BY" mode |
||
| 105 | // to enable it : $this->dbs[$DBname]->exec('SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY"'); |
||
| 106 | $this->dbs[$DBname]->exec('SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'); |
||
| 107 | // Force usage of UTC |
||
| 108 | $this->dbs[$DBname]->exec('SET SESSION time_zone = "+00:00"'); |
||
| 109 | //$this->dbs[$DBname]->exec('SET @@session.time_zone = "+00:00"'); |
||
| 110 | } else { |
||
| 111 | $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;options='--client_encoding=utf8'", $globalDBSuser, $globalDBSpass); |
||
| 112 | //$this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
||
| 113 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 114 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
||
| 115 | if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,200); |
||
| 116 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
||
| 117 | if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
||
| 118 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
||
| 119 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
||
| 120 | } |
||
| 121 | break; |
||
| 122 | } catch(PDOException $e) { |
||
| 123 | $i++; |
||
| 124 | if (isset($globalDebug) && $globalDebug) echo $e->getMessage()."\n"; |
||
| 125 | //exit; |
||
| 126 | if ($i > $globalDBretry) return false; |
||
| 127 | //return false; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | if ($DBname === 'default') $this->db = $this->dbs['default']; |
||
| 131 | return true; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function tableExists($table) |
||
| 154 | |||
| 155 | public function connectionExists() |
||
| 179 | |||
| 180 | /* |
||
| 181 | * Check if index exist |
||
| 182 | */ |
||
| 183 | public function indexExists($table,$index) |
||
| 202 | |||
| 203 | /* |
||
| 204 | * Get columns name of a table |
||
| 205 | * @return Array all column name in table |
||
| 206 | */ |
||
| 207 | public function getColumnName($table) |
||
| 223 | |||
| 224 | /* |
||
| 225 | * Check if a column name exist in a table |
||
| 226 | * @return Boolean column exist or not |
||
| 227 | */ |
||
| 228 | public function checkColumnName($table,$name) |
||
| 261 | |||
| 262 | /* |
||
| 263 | * Get schema version |
||
| 264 | * @return integer schema version |
||
| 265 | */ |
||
| 266 | public function check_schema_version() { |
||
| 285 | |||
| 286 | /* |
||
| 287 | * Check if schema version is latest_schema |
||
| 288 | * @return Boolean if latest version or not |
||
| 289 | */ |
||
| 290 | public function latest() { |
||
| 294 | |||
| 295 | } |
||
| 296 | ?> |
||
| 297 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.