Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php declare(strict_types=1); |
||
| 39 | class Client |
||
| 40 | { |
||
| 41 | private $httpClient; |
||
| 42 | |||
| 43 | private $applicationCredentials; |
||
| 44 | |||
| 45 | private $accessToken; |
||
| 46 | |||
| 47 | 19 | public function __construct(HttpClient $httpClient, Application $applicationCredentials, AccessToken $accessToken) |
|
| 53 | |||
| 54 | public function request(Request $request): Promise |
||
| 60 | |||
| 61 | private function sendRequest(Request $request) |
||
| 62 | { |
||
| 63 | 18 | switch ($request->getMethod()) { |
|
| 64 | 18 | case 'POST': |
|
| 65 | 16 | $responsePromise = $this->post($request); |
|
| 66 | 16 | break; |
|
| 67 | |||
| 68 | 2 | case 'GET': |
|
| 69 | 1 | $responsePromise = $this->get($request); |
|
| 70 | 1 | break; |
|
| 71 | |||
| 72 | default: |
||
| 73 | 1 | throw new InvalidMethod(); |
|
| 74 | } |
||
| 75 | |||
| 76 | 17 | return resolve($this->handleResponse($request, $responsePromise)); |
|
|
|
|||
| 77 | } |
||
| 78 | |||
| 79 | private function openStream(StreamRequest $request): Promise |
||
| 80 | { |
||
| 81 | $watcher = new StreamReader; |
||
| 82 | |||
| 83 | $this->sendRequest($request)->watch([$watcher, 'onProgress']); |
||
| 84 | |||
| 85 | return $watcher->awaitStreamOpen(); |
||
| 86 | } |
||
| 87 | |||
| 88 | private function getErrorStringFromResponseBody(array $body): array |
||
| 106 | |||
| 107 | // https://dev.twitter.com/overview/api/response-codes |
||
| 108 | 16 | private function throwFromErrorResponse(HttpResponse $response, array $body) |
|
| 109 | { |
||
| 110 | 16 | list($message, $code, $extra) = $this->getErrorStringFromResponseBody($body); |
|
| 111 | |||
| 112 | $exceptions = [ |
||
| 113 | 16 | 400 => BadRequest::class, |
|
| 114 | 401 => Unauthorized::class, |
||
| 115 | 403 => Forbidden::class, |
||
| 116 | 404 => NotFound::class, |
||
| 117 | 406 => NotAcceptable::class, |
||
| 118 | 410 => Gone::class, |
||
| 119 | 420 => RateLimitTriggered::class, |
||
| 120 | 422 => UnprocessableEntity::class, |
||
| 121 | 429 => RateLimitTriggered::class, |
||
| 122 | 500 => ServerError::class, |
||
| 123 | 502 => BadGateway::class, |
||
| 124 | 503 => ServiceUnavailable::class, |
||
| 125 | 504 => GatewayTimeout::class, |
||
| 126 | ]; |
||
| 127 | |||
| 128 | 16 | if (!array_key_exists($response->getStatus(), $exceptions)) { |
|
| 129 | 3 | return; |
|
| 130 | } |
||
| 131 | |||
| 132 | 13 | throw new $exceptions[$response->getStatus()]($message, $code, null, $extra); |
|
| 133 | } |
||
| 134 | |||
| 135 | private function handleResponse(Request $request, Promise $responsePromise) |
||
| 136 | { |
||
| 137 | /** @var HttpResponse $response */ |
||
| 138 | 17 | $response = yield $responsePromise; |
|
| 139 | |||
| 140 | try { |
||
| 141 | 17 | $decoded = json_try_decode($response->getBody(), true); |
|
| 142 | 1 | } catch (JSONDecodeErrorException $e) { |
|
| 143 | 1 | throw new RequestFailed('Failed to decode response body as JSON', $e->getCode(), $e); |
|
| 144 | } |
||
| 145 | |||
| 146 | 16 | $this->throwFromErrorResponse($response, $decoded); |
|
| 147 | |||
| 148 | 3 | return $request->handleResponse($decoded); |
|
| 149 | } |
||
| 150 | |||
| 151 | View Code Duplication | private function post(Request $request): Promise |
|
| 162 | |||
| 163 | 1 | View Code Duplication | private function get(Request $request): Promise |
| 164 | { |
||
| 165 | 1 | $header = $this->getHeader('GET', $request->getEndpoint(), ...$request->getParameters()); |
|
| 166 | |||
| 174 | |||
| 175 | 17 | private function getHeader(string $method, Url $url, Parameter ...$parameters): Header |
|
| 195 | } |
||
| 196 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: