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 = 35; |
||
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 | $this->dbs[$DBname]->exec('SET timezone="UTC"'); |
||
121 | } |
||
122 | break; |
||
123 | } catch(PDOException $e) { |
||
124 | $i++; |
||
125 | if (isset($globalDebug) && $globalDebug) echo 'Error connecting to DB: '.$globalDBSname.' - Error: '.$e->getMessage()."\n"; |
||
126 | //exit; |
||
127 | if ($i > $globalDBretry) return false; |
||
128 | //return false; |
||
129 | } |
||
130 | } |
||
131 | if ($DBname === 'default') $this->db = $this->dbs['default']; |
||
132 | return true; |
||
133 | } |
||
134 | |||
135 | public function tableExists($table) |
||
136 | { |
||
137 | global $globalDBdriver, $globalDBname; |
||
138 | if ($globalDBdriver == 'mysql') { |
||
139 | $query = "SHOW TABLES LIKE '".$table."'"; |
||
140 | } else { |
||
141 | $query = "SELECT * FROM pg_catalog.pg_tables WHERE tablename = '".$table."'"; |
||
142 | } |
||
143 | if ($this->db == NULL) return false; |
||
144 | try { |
||
145 | //$Connection = new Connection(); |
||
146 | $results = $this->db->query($query); |
||
1 ignored issue
–
show
|
|||
147 | } catch(PDOException $e) { |
||
148 | return false; |
||
149 | } |
||
150 | if($results->rowCount()>0) { |
||
151 | return true; |
||
152 | } |
||
153 | else return false; |
||
154 | } |
||
155 | |||
156 | public function connectionExists() |
||
180 | |||
181 | /* |
||
182 | * Check if index exist |
||
183 | */ |
||
184 | public function indexExists($table,$index) |
||
204 | |||
205 | /* |
||
206 | * Get columns name of a table |
||
207 | * @return Array all column name in table |
||
208 | */ |
||
209 | public function getColumnName($table) |
||
225 | |||
226 | public function getColumnType($table,$column) { |
||
231 | |||
232 | /* |
||
233 | * Check if a column name exist in a table |
||
234 | * @return Boolean column exist or not |
||
235 | */ |
||
236 | public function checkColumnName($table,$name) |
||
270 | |||
271 | /* |
||
272 | * Get schema version |
||
273 | * @return integer schema version |
||
274 | */ |
||
275 | public function check_schema_version() { |
||
295 | |||
296 | /* |
||
297 | * Check if schema version is latest_schema |
||
298 | * @return Boolean if latest version or not |
||
299 | */ |
||
300 | public function latest() { |
||
304 | |||
305 | } |
||
306 | ?> |
||
307 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.