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); |
||
29 | trait KitsuTrait { |
||
30 | |||
31 | /** |
||
32 | * The request builder for the MAL API |
||
33 | * @var MALRequestBuilder |
||
34 | */ |
||
35 | protected $requestBuilder; |
||
36 | |||
37 | /** |
||
38 | * The Guzzle http client object |
||
39 | * @var object |
||
40 | */ |
||
41 | protected $client; |
||
42 | |||
43 | /** |
||
44 | * Cookie jar object for api requests |
||
45 | * @var object |
||
46 | */ |
||
47 | protected $cookieJar; |
||
48 | |||
49 | /** |
||
50 | * The base url for api requests |
||
51 | * @var string $base_url |
||
52 | */ |
||
53 | protected $baseUrl = "https://kitsu.io/api/edge/"; |
||
54 | |||
55 | /** |
||
56 | * HTTP headers to send with every request |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $defaultHeaders = [ |
||
61 | 'User-Agent' => "Tim's Anime Client/4.0", |
||
62 | 'Accept-Encoding' => 'application/vnd.api+json', |
||
63 | 'Content-Type' => 'application/vnd.api+json', |
||
64 | 'client_id' => 'dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd', |
||
65 | 'client_secret' => '54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151', |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * Set the request builder object |
||
70 | * |
||
71 | * @param KitsuRequestBuilder $requestBuilder |
||
72 | * @return self |
||
73 | */ |
||
74 | public function setRequestBuilder($requestBuilder): self |
||
79 | |||
80 | /** |
||
81 | * Set up the class properties |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | protected function init() |
||
102 | |||
103 | /** |
||
104 | * Make a request via Guzzle |
||
105 | * |
||
106 | * @param string $type |
||
107 | * @param string $url |
||
108 | * @param array $options |
||
109 | * @return Response |
||
110 | */ |
||
111 | private function getResponse(string $type, string $url, array $options = []) |
||
152 | |||
153 | /** |
||
154 | * Make a request via Guzzle |
||
155 | * |
||
156 | * @param string $type |
||
157 | * @param string $url |
||
158 | * @param array $options |
||
159 | * @return array |
||
160 | */ |
||
161 | View Code Duplication | private function request(string $type, string $url, array $options = []): array |
|
181 | |||
182 | /** |
||
183 | * Remove some boilerplate for get requests |
||
184 | * |
||
185 | * @param array $args |
||
186 | * @return array |
||
187 | */ |
||
188 | protected function getRequest(...$args): array |
||
192 | |||
193 | /** |
||
194 | * Remove some boilerplate for patch requests |
||
195 | * |
||
196 | * @param array $args |
||
197 | * @return array |
||
198 | */ |
||
199 | protected function patchRequest(...$args): array |
||
203 | |||
204 | /** |
||
205 | * Remove some boilerplate for post requests |
||
206 | * |
||
207 | * @param array $args |
||
208 | * @return array |
||
209 | */ |
||
210 | View Code Duplication | protected function postRequest(...$args): array |
|
231 | |||
232 | /** |
||
233 | * Remove some boilerplate for delete requests |
||
234 | * |
||
235 | * @param array $args |
||
236 | * @return bool |
||
237 | */ |
||
238 | protected function deleteRequest(...$args): bool |
||
243 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..