Complex classes like RestClient 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 RestClient, and based on these observations, apply Extract Interface, too.
| 1 | <?PHP |
||
| 28 | class RestClient { |
||
| 29 | |||
| 30 | const AUTH_HTTP_SIG = 'http-signatures'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Guzzle |
||
| 34 | */ |
||
| 35 | protected $guzzle; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $apiKey; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $apiEndpoint; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $apiVersion; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $apiSecret; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $options = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $curlOptions = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | protected $verboseErrors = false; |
||
| 71 | |||
| 72 | 29 | public function __construct($apiEndpoint, $apiVersion, $apiKey, $apiSecret) { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @param array $options |
||
| 83 | * @param array $curlOptions |
||
| 84 | * @return Guzzle |
||
| 85 | */ |
||
| 86 | 29 | protected function createGuzzleClient(array $options = [], array $curlOptions = []) { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @return Guzzle |
||
| 119 | */ |
||
| 120 | public function getGuzzleClient() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * enable CURL debugging output |
||
| 126 | * |
||
| 127 | * @param bool $debug |
||
| 128 | */ |
||
| 129 | public function setCurlDebugging($debug = true) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * enable verbose errors |
||
| 137 | * |
||
| 138 | * @param bool $verboseErrors |
||
| 139 | */ |
||
| 140 | public function setVerboseErrors($verboseErrors = true) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * set cURL default option on Guzzle client |
||
| 146 | * @param string $key |
||
| 147 | * @param bool $value |
||
| 148 | */ |
||
| 149 | public function setCurlDefaultOption($key, $value) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * set the proxy config for Guzzle |
||
| 157 | * |
||
| 158 | * @param $proxy |
||
| 159 | */ |
||
| 160 | public function setProxy($proxy) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $endpointUrl |
||
| 168 | * @param array $queryString |
||
| 169 | * @param string $auth http-signatures to enable http-signature signing |
||
| 170 | * @param float $timeout timeout in seconds |
||
| 171 | * @return Response |
||
| 172 | */ |
||
| 173 | 19 | public function get($endpointUrl, $queryString = null, $auth = null, $timeout = null) { |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $endpointUrl |
||
| 179 | * @param null $queryString |
||
| 180 | * @param array|string $postData |
||
| 181 | * @param string $auth http-signatures to enable http-signature signing |
||
| 182 | * @param float $timeout timeout in seconds |
||
| 183 | * @return Response |
||
| 184 | */ |
||
| 185 | 16 | public function post($endpointUrl, $queryString = null, $postData = '', $auth = null, $timeout = null) { |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $endpointUrl |
||
| 191 | * @param null $queryString |
||
| 192 | * @param array|string $putData |
||
| 193 | * @param string $auth http-signatures to enable http-signature signing |
||
| 194 | * @param float $timeout timeout in seconds |
||
| 195 | * @return Response |
||
| 196 | */ |
||
| 197 | 1 | public function put($endpointUrl, $queryString = null, $putData = '', $auth = null, $timeout = null) { |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $endpointUrl |
||
| 203 | * @param null $queryString |
||
| 204 | * @param array|string $postData |
||
| 205 | * @param string $auth http-signatures to enable http-signature signing |
||
| 206 | * @param float $timeout timeout in seconds |
||
| 207 | * @return Response |
||
| 208 | */ |
||
| 209 | 11 | public function delete($endpointUrl, $queryString = null, $postData = null, $auth = null, $timeout = null) { |
|
| 212 | |||
| 213 | private static $replaceQuery = ['=' => '%3D', '&' => '%26']; |
||
| 214 | 21 | public static function hasQueryValue(Uri $uri, $key) { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * generic request executor |
||
| 234 | * |
||
| 235 | * @param string $method GET, POST, PUT, DELETE |
||
| 236 | * @param string $endpointUrl |
||
| 237 | * @param array $queryString |
||
| 238 | * @param array|string $body |
||
| 239 | * @param string $auth http-signatures to enable http-signature signing |
||
| 240 | * @param string $contentMD5Mode body or url |
||
| 241 | * @param float $timeout timeout in seconds |
||
| 242 | * @return Request |
||
| 243 | */ |
||
| 244 | 21 | public function buildRequest($method, $endpointUrl, $queryString = null, $body = null, $auth = null, $contentMD5Mode = null, $timeout = null) { |
|
| 290 | |||
| 291 | /** |
||
| 292 | * generic request executor |
||
| 293 | * |
||
| 294 | * @param string $method GET, POST, PUT, DELETE |
||
| 295 | * @param string $endpointUrl |
||
| 296 | * @param array $queryString |
||
| 297 | * @param array|string $body |
||
| 298 | * @param string $auth http-signatures to enable http-signature signing |
||
| 299 | * @param string $contentMD5Mode body or url |
||
| 300 | * @param float $timeout timeout in seconds |
||
| 301 | * @return Response |
||
| 302 | */ |
||
| 303 | 21 | public function request($method, $endpointUrl, $queryString = null, $body = null, $auth = null, $contentMD5Mode = null, $timeout = null) { |
|
| 309 | |||
| 310 | 21 | public function responseHandler(ResponseInterface $responseObj) { |
|
| 311 | 21 | $httpResponseCode = (int)$responseObj->getStatusCode(); |
|
| 312 | 21 | $httpResponsePhrase = (string)$responseObj->getReasonPhrase(); |
|
| 313 | 21 | $body = $responseObj->getBody(); |
|
| 314 | |||
| 315 | 21 | if ($httpResponseCode == 200) { |
|
| 316 | 21 | if (!$body) { |
|
| 317 | throw new EmptyResponse(Blocktrail::EXCEPTION_EMPTY_RESPONSE, $httpResponseCode); |
||
| 318 | } |
||
| 319 | |||
| 320 | 21 | $result = new Response($httpResponseCode, $body); |
|
| 321 | |||
| 322 | 21 | return $result; |
|
| 323 | 10 | } elseif ($httpResponseCode == 400 || $httpResponseCode == 403) { |
|
| 324 | 6 | $data = json_decode($body, true); |
|
| 325 | |||
| 326 | 6 | if ($data && isset($data['msg'], $data['code'])) { |
|
| 327 | 6 | throw new EndpointSpecificError(!is_string($data['msg']) ? json_encode($data['msg']) : $data['msg'], $data['code']); |
|
| 328 | } else { |
||
| 329 | if (preg_match("/^banned( IP)? \[(.+)\]\n?$/", $body, $m)) { |
||
| 330 | throw new BannedIP($m[2]); |
||
| 331 | } |
||
| 332 | |||
| 333 | throw new UnknownEndpointSpecificError($this->verboseErrors ? $body : Blocktrail::EXCEPTION_UNKNOWN_ENDPOINT_SPECIFIC_ERROR); |
||
| 334 | } |
||
| 335 | 10 | } elseif ($httpResponseCode == 401) { |
|
| 336 | 1 | throw new InvalidCredentials($this->verboseErrors ? $body : Blocktrail::EXCEPTION_INVALID_CREDENTIALS, $httpResponseCode); |
|
| 337 | 9 | } elseif ($httpResponseCode == 404) { |
|
| 338 | 8 | if ($httpResponsePhrase == "Endpoint Not Found") { |
|
| 339 | throw new MissingEndpoint($this->verboseErrors ? $body : Blocktrail::EXCEPTION_MISSING_ENDPOINT, $httpResponseCode); |
||
| 340 | } else { |
||
| 341 | 8 | throw new ObjectNotFound($this->verboseErrors ? $body : Blocktrail::EXCEPTION_OBJECT_NOT_FOUND, $httpResponseCode); |
|
| 342 | } |
||
| 343 | 1 | } elseif ($httpResponseCode == 500) { |
|
| 344 | 1 | throw new GenericServerError(Blocktrail::EXCEPTION_GENERIC_SERVER_ERROR . "\nServer Response: " . $body, $httpResponseCode); |
|
| 345 | } else { |
||
| 346 | throw new GenericHTTPError(Blocktrail::EXCEPTION_GENERIC_HTTP_ERROR . "\nServer Response: " . $body, $httpResponseCode); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns curent fate time in RFC1123 format, using UTC time zone |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | 21 | private function getRFC1123DateString() { |
|
| 359 | } |
||
| 360 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.