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 |
||
| 29 | class RestClient |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Your API key. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $apiKey; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var HttpClient |
||
| 40 | */ |
||
| 41 | protected $httpClient; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $apiHost; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The version of the API to use. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $apiVersion = 'v2'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * If we should use SSL or not. |
||
| 57 | * |
||
| 58 | * @var bool |
||
| 59 | * |
||
| 60 | * @deprecated To be removed in 3.0 |
||
| 61 | */ |
||
| 62 | protected $sslEnabled = true; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $apiKey |
||
| 66 | * @param string $apiHost |
||
| 67 | * @param HttpClient $httpClient |
||
| 68 | */ |
||
| 69 | 31 | public function __construct($apiKey, $apiHost, HttpClient $httpClient = null) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $method |
||
| 78 | * @param string $uri |
||
| 79 | * @param mixed $body |
||
| 80 | * @param array $files |
||
| 81 | * @param array $headers |
||
| 82 | * |
||
| 83 | * @throws GenericHTTPError |
||
| 84 | * @throws InvalidCredentials |
||
| 85 | * @throws MissingEndpoint |
||
| 86 | * @throws MissingRequiredParameters |
||
| 87 | * |
||
| 88 | * @return \stdClass |
||
| 89 | */ |
||
| 90 | protected function send($method, $uri, $body = null, $files = [], array $headers = []) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $url |
||
| 115 | * |
||
| 116 | * @throws GenericHTTPError |
||
| 117 | * @throws InvalidCredentials |
||
| 118 | * @throws MissingEndpoint |
||
| 119 | * @throws MissingRequiredParameters |
||
| 120 | * |
||
| 121 | * @return \stdClass |
||
| 122 | */ |
||
| 123 | 1 | public function getAttachment($url) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $endpointUrl |
||
| 135 | * @param array $postData |
||
| 136 | * @param array $files |
||
| 137 | * |
||
| 138 | * @throws GenericHTTPError |
||
| 139 | * @throws InvalidCredentials |
||
| 140 | * @throws MissingEndpoint |
||
| 141 | * @throws MissingRequiredParameters |
||
| 142 | * |
||
| 143 | * @return \stdClass |
||
| 144 | */ |
||
| 145 | 5 | public function post($endpointUrl, array $postData = [], $files = []) |
|
| 146 | { |
||
| 147 | 5 | $postFiles = []; |
|
| 148 | |||
| 149 | 5 | $fields = ['message', 'attachment', 'inline']; |
|
| 150 | 5 | foreach ($fields as $fieldName) { |
|
| 151 | 5 | if (isset($files[$fieldName])) { |
|
| 152 | 3 | if (is_array($files[$fieldName])) { |
|
| 153 | 3 | foreach ($files[$fieldName] as $file) { |
|
| 154 | 3 | $postFiles[] = $this->prepareFile($fieldName, $file); |
|
| 155 | 3 | } |
|
| 156 | 3 | } else { |
|
| 157 | $postFiles[] = $this->prepareFile($fieldName, $files[$fieldName]); |
||
| 158 | } |
||
| 159 | 3 | } |
|
| 160 | 5 | } |
|
| 161 | |||
| 162 | 5 | $postDataMultipart = []; |
|
| 163 | 5 | foreach ($postData as $key => $value) { |
|
| 164 | 5 | if (is_array($value)) { |
|
| 165 | 2 | foreach ($value as $subValue) { |
|
| 166 | 2 | $postDataMultipart[] = [ |
|
| 167 | 2 | 'name' => $key, |
|
| 168 | 2 | 'contents' => $subValue, |
|
| 169 | ]; |
||
| 170 | 2 | } |
|
| 171 | 2 | } else { |
|
| 172 | 5 | $postDataMultipart[] = [ |
|
| 173 | 5 | 'name' => $key, |
|
| 174 | 5 | 'contents' => $value, |
|
| 175 | ]; |
||
| 176 | } |
||
| 177 | 5 | } |
|
| 178 | |||
| 179 | 5 | return $this->send('POST', $endpointUrl, [], array_merge($postDataMultipart, $postFiles)); |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $endpointUrl |
||
| 184 | * @param array $queryString |
||
| 185 | * |
||
| 186 | * @throws GenericHTTPError |
||
| 187 | * @throws InvalidCredentials |
||
| 188 | * @throws MissingEndpoint |
||
| 189 | * @throws MissingRequiredParameters |
||
| 190 | * |
||
| 191 | * @return \stdClass |
||
| 192 | */ |
||
| 193 | public function get($endpointUrl, $queryString = []) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $endpointUrl |
||
| 200 | * |
||
| 201 | * @throws GenericHTTPError |
||
| 202 | * @throws InvalidCredentials |
||
| 203 | * @throws MissingEndpoint |
||
| 204 | * @throws MissingRequiredParameters |
||
| 205 | * |
||
| 206 | * @return \stdClass |
||
| 207 | */ |
||
| 208 | public function delete($endpointUrl) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $endpointUrl |
||
| 215 | * @param mixed $putData |
||
| 216 | * |
||
| 217 | * @throws GenericHTTPError |
||
| 218 | * @throws InvalidCredentials |
||
| 219 | * @throws MissingEndpoint |
||
| 220 | * @throws MissingRequiredParameters |
||
| 221 | * |
||
| 222 | * @return \stdClass |
||
| 223 | */ |
||
| 224 | public function put($endpointUrl, $putData) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param ResponseInterface $responseObj |
||
| 231 | * |
||
| 232 | * @throws GenericHTTPError |
||
| 233 | * @throws InvalidCredentials |
||
| 234 | * @throws MissingEndpoint |
||
| 235 | * @throws MissingRequiredParameters |
||
| 236 | * |
||
| 237 | * @return \stdClass |
||
| 238 | */ |
||
| 239 | 1 | public function responseHandler(ResponseInterface $responseObj) |
|
| 240 | { |
||
| 241 | 1 | $httpResponseCode = (int) $responseObj->getStatusCode(); |
|
| 242 | |||
| 243 | switch ($httpResponseCode) { |
||
| 244 | 1 | case 200: |
|
| 245 | 1 | $data = (string) $responseObj->getBody(); |
|
| 246 | 1 | $jsonResponseData = json_decode($data, false); |
|
| 247 | 1 | $result = new \stdClass(); |
|
| 248 | // return response data as json if possible, raw if not |
||
| 249 | 1 | $result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData; |
|
| 250 | 1 | $result->http_response_code = $httpResponseCode; |
|
| 251 | |||
| 252 | 1 | return $result; |
|
| 253 | case 400: |
||
| 254 | throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS.$this->getResponseExceptionMessage($responseObj)); |
||
| 255 | case 401: |
||
| 256 | throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); |
||
| 257 | case 404: |
||
| 258 | throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT.$this->getResponseExceptionMessage($responseObj)); |
||
| 259 | default: |
||
| 260 | throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param ResponseInterface $responseObj |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | protected function getResponseExceptionMessage(ResponseInterface $responseObj) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Prepare a file for the postBody. |
||
| 282 | * |
||
| 283 | * @param string $fieldName |
||
| 284 | * @param string|array $filePath |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | 3 | protected function prepareFile($fieldName, $filePath) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @return HttpClient |
||
| 322 | */ |
||
| 323 | protected function getHttpClient() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $uri |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | private function getApiUrl($uri) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $apiEndpoint |
||
| 344 | * @param string $apiVersion |
||
| 345 | * @param bool $ssl |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $apiVersion |
||
| 356 | * |
||
| 357 | * @return RestClient |
||
| 358 | */ |
||
| 359 | public function setApiVersion($apiVersion) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param bool $sslEnabled |
||
| 368 | * |
||
| 369 | * @return RestClient |
||
| 370 | * |
||
| 371 | * @deprecated To be removed in 3.0 |
||
| 372 | */ |
||
| 373 | public function setSslEnabled($sslEnabled) |
||
| 379 | } |
||
| 380 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.