| 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 |
||
| 57 | public function createDBConnection($DBname = null, $user = null, $pass = null) |
||
| 58 | { |
||
| 59 | global $globalDBdriver, $globalDBhost, $globalDBuser, $globalDBpass, $globalDBname, $globalDebug, $globalDB, $globalDBport, $globalDBTimeOut, $globalDBretry, $globalDBPersistent; |
||
| 60 | if ($DBname === null) { |
||
| 61 | if ($user === null && $pass === null) { |
||
| 62 | $DBname = 'default'; |
||
| 63 | $globalDBSdriver = $globalDBdriver; |
||
| 64 | $globalDBShost = $globalDBhost; |
||
| 65 | $globalDBSname = $globalDBname; |
||
| 66 | $globalDBSuser = $globalDBuser; |
||
| 67 | $globalDBSpass = $globalDBpass; |
||
| 68 | if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') $globalDBSport = 3306; |
||
| 69 | else $globalDBSport = $globalDBport; |
||
| 70 | } else { |
||
| 71 | $DBname = 'default'; |
||
| 72 | $globalDBSdriver = $globalDBdriver; |
||
| 73 | $globalDBShost = $globalDBhost; |
||
| 74 | $globalDBSname = $globalDBname; |
||
| 75 | $globalDBSuser = $user; |
||
| 76 | $globalDBSpass = $pass; |
||
| 77 | if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport == '') $globalDBSport = 3306; |
||
| 78 | else $globalDBSport = $globalDBport; |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | $globalDBSdriver = $globalDB[$DBname]['driver']; |
||
| 82 | $globalDBShost = $globalDB[$DBname]['host']; |
||
| 83 | $globalDBSname = $globalDB[$DBname]['name']; |
||
| 84 | $globalDBSuser = $globalDB[$DBname]['user']; |
||
| 85 | $globalDBSpass = $globalDB[$DBname]['pass']; |
||
| 86 | if (isset($globalDB[$DBname]['port'])) $globalDBSport = $globalDB[$DBname]['port']; |
||
| 87 | else $globalDBSport = 3306; |
||
| 88 | } |
||
| 89 | // Set number of try to connect to DB |
||
| 90 | if (!isset($globalDBretry) || $globalDBretry == '' || $globalDBretry === NULL) $globalDBretry = 5; |
||
| 91 | $i = 0; |
||
| 92 | while (true) { |
||
| 93 | try { |
||
| 94 | if ($globalDBSdriver == 'mysql') { |
||
| 95 | $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;charset=utf8", $globalDBSuser, $globalDBSpass); |
||
| 96 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
||
| 97 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 98 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
||
| 99 | if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,500); |
||
| 100 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
||
| 101 | if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
||
| 102 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
||
| 103 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
||
| 104 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
||
| 105 | // Workaround against "ONLY_FULL_GROUP_BY" mode |
||
| 106 | // to enable it : $this->dbs[$DBname]->exec('SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY"'); |
||
| 107 | $this->dbs[$DBname]->exec('SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'); |
||
| 108 | // Force usage of UTC |
||
| 109 | $this->dbs[$DBname]->exec('SET SESSION time_zone = "+00:00"'); |
||
| 110 | //$this->dbs[$DBname]->exec('SET @@session.time_zone = "+00:00"'); |
||
| 111 | } else { |
||
| 112 | $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;options='--client_encoding=utf8'", $globalDBSuser, $globalDBSpass); |
||
| 113 | //$this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
||
| 114 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 115 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
||
| 116 | if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,200); |
||
| 117 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
||
| 118 | if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
||
| 119 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
||
| 120 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
||
| 121 | $this->dbs[$DBname]->exec('SET timezone="UTC"'); |
||
| 122 | } |
||
| 123 | break; |
||
| 124 | } catch(PDOException $e) { |
||
| 125 | $i++; |
||
| 126 | if (isset($globalDebug) && $globalDebug) echo 'Error connecting to DB: '.$globalDBSname.' - Error: '.$e->getMessage()."\n"; |
||
| 127 | //exit; |
||
| 128 | if ($i > $globalDBretry) return false; |
||
| 129 | //return false; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | if ($DBname === 'default') $this->db = $this->dbs['default']; |
||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 310 |