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 RequestService |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $config; |
||
17 | |||
18 | /** |
||
19 | * @var Client |
||
20 | */ |
||
21 | protected $client; |
||
22 | |||
23 | /** |
||
24 | * RequestService constructor. |
||
25 | * @param array $config |
||
26 | * @param Client $client |
||
27 | */ |
||
28 | public function __construct(array $config, Client $client) |
||
33 | |||
34 | /** |
||
35 | * @param string $url |
||
36 | * @param array $params |
||
37 | * @return array |
||
38 | */ |
||
39 | View Code Duplication | public function post($url, array $params = []) |
|
49 | |||
50 | /** |
||
51 | * @param string $url |
||
52 | * @param string $type |
||
53 | * @param array $params |
||
54 | * @return array |
||
55 | */ |
||
56 | public function postFile($url, $type, array $params = []) |
||
85 | |||
86 | /** |
||
87 | * @param string $url |
||
88 | * @param array $params |
||
89 | * @return array |
||
90 | */ |
||
91 | View Code Duplication | public function get($url, array $params = []) |
|
101 | |||
102 | /** |
||
103 | * @param string $url |
||
104 | * @param array $params |
||
105 | * @return array |
||
106 | */ |
||
107 | View Code Duplication | public function put($url, array $params = []) |
|
117 | |||
118 | /** |
||
119 | * @param string $url |
||
120 | * @return int |
||
121 | */ |
||
122 | public function delete($url) |
||
131 | |||
132 | /** |
||
133 | * @param string $url |
||
134 | * @return string |
||
135 | */ |
||
136 | protected function getUrl($url) |
||
140 | |||
141 | /** |
||
142 | * @return array |
||
143 | */ |
||
144 | protected function getHeaders() |
||
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | protected function getAuth() |
||
162 | } |
||
163 |
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.