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 |
||
17 | class HttpClient implements HttpClientInterface |
||
18 | { |
||
19 | /** |
||
20 | * Imgur client |
||
21 | * |
||
22 | * @var ImgurClient |
||
23 | */ |
||
24 | protected $client; |
||
25 | |||
26 | /** |
||
27 | * HTTP client object |
||
28 | * |
||
29 | * @var GuzzleClient |
||
30 | */ |
||
31 | protected $httpClient; |
||
32 | |||
33 | /** |
||
34 | * Client constructor. |
||
35 | * @param ImgurClient $client |
||
36 | */ |
||
37 | 8 | public function __construct(ImgurClient $client) |
|
42 | |||
43 | /** |
||
44 | * @inheritdoc |
||
45 | */ |
||
46 | View Code Duplication | public function get($uri, array $queryParams = null) |
|
|
|||
47 | { |
||
48 | $options = []; |
||
49 | |||
50 | if (!empty($queryParams)) { |
||
51 | $options['query'] = $queryParams; |
||
52 | } |
||
53 | |||
54 | $response = $this->makeRequest('GET', $uri, $options); |
||
55 | |||
56 | return $response; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | View Code Duplication | public function post($uri, array $postData = null) |
|
63 | { |
||
64 | $options = []; |
||
65 | |||
66 | if (!empty($postData)) { |
||
67 | $options['form_params'] = $postData; |
||
68 | } |
||
69 | |||
70 | $response = $this->makeRequest('POST', $uri, $options); |
||
71 | |||
72 | return $response; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public function delete($uri) |
||
84 | |||
85 | /** |
||
86 | * Makes http request and handles Imgur errors |
||
87 | * |
||
88 | * @param string $method |
||
89 | * @param string $uri |
||
90 | * @param array $options |
||
91 | * @return ImgurResponseInterface |
||
92 | * @throws AuthenticationException |
||
93 | * @throws InvalidParameterException |
||
94 | * @throws NotFoundException |
||
95 | * @throws RateLimitException |
||
96 | * @throws ServerErrorException |
||
97 | * @throws \Exception |
||
98 | */ |
||
99 | 6 | protected function makeRequest($method, $uri = null, $options = []) |
|
139 | |||
140 | /** |
||
141 | * Initializes HTTP client and sets base URI |
||
142 | */ |
||
143 | 8 | protected function initHttpClient() |
|
158 | |||
159 | /** |
||
160 | * Prepare Authentication headers required by Imgur and/or Mashape |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | 8 | protected function prepareAuthenticationHeaders() |
|
179 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.