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 |
||
19 | class Client implements ClientInterface |
||
20 | { |
||
21 | /** |
||
22 | * Constant to represent the base string of the API calls |
||
23 | * |
||
24 | * @const string BASEURL |
||
25 | */ |
||
26 | const BASEURL = 'https://api.digitalocean.com/v2/'; |
||
27 | |||
28 | /** |
||
29 | * An HTTP Client to perform the HTTP Requests |
||
30 | * |
||
31 | * @var \GuzzleHttp\Client |
||
32 | */ |
||
33 | private $httpClient; |
||
34 | |||
35 | /** |
||
36 | * Wrapper for the GuzzleHttp\Client::get() method |
||
37 | * |
||
38 | * @param $url string |
||
39 | * @param Array $options |
||
40 | * @return \GuzzleHttp\Message\ResponseInterface |
||
41 | * @throws \GuzzleHttp\Exception\RequestException |
||
42 | * @throws \Exception |
||
43 | */ |
||
44 | View Code Duplication | public function get($url = null, Array $options = []) |
|
56 | |||
57 | /** |
||
58 | * Wrapper for the GuzzleHttp\Client::post() method |
||
59 | * |
||
60 | * @param $url string |
||
61 | * @param Array $options |
||
62 | * @return \GuzzleHttp\Message\ResponseInterface |
||
63 | * @throws \GuzzleHttp\Exception\RequestException |
||
64 | * @throws \Exception |
||
65 | */ |
||
66 | View Code Duplication | public function post($url = null, Array $options = []) |
|
78 | |||
79 | /** |
||
80 | * Method to send a request object via HTTP and retrieve a response |
||
81 | * |
||
82 | * @param \GuzzleHttp\Message\RequestInterface $request |
||
83 | * @return \GuzzleHttp\Message\ResponseInterface |
||
84 | * @throws \GuzzleHttp\Exception\RequestException |
||
85 | * @throws \Exception |
||
86 | */ |
||
87 | public function send(RequestInterface $request) |
||
99 | |||
100 | /** |
||
101 | * Method to set the HttpClient |
||
102 | * |
||
103 | * @param \GuzzleHttp\Client |
||
104 | */ |
||
105 | public function setHttpClient(HttpClient $client) |
||
109 | |||
110 | /** |
||
111 | * Method to retrieve or instantiate an Http Client |
||
112 | * |
||
113 | * @return \GuzzleHttp\Client |
||
114 | */ |
||
115 | public function getHttpClient() |
||
122 | } |
||
123 |
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.