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 |
||
16 | trait Rest |
||
17 | { |
||
18 | /** @var array */ |
||
19 | protected $headers = []; |
||
20 | |||
21 | /** @var string */ |
||
22 | protected $apiUrl; |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $apiVersion = '1.0'; |
||
26 | |||
27 | /** |
||
28 | * @param string $apiUrl |
||
29 | * @return self |
||
30 | */ |
||
31 | public function setApiUrl(string $apiUrl) |
||
36 | |||
37 | /** |
||
38 | * @param string $apiVersion |
||
39 | * @return self |
||
40 | */ |
||
41 | public function setApiVersion(string $apiVersion) |
||
46 | |||
47 | /** |
||
48 | * @param array $header |
||
49 | * @return void |
||
50 | */ |
||
51 | public function setHeaders($header) |
||
55 | |||
56 | /** |
||
57 | * @param string $endpoint |
||
58 | * @return bool |
||
59 | */ |
||
60 | private function requireCorrelationId(string $endpoint) |
||
69 | |||
70 | /** |
||
71 | * @param string $endpoint |
||
72 | * @param array|string|null $query |
||
73 | * @param string|null $correlationId |
||
74 | * @return array|mixed |
||
75 | * @throws RequestException |
||
76 | */ |
||
77 | public function get(string $endpoint, $query = null, $correlationId = null) |
||
95 | |||
96 | /** |
||
97 | * @param string $endpoint |
||
98 | * @param array $body |
||
99 | * @param string|null $correlationId |
||
100 | * @param bool $asJson |
||
101 | * @return array|mixed |
||
102 | * @throws RequestException |
||
103 | */ |
||
104 | View Code Duplication | private function post(string $endpoint, array $body = [], $correlationId = null, bool $asJson = false) |
|
125 | |||
126 | /** |
||
127 | * @param string $endpoint |
||
128 | * @param array $body |
||
129 | * @param string|null $correlationId |
||
130 | * @param bool $asJson |
||
131 | * @param bool $attachment |
||
132 | * @param DocumentAnalysis $document |
||
133 | * @return array|mixed |
||
134 | * @throws RequestException |
||
135 | */ |
||
136 | private function put( |
||
168 | |||
169 | /** |
||
170 | * @param string $endpoint |
||
171 | * @param array $body |
||
172 | * @param string|null $correlationId |
||
173 | * @param bool $asJson |
||
174 | * @return array|mixed |
||
175 | * @throws RequestException |
||
176 | */ |
||
177 | View Code Duplication | private function patch( |
|
203 | |||
204 | /** |
||
205 | * Http delete method. |
||
206 | * |
||
207 | * @param string $endpoint |
||
208 | * @return array|mixed |
||
209 | * @throws RequestException |
||
210 | */ |
||
211 | private function delete(string $endpoint) |
||
221 | |||
222 | /** |
||
223 | * @param string $endpoint |
||
224 | * @return string |
||
225 | */ |
||
226 | private function getFinalUrl(string $endpoint) |
||
234 | } |
||
235 |