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 |
||
9 | class GuzzleHttpClient implements HttpClient |
||
10 | { |
||
11 | /** |
||
12 | * @var ClientInterface |
||
13 | */ |
||
14 | protected $client; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $responseHeaders; |
||
20 | |||
21 | /** |
||
22 | * @param ClientInterface $client |
||
23 | */ |
||
24 | public function __construct(ClientInterface $client) |
||
28 | |||
29 | /** |
||
30 | * @param string $uri |
||
31 | * @param array $params |
||
32 | * @param array $headers |
||
33 | * @return string |
||
34 | */ |
||
35 | public function get($uri, $params = [], $headers = []) |
||
47 | |||
48 | /** |
||
49 | * @param string $uri |
||
50 | * @param array $body |
||
51 | * @param array $headers |
||
52 | * @return string |
||
53 | */ |
||
54 | View Code Duplication | public function post($uri, $body = [], $headers = []) |
|
67 | |||
68 | /** |
||
69 | * @param string $uri |
||
70 | * @param array $body |
||
71 | * @param array $headers |
||
72 | * @return mixed |
||
73 | */ |
||
74 | View Code Duplication | public function put($uri, $body = [], $headers = []) |
|
87 | |||
88 | /** |
||
89 | * @param string $uri |
||
90 | * @param array $body |
||
91 | * @param array $headers |
||
92 | * @return mixed |
||
93 | */ |
||
94 | View Code Duplication | public function delete($uri, $body = [], $headers = []) |
|
105 | |||
106 | /** |
||
107 | * @param string $url |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function setBaseUrl($url) |
||
115 | |||
116 | /** |
||
117 | * @param ResponseInterface $response |
||
118 | * @return array|null |
||
119 | */ |
||
120 | protected function parseResponse(ResponseInterface $response) |
||
128 | |||
129 | /** |
||
130 | * @return array |
||
131 | */ |
||
132 | public function getResponseHeaders() |
||
136 | } |
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.