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 |
||
11 | class RestClient |
||
12 | { |
||
13 | private $baseUri; |
||
14 | |||
15 | private $resource; |
||
16 | |||
17 | private $username; |
||
18 | |||
19 | private $pass; |
||
20 | |||
21 | private $headerAuthentication = []; |
||
22 | |||
23 | private $headers = []; |
||
24 | |||
25 | private $options = []; |
||
26 | |||
27 | private $rawRequest; |
||
28 | |||
29 | 1 | public function __construct( |
|
40 | |||
41 | public function addHeader($name, $value) |
||
45 | |||
46 | 1 | public function get($id = null, $payload = null) |
|
63 | |||
64 | View Code Duplication | public function post($payload) |
|
78 | |||
79 | View Code Duplication | public function put($id, $payload = null) |
|
93 | |||
94 | public function delete($id) |
||
97 | |||
98 | 2 | public function getUri($id = null) |
|
108 | |||
109 | public function setBasicAuthentication($username, $password) |
||
114 | |||
115 | public function setHeaderAuthentication($name, $value) |
||
119 | |||
120 | private function authenticate(RequestInterface $request) |
||
139 | |||
140 | private function normalizePayload($payload) |
||
148 | |||
149 | private function getMessageFactory() |
||
153 | |||
154 | protected function discoverClient() |
||
158 | |||
159 | 1 | View Code Duplication | private function sendRequest(RequestInterface $request) |
172 | |||
173 | /** |
||
174 | * Resolve the response from client. |
||
175 | * |
||
176 | * @param ResponseInterface $response |
||
177 | * @return array An array with following values: |
||
178 | * 'status': The Http status of response |
||
179 | * 'headers': An array of response headers |
||
180 | * 'body': The json decoded body response. (Since we are in |
||
181 | * RestClient) |
||
182 | * 'raw_response': The raw body response for logging purposes. |
||
183 | * 'raw_request': The raw body request for logging purposes. |
||
184 | */ |
||
185 | protected function resolveResponse(ResponseInterface $response) |
||
198 | } |
||
199 |
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.