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 |
||
| 45 | class OwnerStoreController extends AbstractController |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @Inject("config") |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $appConfig; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @Inject(PluginRepository::class) |
||
| 55 | * @var PluginRepository |
||
| 56 | */ |
||
| 57 | protected $pluginRepository; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @Inject(PluginService::class) |
||
| 61 | * @var PluginService |
||
| 62 | */ |
||
| 63 | protected $pluginService; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @Inject("eccube.service.composer") |
||
| 67 | * @var ComposerServiceInterface |
||
| 68 | */ |
||
| 69 | protected $composerService; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var EntityManager |
||
| 73 | * @Inject("orm.em") |
||
| 74 | */ |
||
| 75 | protected $em; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @Inject(SystemService::class) |
||
| 79 | * @var SystemService |
||
| 80 | */ |
||
| 81 | protected $systemService; |
||
| 82 | |||
| 83 | private static $vendorName = 'ec-cube'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Owner's Store Plugin Installation Screen - Search function |
||
| 87 | * |
||
| 88 | * @Route("/{_admin}/store/plugin/search", name="admin_store_plugin_owners_search") |
||
| 89 | * @Template("Store/plugin_search.twig") |
||
| 90 | * @param Application $app |
||
| 91 | * @param Request $request |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function search(Application $app, Request $request) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Do confirm page |
||
| 168 | * |
||
| 169 | * @Route("/{_admin}/store/plugin/{id}/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_install_confirm") |
||
| 170 | * @Template("Store/plugin_confirm.twig") |
||
| 171 | * @param Application $app |
||
| 172 | * @param Request $request |
||
| 173 | * @param string $id |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public function doConfirm(Application $app, Request $request, $id) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Api Install plugin by composer connect with package repo |
||
| 208 | * |
||
| 209 | * @Route("/{_admin}/store/plugin/api/{pluginCode}/{eccubeVersion}/{version}" , name="admin_store_plugin_api_install") |
||
| 210 | * |
||
| 211 | * @param Application $app |
||
| 212 | * @param Request $request |
||
| 213 | * @param string $pluginCode |
||
| 214 | * @param string $eccubeVersion |
||
| 215 | * @param string $version |
||
| 216 | * @return RedirectResponse |
||
| 217 | */ |
||
| 218 | public function apiInstall(Application $app, Request $request, $pluginCode, $eccubeVersion, $version) |
||
| 219 | { |
||
| 220 | // Check plugin code |
||
| 221 | $url = $this->appConfig['package_repo_url'].'/search/packages.json'.'?eccube_version='.$eccubeVersion.'&plugin_code='.$pluginCode.'&version='.$version; |
||
| 222 | list($json, $info) = $this->getRequestApi($url); |
||
| 223 | $existFlg = false; |
||
| 224 | $data = json_decode($json, true); |
||
| 225 | if ($data && isset($data['success'])) { |
||
| 226 | $success = $data['success']; |
||
| 227 | if ($success == '1' && isset($data['item'])) { |
||
| 228 | foreach ($data['item'] as $item) { |
||
| 229 | if ($item['product_code'] == $pluginCode) { |
||
| 230 | $existFlg = true; |
||
| 231 | break; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | if ($existFlg === false) { |
||
| 237 | log_info(sprintf('%s plugin not found!', $pluginCode)); |
||
| 238 | $app->addError('admin.plugin.not.found', 'admin'); |
||
| 239 | |||
| 240 | return $app->redirect($app->url('admin_store_plugin_owners_search')); |
||
| 241 | } |
||
| 242 | $dependents = array(); |
||
| 243 | $items = $data['item']; |
||
| 244 | $plugin = $this->pluginService->buildInfo($items, $pluginCode); |
||
| 245 | $dependents[] = $plugin; |
||
| 246 | $dependents = $this->pluginService->getDependency($items, $plugin, $dependents); |
||
| 247 | |||
| 248 | // Unset first param |
||
| 249 | unset($dependents[0]); |
||
| 250 | $dependentModifier = []; |
||
| 251 | $packageNames = ''; |
||
| 252 | if (!empty($dependents)) { |
||
| 253 | foreach ($dependents as $item) { |
||
| 254 | $packageNames .= self::$vendorName . '/' . $item['product_code'] . ' '; |
||
| 255 | $pluginItem = [ |
||
| 256 | "product_code" => $item['product_code'], |
||
| 257 | "version" => $item['version'] |
||
| 258 | ]; |
||
| 259 | array_push($dependentModifier, $pluginItem); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | $packageNames .= self::$vendorName . '/' . $pluginCode; |
||
| 263 | $return = $this->composerService->execRequire($packageNames); |
||
| 264 | $data = array( |
||
| 265 | 'code' => $pluginCode, |
||
| 266 | 'version' => $version, |
||
| 267 | 'core_version' => $eccubeVersion, |
||
| 268 | 'php_version' => phpversion(), |
||
| 269 | 'db_version' => $this->systemService->getDbversion(), |
||
| 270 | 'os' => php_uname('s') . ' ' . php_uname('r') . ' ' . php_uname('v'), |
||
| 271 | 'host' => $request->getHost(), |
||
| 272 | 'web_server' => $request->server->get("SERVER_SOFTWARE"), |
||
| 273 | 'composer_version' => $this->composerService->composerVersion(), |
||
| 274 | 'composer_execute_mode' => $this->composerService->getMode(), |
||
| 275 | 'dependents' => json_encode($dependentModifier) |
||
| 276 | ); |
||
| 277 | if ($return) { |
||
| 278 | $url = $this->appConfig['package_repo_url'] . '/report'; |
||
| 279 | $this->postRequestApi($url, $data); |
||
| 280 | $app->addSuccess('admin.plugin.install.complete', 'admin'); |
||
| 281 | |||
| 282 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 283 | } |
||
| 284 | $url = $this->appConfig['package_repo_url'] . '/report/fail'; |
||
| 285 | $this->postRequestApi($url, $data); |
||
| 286 | $app->addError('admin.plugin.install.fail', 'admin'); |
||
| 287 | |||
| 288 | return $app->redirect($app->url('admin_store_plugin_owners_search')); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Do confirm page |
||
| 293 | * |
||
| 294 | * @Route("/{_admin}/store/plugin/delete/{id}/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_delete_confirm") |
||
| 295 | * @Template("Store/plugin_confirm_uninstall.twig") |
||
| 296 | * @param Application $app |
||
| 297 | * @param Plugin $Plugin |
||
| 298 | * @return array|RedirectResponse |
||
| 299 | */ |
||
| 300 | public function deleteConfirm(Application $app, Plugin $Plugin) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * New ways to remove plugin: using composer command |
||
| 344 | * |
||
| 345 | * @Method("DELETE") |
||
| 346 | * @Route("/{_admin}/store/plugin/api/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_api_uninstall") |
||
| 347 | * @param Application $app |
||
| 348 | * @param Plugin $Plugin |
||
| 349 | * @return RedirectResponse |
||
| 350 | */ |
||
| 351 | public function apiUninstall(Application $app, Plugin $Plugin) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * API request processing |
||
| 372 | * |
||
| 373 | * @param string $url |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | private function getRequestApi($url) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * API post request processing |
||
| 405 | * |
||
| 406 | * @param string $url |
||
| 407 | * @param array $data |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | private function postRequestApi($url, $data) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get message |
||
| 429 | * |
||
| 430 | * @param $info |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | View Code Duplication | private function getResponseErrorMessage($info) |
|
| 446 | } |
||
| 447 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.