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 |
||
25 | class PluginApiService |
||
26 | { |
||
27 | /** |
||
28 | * Url for Api |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $apiUrl; |
||
33 | |||
34 | /** |
||
35 | * @var EccubeConfig |
||
36 | */ |
||
37 | private $eccubeConfig; |
||
38 | |||
39 | /** |
||
40 | * @var RequestStack |
||
41 | */ |
||
42 | private $requestStack; |
||
43 | |||
44 | /** |
||
45 | * @var BaseInfo |
||
46 | */ |
||
47 | private $BaseInfo; |
||
48 | /** |
||
49 | * @var PluginRepository |
||
50 | */ |
||
51 | private $pluginRepository; |
||
52 | /** |
||
53 | * @var PluginService |
||
54 | */ |
||
55 | private $pluginService; |
||
56 | |||
57 | /** |
||
58 | * PluginApiService constructor. |
||
59 | * |
||
60 | * @param EccubeConfig $eccubeConfig |
||
61 | * @param RequestStack $requestStack |
||
62 | * @param BaseInfoRepository $baseInfoRepository |
||
63 | * |
||
64 | * @param PluginRepository $pluginRepository |
||
65 | * @param PluginService $pluginService |
||
66 | * @throws \Doctrine\ORM\NoResultException |
||
67 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
68 | */ |
||
69 | View Code Duplication | public function __construct(EccubeConfig $eccubeConfig, RequestStack $requestStack, BaseInfoRepository $baseInfoRepository, PluginRepository $pluginRepository, PluginService $pluginService) |
|
77 | |||
78 | /** |
||
79 | * @return mixed |
||
80 | */ |
||
81 | public function getApiUrl() |
||
89 | |||
90 | /** |
||
91 | * @param mixed $apiUrl |
||
92 | */ |
||
93 | public function setApiUrl($apiUrl) |
||
97 | |||
98 | /** |
||
99 | * Get master data: category |
||
100 | * |
||
101 | * @return array |
||
102 | * @throws PluginApiException |
||
103 | */ |
||
104 | public function getCategory() |
||
110 | |||
111 | /** |
||
112 | * Get plugins list |
||
113 | * |
||
114 | * @param $data |
||
115 | * @return array |
||
116 | * @throws PluginApiException |
||
117 | */ |
||
118 | public function getPlugins($data) |
||
137 | |||
138 | /** |
||
139 | * Get purchased plugins list |
||
140 | * |
||
141 | * @return array |
||
142 | * @throws PluginApiException |
||
143 | */ |
||
144 | View Code Duplication | public function getPurchased() |
|
153 | |||
154 | /** |
||
155 | * Get recommended plugins list |
||
156 | * |
||
157 | * @return array($result, $info) |
||
158 | * @throws PluginApiException |
||
159 | */ |
||
160 | View Code Duplication | public function getRecommended() |
|
169 | |||
170 | private function buildPlugins(&$plugins) |
||
199 | |||
200 | /** |
||
201 | * Get a plugin |
||
202 | * |
||
203 | * @param int|string $id Id or plugin code |
||
204 | * |
||
205 | * @return array |
||
206 | * @throws PluginApiException |
||
207 | */ |
||
208 | public function getPlugin($id) |
||
216 | |||
217 | /** |
||
218 | * API request processing |
||
219 | * |
||
220 | * @param string $url |
||
221 | * @param array $data |
||
222 | * |
||
223 | * @return array |
||
224 | * @throws PluginApiException |
||
225 | */ |
||
226 | public function getRequestApi($url, $data = []) |
||
268 | } |
||
269 |
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.