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 |
||
14 | class RequestService |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $config; |
||
20 | |||
21 | /** |
||
22 | * @var Client |
||
23 | */ |
||
24 | protected $client; |
||
25 | |||
26 | /** |
||
27 | * @var int|null |
||
28 | */ |
||
29 | protected $callsLeft; |
||
30 | |||
31 | /** |
||
32 | * RequestService constructor. |
||
33 | * @param array $config |
||
34 | */ |
||
35 | public function __construct(array $config) |
||
40 | |||
41 | /** |
||
42 | * @return int|null |
||
43 | */ |
||
44 | public function getCallsLeft() |
||
48 | |||
49 | /** |
||
50 | * @param string $url |
||
51 | * @param array $params |
||
52 | * @return array |
||
53 | */ |
||
54 | View Code Duplication | public function post($url, array $params = []) |
|
64 | |||
65 | /** |
||
66 | * @param string $url |
||
67 | * @param string $name |
||
68 | * @param array $params |
||
69 | * @return array |
||
70 | */ |
||
71 | public function postMultipart($url, $name, array $params = []) |
||
109 | |||
110 | /** |
||
111 | * @param string $url |
||
112 | * @param array $params |
||
113 | * @return array |
||
114 | */ |
||
115 | View Code Duplication | public function get($url, array $params = []) |
|
125 | |||
126 | /** |
||
127 | * @param string $url |
||
128 | * @param array $params |
||
129 | * @return array |
||
130 | */ |
||
131 | View Code Duplication | public function put($url, array $params = []) |
|
141 | |||
142 | /** |
||
143 | * @param string $url |
||
144 | * @return int |
||
145 | */ |
||
146 | public function delete($url) |
||
155 | |||
156 | /** |
||
157 | * @return Client |
||
158 | */ |
||
159 | protected function getClient() |
||
175 | |||
176 | /** |
||
177 | * @param string $url |
||
178 | * @return string |
||
179 | */ |
||
180 | protected function getUrl($url) |
||
184 | |||
185 | /** |
||
186 | * @return array |
||
187 | */ |
||
188 | protected function getHeaders() |
||
195 | |||
196 | /** |
||
197 | * @return array |
||
198 | */ |
||
199 | protected function getAuth() |
||
206 | } |
||
207 |
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.