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 | 18 | public function request(Request $request): Promise |
|
55 | { |
||
56 | 18 | return $request instanceof StreamRequest |
|
57 | ? $this->openStream($request) |
||
58 | 18 | : $this->sendRequest($request); |
|
59 | } |
||
60 | |||
61 | 18 | 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 | 16 | private function getErrorStringFromResponseBody(array $body): array |
|
89 | { |
||
90 | 16 | if (!isset($body['errors'])) { |
|
91 | 2 | return ['Unknown error', -1, []]; |
|
92 | } |
||
93 | |||
94 | 14 | $firstError = array_shift($body['errors']); |
|
95 | |||
96 | 14 | $message = $firstError['message'] ?? 'Unknown error'; |
|
97 | 14 | $code = $firstError['code'] ?? -1; |
|
98 | 14 | $extra = []; |
|
99 | |||
100 | 14 | foreach ($body['errors'] as $error) { |
|
101 | $extra[($error['code'] ?? -1)] = ($error['message'] ?? 'Unknown error'); |
||
102 | } |
||
103 | |||
104 | 14 | return [$message, $code, $extra]; |
|
105 | } |
||
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 | 17 | private function handleResponse(Request $request, Promise $responsePromise) |
|
150 | |||
151 | 16 | View Code Duplication | private function post(Request $request): Promise |
152 | { |
||
153 | 16 | $header = $this->getHeader('POST', $request->getEndpoint(), ...$request->getParameters()); |
|
154 | |||
155 | 16 | $flags = 0; |
|
156 | 16 | if ($request instanceof StreamRequest) { |
|
157 | $flags |= HttpClient::OP_STREAM; |
||
158 | } |
||
159 | |||
160 | 16 | return $this->httpClient->post($request->getEndpoint(), $header, new Body(...$request->getParameters()), $flags); |
|
161 | } |
||
162 | |||
163 | 1 | View Code Duplication | private function get(Request $request): Promise |
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: