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 | /** |
||
50 | * @var PluginRepository |
||
51 | */ |
||
52 | private $pluginRepository; |
||
53 | |||
54 | /** |
||
55 | * @var PluginService |
||
56 | */ |
||
57 | private $pluginService; |
||
58 | |||
59 | /** |
||
60 | * PluginApiService constructor. |
||
61 | * |
||
62 | * @param EccubeConfig $eccubeConfig |
||
63 | * @param RequestStack $requestStack |
||
64 | * @param BaseInfoRepository $baseInfoRepository |
||
65 | * @param PluginRepository $pluginRepository |
||
66 | * @param PluginService $pluginService |
||
67 | * |
||
68 | * @throws \Doctrine\ORM\NoResultException |
||
69 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
70 | */ |
||
71 | View Code Duplication | public function __construct(EccubeConfig $eccubeConfig, RequestStack $requestStack, BaseInfoRepository $baseInfoRepository, PluginRepository $pluginRepository, PluginService $pluginService) |
|
79 | |||
80 | /** |
||
81 | * @return mixed |
||
82 | */ |
||
83 | public function getApiUrl() |
||
91 | |||
92 | /** |
||
93 | * @param mixed $apiUrl |
||
94 | */ |
||
95 | public function setApiUrl($apiUrl) |
||
99 | |||
100 | /** |
||
101 | * Get master data: category |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | public function getCategory() |
||
115 | |||
116 | /** |
||
117 | * Get plugins list |
||
118 | * |
||
119 | * @param $data |
||
120 | * |
||
121 | * @return array |
||
122 | * |
||
123 | * @throws PluginApiException |
||
124 | */ |
||
125 | public function getPlugins($data) |
||
144 | |||
145 | /** |
||
146 | * Get purchased plugins list |
||
147 | * |
||
148 | * @return array |
||
149 | * |
||
150 | * @throws PluginApiException |
||
151 | */ |
||
152 | View Code Duplication | public function getPurchased() |
|
161 | |||
162 | /** |
||
163 | * Get recommended plugins list |
||
164 | * |
||
165 | * @return array($result, $info) |
||
166 | * |
||
167 | * @throws PluginApiException |
||
168 | */ |
||
169 | View Code Duplication | public function getRecommended() |
|
178 | |||
179 | private function buildPlugins(&$plugins) |
||
208 | |||
209 | /** |
||
210 | * Get a plugin |
||
211 | * |
||
212 | * @param int|string $id Id or plugin code |
||
213 | * |
||
214 | * @return array |
||
215 | * |
||
216 | * @throws PluginApiException |
||
217 | */ |
||
218 | View Code Duplication | public function getPlugin($id) |
|
227 | |||
228 | /** |
||
229 | * API request processing |
||
230 | * |
||
231 | * @param string $url |
||
232 | * @param array $data |
||
233 | * |
||
234 | * @return array |
||
235 | * |
||
236 | * @throws PluginApiException |
||
237 | */ |
||
238 | public function getRequestApi($url, $data = []) |
||
280 | |||
281 | /** |
||
282 | * Get plugin information |
||
283 | * |
||
284 | * @param array $plugin |
||
285 | * |
||
286 | * @return array|null |
||
287 | */ |
||
288 | public function buildInfo(&$plugin) |
||
294 | |||
295 | /** |
||
296 | * Check support version |
||
297 | * |
||
298 | * @param $plugin |
||
299 | */ |
||
300 | public function supportedVersion(&$plugin) |
||
309 | } |
||
310 |
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.