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) |
|
134 | |||
135 | 17 | private function handleResponse(Request $request, Promise $responsePromise) |
|
136 | { |
||
137 | /** @var HttpResponse $response */ |
||
138 | 17 | $response = yield $responsePromise;; |
|
157 | |||
158 | 16 | View Code Duplication | private function post(Request $request): Promise |
169 | |||
170 | 1 | View Code Duplication | private function get(Request $request): Promise |
181 | |||
182 | 17 | private function getHeader(string $method, Url $url, Parameter ...$parameters): Header |
|
202 | } |
||
203 |
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: