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 CurlRestClient extends RestClient |
||
10 | { |
||
11 | /** |
||
12 | * @var $url null |
||
13 | */ |
||
14 | private $url; |
||
15 | /** |
||
16 | * @var $header array |
||
17 | */ |
||
18 | private $header; |
||
19 | /** |
||
20 | * @var $auth array |
||
21 | */ |
||
22 | private $auth; |
||
23 | |||
24 | /** |
||
25 | * @var $options array |
||
26 | */ |
||
27 | private $options = array(); |
||
28 | |||
29 | private $curl; |
||
30 | |||
31 | /** |
||
32 | * @param null $url |
||
33 | * @param array $header |
||
34 | * @param array $auth |
||
35 | * @param array $options |
||
36 | */ |
||
37 | public function __construct($url = null, $header = array(), $auth = array(), $options = array()) |
||
44 | |||
45 | /** |
||
46 | * function setHeader |
||
47 | * @param array $header |
||
48 | */ |
||
49 | public function setHeader($header) |
||
53 | |||
54 | /** |
||
55 | * function setAuth |
||
56 | * @param array $auth |
||
57 | */ |
||
58 | public function setAuth($auth) |
||
62 | |||
63 | /** |
||
64 | * function setOptions |
||
65 | * @param array $options |
||
66 | */ |
||
67 | public function setOptions(array $options) |
||
71 | |||
72 | /** |
||
73 | * @param string $url |
||
74 | * @param string $method |
||
75 | * @param array $header |
||
76 | * @param array $data |
||
77 | * @param array $auth |
||
78 | * @param bool $forceInit |
||
79 | * @return mixed |
||
80 | * @throws \Exception |
||
81 | */ |
||
82 | public function executeQuery($url, $method = 'GET', $header = array(), $data = array(), $auth = array(), $forceInit = false) |
||
141 | |||
142 | /** |
||
143 | * function call |
||
144 | * @param string $method |
||
145 | * @param string $segment |
||
146 | * @param array $data |
||
147 | * @return array |
||
148 | */ |
||
149 | private function call($method, $segment, $data = array()) |
||
153 | |||
154 | /** |
||
155 | * function get |
||
156 | * @param string $segment |
||
157 | * @param array $data |
||
158 | * @return array |
||
159 | */ |
||
160 | public function get($segment, $data = array()) |
||
164 | |||
165 | /** |
||
166 | * function post |
||
167 | * @param string $segment |
||
168 | * @param array $data |
||
169 | * @return array |
||
170 | */ |
||
171 | public function post($segment, $data) |
||
175 | |||
176 | /** |
||
177 | * function put |
||
178 | * @param string $segment |
||
179 | * @param array $data |
||
180 | * @return array |
||
181 | */ |
||
182 | public function put($segment, $data) |
||
186 | |||
187 | /** |
||
188 | * function delete |
||
189 | * @param string $segment |
||
190 | * @param array $data |
||
191 | * @return array |
||
192 | */ |
||
193 | public function delete($segment, $data) |
||
197 | |||
198 | /** |
||
199 | * function getName |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getName() |
||
206 | |||
207 | public function close() |
||
215 | |||
216 | |||
217 | } |
||
218 |
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.