| Conditions | 7 |
| Paths | 7 |
| Total Lines | 66 |
| 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 |
||
| 103 | private function parseDsn() |
||
| 104 | { |
||
| 105 | $dsn = $this->configuration->mustHave('dsn')->getParameter('dsn'); |
||
| 106 | if (!preg_match( |
||
| 107 | '#([a-z]+)://([^:@]+)(?::([^@]*))?(?:@([\w\.-]+|!/.+[^/]!)(?::(\w+))?)?/(.+)#', |
||
| 108 | $dsn, |
||
| 109 | $matches |
||
| 110 | )) { |
||
| 111 | throw new ConnectionException(sprintf('Could not parse DSN "%s".', $dsn)); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($matches[1] == null || $matches[1] !== 'pgsql') { |
||
| 115 | throw new ConnectionException( |
||
| 116 | sprintf( |
||
| 117 | "bad protocol information '%s' in dsn '%s'. Pomm does only support 'pgsql' for now.", |
||
| 118 | $matches[1], |
||
| 119 | $dsn |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | $adapter = $matches[1]; |
||
| 125 | |||
| 126 | if ($matches[2] === null) { |
||
| 127 | throw new ConnectionException( |
||
| 128 | sprintf( |
||
| 129 | "No user information in dsn '%s'.", |
||
| 130 | $dsn |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | $user = $matches[2]; |
||
| 136 | $pass = $matches[3]; |
||
| 137 | |||
| 138 | if (preg_match('/!(.*)!/', $matches[4], $host_matches)) { |
||
| 139 | $host = $host_matches[1]; |
||
| 140 | } else { |
||
| 141 | $host = $matches[4]; |
||
| 142 | } |
||
| 143 | |||
| 144 | $port = $matches[5]; |
||
| 145 | |||
| 146 | if ($matches[6] === null) { |
||
| 147 | throw new ConnectionException( |
||
| 148 | sprintf( |
||
| 149 | "No database name in dsn '%s'.", |
||
| 150 | $dsn |
||
| 151 | ) |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | $database = $matches[6]; |
||
| 156 | $this->configuration |
||
| 157 | ->setParameter('adapter', $adapter) |
||
| 158 | ->setParameter('user', $user) |
||
| 159 | ->setParameter('pass', $pass) |
||
| 160 | ->setParameter('host', $host) |
||
| 161 | ->setParameter('port', $port) |
||
| 162 | ->setParameter('database', $database) |
||
| 163 | ->mustHave('user') |
||
| 164 | ->mustHave('database') |
||
| 165 | ; |
||
| 166 | |||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 251 |