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:
Complex classes like OwnerStoreController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OwnerStoreController, and based on these observations, apply Extract Interface, too.
| 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 | $data = array( |
||
| 264 | 'code' => $pluginCode, |
||
| 265 | 'version' => $version, |
||
| 266 | 'core_version' => $eccubeVersion, |
||
| 267 | 'php_version' => phpversion(), |
||
| 268 | 'db_version' => $this->systemService->getDbversion(), |
||
| 269 | 'os' => php_uname('s') . ' ' . php_uname('r') . ' ' . php_uname('v'), |
||
| 270 | 'host' => $request->getHost(), |
||
| 271 | 'web_server' => $request->server->get("SERVER_SOFTWARE"), |
||
| 272 | 'composer_version' => $this->composerService->composerVersion(), |
||
| 273 | 'composer_execute_mode' => $this->composerService->getMode(), |
||
| 274 | 'dependents' => json_encode($dependentModifier) |
||
| 275 | ); |
||
| 276 | |||
| 277 | try { |
||
| 278 | $return = $this->composerService->execRequire($packageNames); |
||
| 279 | if ($return) { |
||
| 280 | // Do report to package repo |
||
| 281 | $url = $this->appConfig['package_repo_url'] . '/report'; |
||
| 282 | $this->postRequestApi($url, $data); |
||
| 283 | $app->addSuccess('admin.plugin.install.complete', 'admin'); |
||
| 284 | |||
| 285 | return $app->redirect($app->url('admin_store_plugin')); |
||
| 286 | } |
||
| 287 | } catch (\Exception $exception) { |
||
| 288 | log_info($exception); |
||
| 289 | } |
||
| 290 | |||
| 291 | // Do report to package repo |
||
| 292 | $url = $this->appConfig['package_repo_url'] . '/report/fail'; |
||
| 293 | $this->postRequestApi($url, $data); |
||
| 294 | $app->addError('admin.plugin.install.fail', 'admin'); |
||
| 295 | |||
| 296 | return $app->redirect($app->url('admin_store_plugin_owners_search')); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Do confirm page |
||
| 301 | * |
||
| 302 | * @Route("/{_admin}/store/plugin/delete/{id}/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_delete_confirm") |
||
| 303 | * @Template("Store/plugin_confirm_uninstall.twig") |
||
| 304 | * @param Application $app |
||
| 305 | * @param Plugin $Plugin |
||
| 306 | * @return array|RedirectResponse |
||
| 307 | */ |
||
| 308 | public function deleteConfirm(Application $app, Plugin $Plugin) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * New ways to remove plugin: using composer command |
||
| 352 | * |
||
| 353 | * @Method("DELETE") |
||
| 354 | * @Route("/{_admin}/store/plugin/api/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_api_uninstall") |
||
| 355 | * @param Application $app |
||
| 356 | * @param Plugin $Plugin |
||
| 357 | * @return RedirectResponse |
||
| 358 | */ |
||
| 359 | public function apiUninstall(Application $app, Plugin $Plugin) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * API request processing |
||
| 384 | * |
||
| 385 | * @param string $url |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | private function getRequestApi($url) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * API post request processing |
||
| 417 | * |
||
| 418 | * @param string $url |
||
| 419 | * @param array $data |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | private function postRequestApi($url, $data) |
||
| 423 | { |
||
| 424 | $curl = curl_init($url); |
||
| 425 | curl_setopt($curl, CURLOPT_URL, $url); |
||
| 426 | curl_setopt($curl, CURLOPT_POST, 1); |
||
| 427 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
| 428 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
||
| 429 | $result = curl_exec($curl); |
||
| 430 | $info = curl_getinfo($curl); |
||
| 431 | $message = curl_error($curl); |
||
| 432 | $info['message'] = $message; |
||
| 433 | curl_close($curl); |
||
| 434 | |||
| 435 | log_info('http post_info', $info); |
||
| 436 | return array($result, $info); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get message |
||
| 441 | * |
||
| 442 | * @param $info |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | View Code Duplication | private function getResponseErrorMessage($info) |
|
| 458 | } |
||
| 459 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.