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 |
||
| 37 | class Connection extends Component |
||
| 38 | { |
||
| 39 | const EVENT_AFTER_OPEN = 'afterOpen'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array Config |
||
| 43 | */ |
||
| 44 | public $config = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Handler |
||
| 48 | */ |
||
| 49 | protected static $_handler = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array authorization config |
||
| 53 | */ |
||
| 54 | protected $_auth = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool is auth disabled |
||
| 58 | */ |
||
| 59 | protected $_disabledAuth = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Closure Callback to test if API response has error |
||
| 63 | * The function signature: `function ($response)` |
||
| 64 | * Must return `null`, if the response does not contain an error. |
||
| 65 | */ |
||
| 66 | protected $_errorChecker; |
||
| 67 | |||
| 68 | public function setAuth($auth) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Returns auth settings. |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | 2 | public function getAuth() |
|
| 78 | { |
||
| 79 | 2 | if ($this->_disabledAuth) { |
|
| 80 | return []; |
||
| 81 | } |
||
| 82 | 2 | if ($this->_auth instanceof Closure) { |
|
| 83 | $this->_auth = call_user_func($this->_auth, $this); |
||
|
|
|||
| 84 | } |
||
| 85 | |||
| 86 | 2 | return $this->_auth; |
|
| 87 | 2 | } |
|
| 88 | |||
| 89 | public function disableAuth() |
||
| 93 | |||
| 94 | public function enableAuth() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | * @throws InvalidConfigException |
||
| 102 | */ |
||
| 103 | 2 | public function init() |
|
| 104 | { |
||
| 105 | 2 | if (!$this->config['base_uri']) { |
|
| 106 | throw new InvalidConfigException('The `base_uri` config option must be set'); |
||
| 107 | } |
||
| 108 | |||
| 109 | 2 | if (!isset($this->config['headers']['User-Agent'])) { |
|
| 110 | 2 | $this->config['headers']['User-Agent'] = 'HiArt/0.x'; |
|
| 111 | } |
||
| 112 | 2 | } |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Closes the connection when this component is being serialized. |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function __sleep() |
||
| 119 | { |
||
| 120 | return array_keys(get_object_vars($this)); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the name of the DB driver for the current [[dsn]]. |
||
| 125 | * |
||
| 126 | * @return string name of the DB driver |
||
| 127 | */ |
||
| 128 | public function getDriverName() |
||
| 129 | { |
||
| 130 | return 'hiart'; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Creates a command for execution. |
||
| 135 | * |
||
| 136 | * @param array $config the configuration for the Command class |
||
| 137 | * |
||
| 138 | * @return Command the DB command |
||
| 139 | */ |
||
| 140 | public function createCommand($config = []) |
||
| 141 | { |
||
| 142 | $config['db'] = $this; |
||
| 143 | $command = new Command($config); |
||
| 144 | |||
| 145 | return $command; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Creates new query builder instance. |
||
| 150 | * |
||
| 151 | * @return QueryBuilder |
||
| 152 | */ |
||
| 153 | public function getQueryBuilder() |
||
| 154 | { |
||
| 155 | return new QueryBuilder($this); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Performs GET HTTP request. |
||
| 160 | * @param string $url URL |
||
| 161 | * @param array $query query options |
||
| 162 | * @param string $body request body |
||
| 163 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 164 | * @throws HiArtException |
||
| 165 | * @throws \yii\base\InvalidConfigException |
||
| 166 | * @return mixed response |
||
| 167 | */ |
||
| 168 | 1 | public function get($url, $query = [], $body = null, $raw = false) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Performs HEAD HTTP request. |
||
| 175 | * @param string $url URL |
||
| 176 | * @param array $query query options |
||
| 177 | * @param string $body request body |
||
| 178 | * @throws HiArtException |
||
| 179 | * @throws \yii\base\InvalidConfigException |
||
| 180 | * @return mixed response |
||
| 181 | */ |
||
| 182 | public function head($url, $query = [], $body = null) |
||
| 183 | { |
||
| 184 | return $this->makeRequest('HEAD', $url, $query, $body, $raw); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Performs POST HTTP request. |
||
| 189 | * @param string $url URL |
||
| 190 | * @param array $query query options |
||
| 191 | * @param string $body request body |
||
| 192 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 193 | * @throws HiArtException |
||
| 194 | * @throws \yii\base\InvalidConfigException |
||
| 195 | * @return mixed response |
||
| 196 | */ |
||
| 197 | public function post($url, $query = [], $body = null, $raw = false) |
||
| 198 | { |
||
| 199 | return $this->makeRequest('POST', $url, $query, $body, $raw); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Performs PUT HTTP request. |
||
| 204 | * @param string $url URL |
||
| 205 | * @param array $query query options |
||
| 206 | * @param string $body request body |
||
| 207 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 208 | * @throws HiArtException |
||
| 209 | * @throws \yii\base\InvalidConfigException |
||
| 210 | * @return mixed response |
||
| 211 | */ |
||
| 212 | public function put($url, $query = [], $body = null, $raw = false) |
||
| 213 | { |
||
| 214 | return $this->makeRequest('PUT', $url, $query, $body, $raw); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Performs DELETE HTTP request. |
||
| 219 | * @param string $url URL |
||
| 220 | * @param array $query query options |
||
| 221 | * @param string $body request body |
||
| 222 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 223 | * @throws HiArtException |
||
| 224 | * @throws \yii\base\InvalidConfigException |
||
| 225 | * @return mixed response |
||
| 226 | */ |
||
| 227 | public function delete($url, $query = [], $body = null, $raw = false) |
||
| 228 | { |
||
| 229 | return $this->makeRequest('DELETE', $url, $query, $body, $raw); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * XXX DEPRECATED in favour of post(). |
||
| 234 | * @param $url |
||
| 235 | * @param array $query |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | public function perform($url, $body = []) |
||
| 239 | { |
||
| 240 | return $this->makeRequest('DELETE', $url, [], $body); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Make request and check for error. |
||
| 245 | * @param string $url URL |
||
| 246 | * @param array $query query options, (GET parameters) |
||
| 247 | * @param string $body request body, (POST parameters) |
||
| 248 | * @param bool $raw if response body contains JSON and should be decoded |
||
| 249 | * @throws HiArtException |
||
| 250 | * @throws \yii\base\InvalidConfigException |
||
| 251 | * @return mixed response |
||
| 252 | */ |
||
| 253 | 1 | public function makeRequest($method, $url, $query = [], $body = null, $raw = false) |
|
| 254 | { |
||
| 255 | $result = $this->handleRequest($method, $this->prepareUrl($url, $query), $body, $raw); |
||
| 256 | |||
| 257 | 1 | return $this->checkResponse($result, $url, $query); |
|
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Creates URL. |
||
| 262 | * @param mixed $path path |
||
| 263 | * @param array $query query options |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | 2 | private function prepareUrl($path, array $query = []) |
|
| 267 | { |
||
| 268 | 2 | $url = $path; |
|
| 269 | $query = array_merge($this->getAuth(), $query); |
||
| 270 | 2 | if (!empty($query)) { |
|
| 271 | $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($query); |
||
| 272 | } |
||
| 273 | |||
| 274 | 2 | return $url; |
|
| 275 | 2 | } |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Handles the request with handler. |
||
| 279 | * Returns array or raw response content, if $raw is true. |
||
| 280 | * |
||
| 281 | * @param string $method POST, GET, etc |
||
| 282 | * @param string $url the URL for request, not including proto and site |
||
| 283 | * @param array|string $body the request body. When array - will be sent as POST params, otherwise - as RAW body. |
||
| 284 | * @param bool $raw Whether to decode data, when response is decodeable (JSON). |
||
| 285 | * @return array|string |
||
| 286 | */ |
||
| 287 | 2 | protected function handleRequest($method, $url, $body = null, $raw = false) |
|
| 288 | { |
||
| 289 | $method = strtoupper($method); |
||
| 290 | $profile = $method . ' ' . $url . '#' . (is_array($body) ? http_build_query($body) : $body); |
||
| 291 | $options = [(is_array($body) ? 'form_params' : 'body') => $body]; |
||
| 292 | Yii::beginProfile($profile, __METHOD__); |
||
| 293 | $response = $this->getHandler()->request($method, $url, $options); |
||
| 294 | Yii::endProfile($profile, __METHOD__); |
||
| 295 | |||
| 296 | $res = $response->getBody()->getContents(); |
||
| 297 | if (!$raw && preg_grep('|application/json|i', $response->getHeader('Content-Type'))) { |
||
| 298 | $res = Json::decode($res); |
||
| 299 | } |
||
| 300 | |||
| 301 | 2 | return $res; |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Returns the request handler (Guzzle client for the moment). |
||
| 306 | * Creates and setups handler if not set. |
||
| 307 | * @return Handler |
||
| 308 | */ |
||
| 309 | 2 | public function getHandler() |
|
| 310 | { |
||
| 311 | 2 | if (static::$_handler === null) { |
|
| 312 | static::$_handler = new Handler($this->config); |
||
| 313 | } |
||
| 314 | |||
| 315 | 2 | return static::$_handler; |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Set handler manually. |
||
| 320 | * @param Handler $value |
||
| 321 | * @return void |
||
| 322 | */ |
||
| 323 | 2 | public function setHandler($value) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @return boolean |
||
| 330 | */ |
||
| 331 | public function isDisabledAuth() |
||
| 332 | { |
||
| 333 | return $this->_disabledAuth; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param boolean $disabledAuth |
||
| 338 | */ |
||
| 339 | public function setDisabledAuth($disabledAuth) |
||
| 340 | { |
||
| 341 | $this->_disabledAuth = $disabledAuth; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Try to decode error information if it is valid json, return it if not. |
||
| 346 | * @param $body |
||
| 347 | * @return mixed |
||
| 348 | */ |
||
| 349 | protected function decodeErrorBody($body) |
||
| 350 | { |
||
| 351 | try { |
||
| 352 | $decoded = Json::decode($body); |
||
| 353 | if (isset($decoded['error'])) { |
||
| 354 | $decoded['error'] = preg_replace('/\b\w+?Exception\[/', |
||
| 355 | "<span style=\"color: red;\">\\0</span>\n ", $decoded['error']); |
||
| 356 | } |
||
| 357 | |||
| 358 | return $decoded; |
||
| 359 | } catch (InvalidParamException $e) { |
||
| 360 | return $body; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Setter for errorChecker. |
||
| 366 | * @param Closure|array $value |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | 1 | public function setErrorChecker($value) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Checks response with checkError method and raises exception if error. |
||
| 376 | * @param array $response response data from API |
||
| 377 | * @param string $url request URL |
||
| 378 | * @param array $options request data |
||
| 379 | * @throws ErrorResponseException |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | 2 | protected function checkResponse($response, $url, $options) |
|
| 383 | { |
||
| 384 | $error = $this->checkError($response); |
||
| 385 | 2 | if (isset($error)) { |
|
| 386 | throw new ErrorResponseException($error, [ |
||
| 387 | 'requestUrl' => $url, |
||
| 388 | 'request' => $options, |
||
| 389 | 'response' => $response, |
||
| 390 | ]); |
||
| 391 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Checks response with errorChecker callback and returns not null if error. |
||
| 398 | * @param array $response response data from API |
||
| 399 | * @return null|string |
||
| 400 | */ |
||
| 401 | 2 | public function checkError($response) |
|
| 409 | } |
||
| 410 |
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..