Conditions | 25 |
Paths | 1056 |
Total Lines | 74 |
Code Lines | 61 |
Lines | 12 |
Ratio | 16.22 % |
Changes | 7 | ||
Bugs | 3 | Features | 1 |
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 |
||
45 | public function createDBConnection($DBname = null, $user = null, $pass = null) |
||
46 | { |
||
47 | global $globalDBdriver, $globalDBhost, $globalDBuser, $globalDBpass, $globalDBname, $globalDebug, $globalDB, $globalDBport, $globalDBTimeOut, $globalDBretry, $globalDBPersistent; |
||
48 | if ($DBname === null) { |
||
49 | if ($user === null && $pass === null) { |
||
50 | $DBname = 'default'; |
||
51 | $globalDBSdriver = $globalDBdriver; |
||
52 | $globalDBShost = $globalDBhost; |
||
53 | $globalDBSname = $globalDBname; |
||
54 | $globalDBSuser = $globalDBuser; |
||
55 | $globalDBSpass = $globalDBpass; |
||
56 | View Code Duplication | if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport = '') $globalDBSport = 3306; |
|
1 ignored issue
–
show
|
|||
57 | else $globalDBSport = intval($globalDBport); |
||
58 | } else { |
||
59 | $DBname = 'default'; |
||
60 | $globalDBSdriver = $globalDBdriver; |
||
61 | $globalDBShost = $globalDBhost; |
||
62 | $globalDBSname = $globalDBname; |
||
63 | $globalDBSuser = $user; |
||
64 | $globalDBSpass = $pass; |
||
65 | View Code Duplication | if (!isset($globalDBport) || $globalDBport === NULL || $globalDBport = '') $globalDBSport = 3306; |
|
1 ignored issue
–
show
|
|||
66 | else $globalDBSport = intval($globalDBport); |
||
67 | } |
||
68 | } else { |
||
69 | $globalDBSdriver = $globalDB[$DBname]['driver']; |
||
70 | $globalDBShost = $globalDB[$DBname]['host']; |
||
71 | $globalDBSname = $globalDB[$DBname]['name']; |
||
72 | $globalDBSuser = $globalDB[$DBname]['user']; |
||
73 | $globalDBSpass = $globalDB[$DBname]['pass']; |
||
74 | if (isset($globalDB[$DBname]['port'])) $globalDBSport = intval($globalDB[$DBname]['port']); |
||
75 | else $globalDBSport = 3306; |
||
76 | } |
||
77 | // Set number of try to connect to DB |
||
78 | if (!isset($globalDBretry) || $globalDBretry == '' || $globalDBretry === NULL) $globalDBretry = 5; |
||
79 | $i = 0; |
||
80 | while (true) { |
||
81 | try { |
||
82 | if ($globalDBSdriver == 'mysql') { |
||
83 | $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;charset=utf8", $globalDBSuser, $globalDBSpass); |
||
84 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
||
85 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
86 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
||
87 | View Code Duplication | if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,200); |
|
1 ignored issue
–
show
|
|||
88 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
||
89 | View Code Duplication | if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
|
1 ignored issue
–
show
|
|||
90 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
||
91 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
||
92 | $this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); |
||
93 | // Workaround against "ONLY_FULL_GROUP_BY" mode |
||
94 | // to enable it : $this->dbs[$DBname]->exec('SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY"'); |
||
1 ignored issue
–
show
|
|||
95 | $this->dbs[$DBname]->exec('SET sql_mode = "NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'); |
||
96 | } else { |
||
97 | $this->dbs[$DBname] = new PDO("$globalDBSdriver:host=$globalDBShost;port=$globalDBSport;dbname=$globalDBSname;options='--client_encoding=utf8'", $globalDBSuser, $globalDBSpass); |
||
98 | //$this->dbs[$DBname]->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'"); |
||
1 ignored issue
–
show
|
|||
99 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
100 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); |
||
101 | View Code Duplication | if (!isset($globalDBTimeOut)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,200); |
|
1 ignored issue
–
show
|
|||
102 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_TIMEOUT,$globalDBTimeOut); |
||
103 | View Code Duplication | if (!isset($globalDBPersistent)) $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,true); |
|
1 ignored issue
–
show
|
|||
104 | else $this->dbs[$DBname]->setAttribute(PDO::ATTR_PERSISTENT,$globalDBPersistent); |
||
105 | $this->dbs[$DBname]->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
||
106 | } |
||
107 | break; |
||
108 | } catch(PDOException $e) { |
||
109 | $i++; |
||
110 | if (isset($globalDebug) && $globalDebug) echo $e->getMessage()."\n"; |
||
111 | //exit; |
||
112 | if ($i > $globalDBretry) return false; |
||
113 | //return false; |
||
114 | } |
||
115 | } |
||
116 | if ($DBname === 'default') $this->db = $this->dbs['default']; |
||
117 | return true; |
||
118 | } |
||
119 | |||
266 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.