Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Connection extends Param |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Default elastic search port. |
||
| 17 | */ |
||
| 18 | public const DEFAULT_PORT = 9200; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Default host. |
||
| 22 | */ |
||
| 23 | public const DEFAULT_HOST = 'localhost'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Default transport. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public const DEFAULT_TRANSPORT = 'Http'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Default compression. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | public const DEFAULT_COMPRESSION = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Number of seconds after a timeout occurs for every request |
||
| 41 | * If using indexing of file large value necessary. |
||
| 42 | */ |
||
| 43 | public const TIMEOUT = 300; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Number of seconds after a connection timeout occurs for every request during the connection phase. |
||
| 47 | * |
||
| 48 | * @see Connection::setConnectTimeout(); |
||
| 49 | */ |
||
| 50 | public const CONNECT_TIMEOUT = 0; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Creates a new connection object. A connection is enabled by default. |
||
| 54 | * |
||
| 55 | * @param array $params OPTIONAL Connection params: host, port, transport, timeout. All are optional |
||
| 56 | */ |
||
| 57 | public function __construct(array $params = []) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return int Server port |
||
| 70 | */ |
||
| 71 | public function getPort() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param int $port |
||
| 78 | * |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | public function setPort($port) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return string Host |
||
| 88 | */ |
||
| 89 | public function getHost() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $host |
||
| 96 | * |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | public function setHost($host) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string|null Host |
||
| 106 | */ |
||
| 107 | public function getProxy() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set proxy for http connections. Null is for environmental proxy, |
||
| 114 | * empty string to disable proxy and proxy string to set actual http proxy. |
||
| 115 | * |
||
| 116 | * @see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTPROXY |
||
| 117 | * |
||
| 118 | * @param string|null $proxy |
||
| 119 | * |
||
| 120 | * @return $this |
||
| 121 | */ |
||
| 122 | public function setProxy($proxy) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return array|string |
||
| 129 | */ |
||
| 130 | public function getTransport() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param array|string $transport |
||
| 137 | * |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function setTransport($transport) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | public function hasCompression() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param bool $compression |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function setCompression($compression = null) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getPath() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $path |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function setPath($path) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param int $timeout Timeout in seconds |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function setTimeout($timeout) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return int Connection timeout in seconds |
||
| 193 | */ |
||
| 194 | public function getTimeout() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Number of seconds after a connection timeout occurs for every request during the connection phase. |
||
| 201 | * Use a small value if you need a fast fail in case of dead, unresponsive or unreachable servers (~5 sec). |
||
| 202 | * |
||
| 203 | * Set to zero to switch to the default built-in connection timeout (300 seconds in curl). |
||
| 204 | * |
||
| 205 | * @see http://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html |
||
| 206 | * |
||
| 207 | * @param int $timeout Connect timeout in seconds |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function setConnectTimeout($timeout) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return int Connection timeout in seconds |
||
| 218 | */ |
||
| 219 | public function getConnectTimeout() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Enables a connection. |
||
| 226 | * |
||
| 227 | * @param bool $enabled OPTIONAL (default = true) |
||
| 228 | * |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | public function setEnabled($enabled = true) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return bool True if enabled |
||
| 238 | */ |
||
| 239 | public function isEnabled() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns an instance of the transport type. |
||
| 246 | * |
||
| 247 | * @throws \Elastica\Exception\InvalidException If invalid transport type |
||
| 248 | * |
||
| 249 | * @return \Elastica\Transport\AbstractTransport Transport object |
||
| 250 | */ |
||
| 251 | public function getTransportObject() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return bool Returns true if connection is persistent. True by default |
||
| 260 | */ |
||
| 261 | public function isPersistent() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | public function setConfig(array $config) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $key |
||
| 276 | * @param mixed $value |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function addConfig($key, $value) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $key |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function hasConfig($key) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns a specific config key or the whole |
||
| 301 | * config array if not set. |
||
| 302 | * |
||
| 303 | * @param string $key Config key |
||
| 304 | * |
||
| 305 | * @throws \Elastica\Exception\InvalidException |
||
| 306 | * |
||
| 307 | * @return array|string Config value |
||
| 308 | */ |
||
| 309 | public function getConfig($key = '') |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param array|\Elastica\Connection $params Params to create a connection |
||
| 325 | * |
||
| 326 | * @throws Exception\InvalidException |
||
| 327 | * |
||
| 328 | * @return self |
||
| 329 | */ |
||
| 330 | public static function create($params = []) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return string User |
||
| 345 | */ |
||
| 346 | public function getUsername() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return string Password |
||
| 353 | */ |
||
| 354 | public function getPassword() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return string AuthType |
||
| 361 | */ |
||
| 362 | public function getAuthType() |
||
| 366 | } |
||
| 367 |