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 |
||
44 | class OwnerStoreController extends AbstractController |
||
45 | { |
||
46 | /** |
||
47 | * @Inject("config") |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $appConfig; |
||
51 | |||
52 | /** |
||
53 | * @Inject(PluginRepository::class) |
||
54 | * @var PluginRepository |
||
55 | */ |
||
56 | protected $pluginRepository; |
||
57 | |||
58 | /** |
||
59 | * @Inject(PluginService::class) |
||
60 | * @var PluginService |
||
61 | */ |
||
62 | protected $pluginService; |
||
63 | |||
64 | /** |
||
65 | * @Inject(ComposerApiService::class) |
||
66 | * @var ComposerApiService |
||
67 | */ |
||
68 | protected $composerService; |
||
69 | |||
70 | /** |
||
71 | * @var EntityManager |
||
72 | * @Inject("orm.em") |
||
73 | */ |
||
74 | protected $em; |
||
75 | |||
76 | private static $vendorName = 'ec-cube'; |
||
77 | |||
78 | /** |
||
79 | * Owner's Store Plugin Installation Screen - Search function |
||
80 | * |
||
81 | * @Route("/{_admin}/store/plugin/search", name="admin_store_plugin_owners_search") |
||
82 | * @Template("Store/plugin_search.twig") |
||
83 | * @param Application $app |
||
84 | * @param Request $request |
||
85 | * @return array |
||
86 | */ |
||
87 | public function search(Application $app, Request $request) |
||
|
|||
88 | { |
||
89 | // Acquire downloadable plug-in information from owners store |
||
90 | $success = 0; |
||
91 | $items = array(); |
||
92 | $promotionItems = array(); |
||
93 | $message = ''; |
||
94 | // Owner's store communication |
||
95 | $url = $this->appConfig['package_repo_url'].'/search/packages.json'; |
||
96 | list($json, $info) = $this->getRequestApi($url, $app); |
||
97 | if ($json === false) { |
||
98 | $message = $this->getResponseErrorMessage($info); |
||
99 | } else { |
||
100 | $data = json_decode($json, true); |
||
101 | if (isset($data['success'])) { |
||
102 | $success = $data['success']; |
||
103 | if ($success == '1') { |
||
104 | $items = array(); |
||
105 | // Check plugin installed |
||
106 | $arrPluginInstalled = $this->pluginRepository->findAll(); |
||
107 | // Update_status 1 : not install/purchased 、2 : Installed、 3 : Update、4 : paid purchase |
||
108 | foreach ($data['item'] as $item) { |
||
109 | // Not install/purchased |
||
110 | $item['update_status'] = 1; |
||
111 | /** @var Plugin $plugin */ |
||
112 | foreach ($arrPluginInstalled as $plugin) { |
||
113 | if ($plugin->getSource() == $item['product_id']) { |
||
114 | // Need update |
||
115 | $item['update_status'] = 3; |
||
116 | if ($plugin->getVersion() == $item['version']) { |
||
117 | // Installed |
||
118 | $item['update_status'] = 2; |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | $items[] = $item; |
||
123 | } |
||
124 | |||
125 | // EC-CUBE version check |
||
126 | $arrDependency = []; |
||
127 | foreach ($items as &$item) { |
||
128 | // Not applicable version |
||
129 | $item['version_check'] = 0; |
||
130 | if (in_array(Constant::VERSION, $item['eccube_version'])) { |
||
131 | // Match version |
||
132 | $item['version_check'] = 1; |
||
133 | } |
||
134 | if ($item['price'] != '0' && $item['purchased'] == '0') { |
||
135 | // Not purchased with paid items |
||
136 | $item['update_status'] = 4; |
||
137 | } |
||
138 | |||
139 | // Add plugin dependency |
||
140 | $item['depend'] = $this->pluginService->getRequirePluginName($items, $item); |
||
141 | } |
||
142 | unset($item); |
||
143 | |||
144 | // Promotion item |
||
145 | $i = 0; |
||
146 | View Code Duplication | foreach ($items as $item) { |
|
147 | if ($item['promotion'] == 1) { |
||
148 | $promotionItems[] = $item; |
||
149 | unset($items[$i]); |
||
150 | } |
||
151 | $i++; |
||
152 | } |
||
153 | } else { |
||
154 | $message = $data['error_code'].' : '.$data['error_message']; |
||
155 | } |
||
156 | } else { |
||
157 | $success = 0; |
||
158 | $message = "EC-CUBEオーナーズストアにエラーが発生しています。"; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return [ |
||
163 | 'success' => $success, |
||
164 | 'items' => $items, |
||
165 | 'promotionItems' => $promotionItems, |
||
166 | 'message' => $message, |
||
167 | ]; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Do confirm page |
||
172 | * |
||
173 | * @Route("/{_admin}/store/plugin/{id}/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_install_confirm") |
||
174 | * @Template("Store/plugin_confirm.twig") |
||
175 | * @param Application $app |
||
176 | * @param Request $request |
||
177 | * @param string $id |
||
178 | * @return array |
||
179 | */ |
||
180 | public function doConfirm(Application $app, Request $request, $id) |
||
209 | |||
210 | /** |
||
211 | * Api Install plugin by composer connect with package repo |
||
212 | * |
||
213 | * @Route("/{_admin}/store/plugin/api/{pluginCode}/{eccubeVersion}/{version}" , name="admin_store_plugin_api_install") |
||
214 | * |
||
215 | * @param Application $app |
||
216 | * @param Request $request |
||
217 | * @param string $pluginCode |
||
218 | * @param string $eccubeVersion |
||
219 | * @param string $version |
||
220 | * @return RedirectResponse |
||
221 | */ |
||
222 | public function apiInstall(Application $app, Request $request, $pluginCode, $eccubeVersion, $version) |
||
258 | |||
259 | /** |
||
260 | * Do confirm page |
||
261 | * |
||
262 | * @Route("/{_admin}/store/plugin/delete/{id}/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_delete_confirm") |
||
263 | * @Template("Store/plugin_confirm_uninstall.twig") |
||
264 | * @param Application $app |
||
265 | * @param Plugin $Plugin |
||
266 | * @return array |
||
267 | */ |
||
268 | public function deleteConfirm(Application $app, Plugin $Plugin) |
||
292 | |||
293 | /** |
||
294 | * New ways to remove plugin: using composer command |
||
295 | * |
||
296 | * @Method("DELETE") |
||
297 | * @Route("/{_admin}/store/plugin/api/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_api_uninstall") |
||
298 | * @param Application $app |
||
299 | * @param Plugin $Plugin |
||
300 | * @return RedirectResponse |
||
301 | */ |
||
302 | public function apiUninstall(Application $app, Plugin $Plugin) |
||
321 | |||
322 | /** |
||
323 | * API request processing |
||
324 | * |
||
325 | * @param string $url |
||
326 | * @param Application $app |
||
327 | * @return array |
||
328 | */ |
||
329 | private function getRequestApi($url, $app) |
||
355 | |||
356 | /** |
||
357 | * Get message |
||
358 | * |
||
359 | * @param $info |
||
360 | * @return string |
||
361 | */ |
||
362 | View Code Duplication | private function getResponseErrorMessage($info) |
|
375 | } |
||
376 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.