| Conditions | 21 |
| Paths | 3463 |
| Total Lines | 114 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 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 |
||
| 40 | function parseDSN($dsn) |
||
| 41 | { |
||
| 42 | if (is_array($dsn)) { |
||
| 43 | return $dsn; |
||
| 44 | } |
||
| 45 | |||
| 46 | $parsed = array( |
||
| 47 | 'phptype' => false, |
||
| 48 | 'dbsyntax' => false, |
||
| 49 | 'username' => false, |
||
| 50 | 'password' => false, |
||
| 51 | 'protocol' => false, |
||
| 52 | 'hostspec' => false, |
||
| 53 | 'port' => false, |
||
| 54 | 'socket' => false, |
||
| 55 | 'database' => false |
||
| 56 | ); |
||
| 57 | |||
| 58 | // Find phptype and dbsyntax |
||
| 59 | if (($pos = strpos($dsn, '://')) !== false) { |
||
| 60 | $str = substr($dsn, 0, $pos); |
||
| 61 | $dsn = substr($dsn, $pos + 3); |
||
| 62 | } else { |
||
| 63 | $str = $dsn; |
||
| 64 | $dsn = null; |
||
| 65 | } |
||
| 66 | |||
| 67 | // Get phptype and dbsyntax |
||
| 68 | // $str => phptype(dbsyntax) |
||
| 69 | if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) { |
||
| 70 | $parsed['phptype'] = $arr[1]; |
||
| 71 | $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2]; |
||
| 72 | } else { |
||
| 73 | $parsed['phptype'] = $str; |
||
| 74 | $parsed['dbsyntax'] = $str; |
||
| 75 | } |
||
| 76 | |||
| 77 | if (empty($dsn)) { |
||
| 78 | return $parsed; |
||
| 79 | } |
||
| 80 | |||
| 81 | // Get (if found): username and password |
||
| 82 | // $dsn => username:password@protocol+hostspec/database |
||
| 83 | if (($at = strrpos($dsn,'@')) !== false) { |
||
| 84 | $str = substr($dsn, 0, $at); |
||
| 85 | $dsn = substr($dsn, $at + 1); |
||
| 86 | if (($pos = strpos($str, ':')) !== false) { |
||
| 87 | $parsed['username'] = rawurldecode(substr($str, 0, $pos)); |
||
| 88 | $parsed['password'] = rawurldecode(substr($str, $pos + 1)); |
||
| 89 | } else { |
||
| 90 | $parsed['username'] = rawurldecode($str); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // Find protocol and hostspec |
||
| 95 | |||
| 96 | // $dsn => proto(proto_opts)/database |
||
| 97 | if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) { |
||
| 98 | $proto = $match[1]; |
||
| 99 | $proto_opts = (!empty($match[2])) ? $match[2] : false; |
||
| 100 | $dsn = $match[3]; |
||
| 101 | |||
| 102 | // $dsn => protocol+hostspec/database (old format) |
||
| 103 | } else { |
||
| 104 | if (strpos($dsn, '+') !== false) { |
||
| 105 | list($proto, $dsn) = explode('+', $dsn, 2); |
||
| 106 | } |
||
| 107 | if (strpos($dsn, '/') !== false) { |
||
| 108 | list($proto_opts, $dsn) = explode('/', $dsn, 2); |
||
| 109 | } else { |
||
| 110 | $proto_opts = $dsn; |
||
| 111 | $dsn = null; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | // process the different protocol options |
||
| 116 | $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp'; |
||
| 117 | $proto_opts = rawurldecode($proto_opts); |
||
| 118 | if ($parsed['protocol'] == 'tcp') { |
||
| 119 | if (strpos($proto_opts, ':') !== false) { |
||
| 120 | list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts); |
||
| 121 | } else { |
||
| 122 | $parsed['hostspec'] = $proto_opts; |
||
| 123 | } |
||
| 124 | } elseif ($parsed['protocol'] == 'unix') { |
||
| 125 | $parsed['socket'] = $proto_opts; |
||
| 126 | } |
||
| 127 | |||
| 128 | // Get dabase if any |
||
| 129 | // $dsn => database |
||
| 130 | if (!empty($dsn)) { |
||
| 131 | // /database |
||
| 132 | if (($pos = strpos($dsn, '?')) === false) { |
||
| 133 | $parsed['database'] = $dsn; |
||
| 134 | // /database?param1=value1¶m2=value2 |
||
| 135 | } else { |
||
| 136 | $parsed['database'] = substr($dsn, 0, $pos); |
||
| 137 | $dsn = substr($dsn, $pos + 1); |
||
| 138 | if (strpos($dsn, '&') !== false) { |
||
| 139 | $opts = explode('&', $dsn); |
||
| 140 | } else { // database?param1=value1 |
||
| 141 | $opts = array($dsn); |
||
| 142 | } |
||
| 143 | foreach ($opts as $opt) { |
||
| 144 | list($key, $value) = explode('=', $opt); |
||
| 145 | if (!isset($parsed[$key])) { // don't allow params overwrite |
||
| 146 | $parsed[$key] = rawurldecode($value); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | return $parsed; |
||
| 153 | } |
||
| 154 | |||
| 188 |