Conditions | 25 |
Paths | 1248 |
Total Lines | 78 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
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.