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 |
||
| 36 | class Connection extends Component |
||
| 37 | { |
||
| 38 | const EVENT_AFTER_OPEN = 'afterOpen'; |
||
| 39 | |||
| 40 | public $config = []; |
||
| 41 | |||
| 42 | public $connectionTimeout = null; |
||
| 43 | |||
| 44 | public $dataTimeout = null; |
||
| 45 | |||
| 46 | public static $curl = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Authorization config. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $_auth; |
||
| 54 | |||
| 55 | public function setAuth($auth) |
||
| 59 | |||
| 60 | public function getAuth() |
||
| 68 | |||
| 69 | public function init() |
||
| 75 | |||
| 76 | public function getHandler() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Closes the connection when this component is being serialized. |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function __sleep() |
||
| 90 | { |
||
| 91 | return array_keys(get_object_vars($this)); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns the name of the DB driver for the current [[dsn]]. |
||
| 96 | * |
||
| 97 | * @return string name of the DB driver |
||
| 98 | */ |
||
| 99 | public function getDriverName() |
||
| 100 | { |
||
| 101 | return 'hiresource'; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Creates a command for execution. |
||
| 106 | * |
||
| 107 | * @param array $config the configuration for the Command class |
||
| 108 | * |
||
| 109 | * @return Command the DB command |
||
| 110 | */ |
||
| 111 | public function createCommand($config = []) |
||
| 112 | { |
||
| 113 | $config['db'] = $this; |
||
| 114 | $command = new Command($config); |
||
| 115 | |||
| 116 | return $command; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Creates new query builder instance. |
||
| 121 | * |
||
| 122 | * @return QueryBuilder |
||
| 123 | */ |
||
| 124 | public function getQueryBuilder() |
||
| 125 | { |
||
| 126 | return new QueryBuilder($this); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Performs GET HTTP request. |
||
| 131 | * @param string $url URL |
||
| 132 | * @param array $options URL options |
||
| 133 | * @param string $body request body |
||
| 134 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 135 | * @throws HiArtException |
||
| 136 | * @throws \yii\base\InvalidConfigException |
||
| 137 | * @return mixed response |
||
| 138 | */ |
||
| 139 | public function get($url, $options = [], $body = null, $raw = false) |
||
| 140 | { |
||
| 141 | $result = $this->httpRequest('POST', $this->createUrl($url), http_build_query($options), $raw); |
||
| 142 | |||
| 143 | return $this->checkResponse($result, $url, $options); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Performs HEAD HTTP request. |
||
| 148 | * @param string $url URL |
||
| 149 | * @param array $options URL options |
||
| 150 | * @param string $body request body |
||
| 151 | * @throws HiArtException |
||
| 152 | * @throws \yii\base\InvalidConfigException |
||
| 153 | * @return mixed response |
||
| 154 | */ |
||
| 155 | public function head($url, $options = [], $body = null) |
||
| 156 | { |
||
| 157 | $result = $this->httpRequest('HEAD', $this->createUrl($url), http_build_query($options)); |
||
| 158 | |||
| 159 | return $this->checkResponse($result, $url, $options); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Performs POST HTTP request. |
||
| 164 | * @param string $url URL |
||
| 165 | * @param array $options URL options |
||
| 166 | * @param string $body request body |
||
| 167 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 168 | * @throws HiArtException |
||
| 169 | * @throws \yii\base\InvalidConfigException |
||
| 170 | * @return mixed response |
||
| 171 | */ |
||
| 172 | public function post($url, $options = [], $body = null, $raw = false) |
||
| 173 | { |
||
| 174 | $result = $this->httpRequest('POST', $this->createUrl($url), http_build_query($options), $raw); |
||
| 175 | |||
| 176 | return $this->checkResponse($result, $url, $options); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Performs PUT HTTP request. |
||
| 181 | * |
||
| 182 | * @param string $url URL |
||
| 183 | * @param array $options URL options |
||
| 184 | * @param string $body request body |
||
| 185 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 186 | * |
||
| 187 | * @throws HiArtException |
||
| 188 | * @throws \yii\base\InvalidConfigException |
||
| 189 | * |
||
| 190 | * @return mixed response |
||
| 191 | */ |
||
| 192 | public function put($url, $options = [], $body = null, $raw = false) |
||
| 193 | { |
||
| 194 | $result = $this->httpRequest('PUT', $this->createUrl($url), http_build_query($options), $raw); |
||
| 195 | |||
| 196 | return $this->checkResponse($result, $url, $options); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Performs DELETE HTTP request. |
||
| 201 | * |
||
| 202 | * @param string $url URL |
||
| 203 | * @param array $options URL options |
||
| 204 | * @param string $body request body |
||
| 205 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 206 | * |
||
| 207 | * @throws HiArtException |
||
| 208 | * @throws \yii\base\InvalidConfigException |
||
| 209 | * |
||
| 210 | * @return mixed response |
||
| 211 | */ |
||
| 212 | public function delete($url, $options = [], $body = null, $raw = false) |
||
| 213 | { |
||
| 214 | $result = $this->httpRequest('DELETE', $this->createUrl($url), http_build_query($options), $raw); |
||
| 215 | |||
| 216 | return $this->checkResponse($result, $url, $options); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param $url |
||
| 221 | * @param array $options |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | public function perform($url, $options = []) |
||
| 225 | { |
||
| 226 | $result = $this->httpRequest('POST', $this->createUrl($url), http_build_query($options)); |
||
| 227 | |||
| 228 | return $this->checkResponse($result, $url, $options); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Make request and check for error. |
||
| 233 | * @param string $url URL |
||
| 234 | * @param array $options URL options |
||
| 235 | * @param string $body request body |
||
| 236 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 237 | * @throws HiArtException |
||
| 238 | * @throws \yii\base\InvalidConfigException |
||
| 239 | * @return mixed response |
||
| 240 | */ |
||
| 241 | public function makeRequest($method, $url, $options = [], $body = null, $raw = false) |
||
| 242 | { |
||
| 243 | $result = $this->httpRequest($method, $this->createUrl($url), http_build_query($options), $raw); |
||
| 244 | |||
| 245 | return $this->checkResponse($result, $url, $options); |
||
| 246 | } |
||
| 247 | /** |
||
| 248 | * Creates URL. |
||
| 249 | * @param mixed $path path |
||
| 250 | * @param array $options URL options |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | private function createUrl($path, array $options = []) |
||
| 254 | { |
||
| 255 | $options = array_merge($this->getAuth(), $options); |
||
| 256 | if (!is_string($path)) { |
||
| 257 | $url = urldecode(reset($path)); |
||
| 258 | if (!empty($options)) { |
||
| 259 | $url .= '?' . http_build_query($options); |
||
| 260 | } |
||
| 261 | } else { |
||
| 262 | $url = $path; |
||
| 263 | if (!empty($options)) { |
||
| 264 | $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($options); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | return [$this->config['api_url'], $url]; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Performs HTTP request. |
||
| 273 | * @param string $method method name |
||
| 274 | * @param string $url URL |
||
| 275 | * @param string $requestBody request body |
||
| 276 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 277 | * @throws ErrorResponseException |
||
| 278 | * @throws HiArtException |
||
| 279 | * @return mixed if request failed |
||
| 280 | */ |
||
| 281 | protected function httpRequest($method, $url, $requestBody = null, $raw = false) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Try to decode error information if it is valid json, return it if not. |
||
| 410 | * @param $body |
||
| 411 | * @return mixed |
||
| 412 | */ |
||
| 413 | protected function decodeErrorBody($body) |
||
| 414 | { |
||
| 415 | try { |
||
| 416 | $decoded = Json::decode($body); |
||
| 417 | if (isset($decoded['error'])) { |
||
| 418 | $decoded['error'] = preg_replace('/\b\w+?Exception\[/', |
||
| 419 | "<span style=\"color: red;\">\\0</span>\n ", $decoded['error']); |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Callback to test if API response has error. |
||
| 430 | */ |
||
| 431 | public $errorChecker; |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Checks response with errorChecker callback and raises exception if error. |
||
| 435 | * @param array $response response data from API |
||
| 436 | * @param string $url request URL |
||
| 437 | * @param array $options request data |
||
| 438 | * @throws ErrorResponseException |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | protected function checkResponse($response, $url, $options) |
||
| 454 | } |
||
| 455 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..