Complex classes like ConnectionParameters often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConnectionParameters, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ConnectionParameters implements \ArrayAccess, \IteratorAggregate |
||
|
|
|||
| 13 | { |
||
| 14 | private $params = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Create connection parameters from an array, a URI, or a connection string. |
||
| 18 | * |
||
| 19 | * For details on passing: |
||
| 20 | * - an array, see {@link fromArray()}; |
||
| 21 | * - a URI, see {@link fromUri()}; |
||
| 22 | * - a connection string, see {@link fromConnectionString()}. |
||
| 23 | * |
||
| 24 | * If a `ConnectionParameters` object is given, a clone is returned. |
||
| 25 | * |
||
| 26 | * @param array|string|ConnectionParameters $params array of parameters, or URI, or connection string, or object |
||
| 27 | * @return ConnectionParameters |
||
| 28 | */ |
||
| 29 | public static function create($params): ConnectionParameters |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Initializes the connection parameters from an associative array of keywords to values. |
||
| 48 | * |
||
| 49 | * The most important are the following parameters: |
||
| 50 | * - `host (string)`: the database server to connect to, |
||
| 51 | * - `port (int)`: the port to connect to, |
||
| 52 | * - `user (string)`: username to authenticate as, |
||
| 53 | * - `password (string)`: password for the given username, |
||
| 54 | * - `dbname (string)`: name of the database to connect to, |
||
| 55 | * - `connect_timeout (int)`: connection timeout (0 means to wait indefinitely), |
||
| 56 | * - `options (string)`: the runtime options to send to the server. |
||
| 57 | * |
||
| 58 | * For details, see the |
||
| 59 | * {@link http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS PostgreSQL documentation}. |
||
| 60 | * Any parameter may be omitted - the default is used then. |
||
| 61 | * |
||
| 62 | * @param array $params map: connection parameter keyword => value |
||
| 63 | * @return ConnectionParameters |
||
| 64 | */ |
||
| 65 | public static function fromArray(array $params): ConnectionParameters |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Creates a connection parameters object from an RFC 3986 URI, e.g., `"postgresql://usr@localhost:5433/db"`. |
||
| 72 | * |
||
| 73 | * The accepted URI is the same as for the libpq connect function, described in the |
||
| 74 | * {@link http://www.postgresql.org/docs/9.4/static/libpq-connect.html PostgreSQL documentation}. |
||
| 75 | * |
||
| 76 | * The following holds for the accepted URIs: |
||
| 77 | * - the URI scheme designator must either be `"postgresql"` or `"postgres"`, |
||
| 78 | * - any part of the URI is optional, |
||
| 79 | * - username and password are used as credentials when connecting, |
||
| 80 | * - server and port specify the server and port to connect to, |
||
| 81 | * - the path specifies the name of the database to connect to, |
||
| 82 | * - URI parameters are also supported, e.g., `"postgresql:///mydb?host=localhost&port=5433"`. |
||
| 83 | * |
||
| 84 | * @param string $uri |
||
| 85 | * @return ConnectionParameters |
||
| 86 | */ |
||
| 87 | public static function fromUri(string $uri): ConnectionParameters |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Creates a connection parameters object from a PostgreSQL connection string (see {@link pg_connect()}). |
||
| 129 | * |
||
| 130 | * A connection string is a set of `keyword = value` pairs, separated by space. Spaces around the equal sign are |
||
| 131 | * optional. To contain special characters, the value may be enclosed in single quotes, using backslash as the |
||
| 132 | * escape character. |
||
| 133 | * |
||
| 134 | * For details about the connection parameter keywords and values, see {@link __construct()}. |
||
| 135 | * |
||
| 136 | * @param string $connStr a PostgreSQL connection string |
||
| 137 | * @return ConnectionParameters |
||
| 138 | */ |
||
| 139 | public static function fromConnectionString(string $connStr): ConnectionParameters |
||
| 159 | |||
| 160 | private function __construct(array $params) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string connection string suitable for the pg_connect() function |
||
| 167 | */ |
||
| 168 | public function buildConnectionString(): string |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string|null |
||
| 187 | */ |
||
| 188 | public function getHost() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return int|null |
||
| 195 | */ |
||
| 196 | public function getPort() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return string|null |
||
| 203 | */ |
||
| 204 | public function getDbName() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return string|null |
||
| 211 | */ |
||
| 212 | public function getUsername() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return string|null |
||
| 219 | */ |
||
| 220 | public function getPassword() |
||
| 224 | |||
| 225 | |||
| 226 | public function getIterator() |
||
| 230 | |||
| 231 | |||
| 232 | public function offsetExists($offset) |
||
| 236 | |||
| 237 | public function offsetGet($offset) |
||
| 241 | |||
| 242 | public function offsetSet($offset, $value) |
||
| 246 | |||
| 247 | public function offsetUnset($offset) |
||
| 251 | } |
||
| 252 |