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 PluginController 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 PluginController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class PluginController extends AbstractController |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @var PluginService |
||
| 46 | */ |
||
| 47 | protected $pluginService; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var BaseInfo |
||
| 51 | */ |
||
| 52 | protected $BaseInfo; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var PluginRepository |
||
| 56 | */ |
||
| 57 | protected $pluginRepository; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var PluginApiService |
||
| 61 | */ |
||
| 62 | protected $pluginApiService; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * PluginController constructor. |
||
| 66 | * |
||
| 67 | * @param PluginRepository $pluginRepository |
||
| 68 | * @param PluginService $pluginService |
||
| 69 | * @param BaseInfoRepository $baseInfoRepository |
||
| 70 | * @param PluginApiService $pluginApiService |
||
| 71 | * |
||
| 72 | * @throws \Doctrine\ORM\NoResultException |
||
| 73 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 74 | */ |
||
| 75 | public function __construct(PluginRepository $pluginRepository, PluginService $pluginService, BaseInfoRepository $baseInfoRepository, PluginApiService $pluginApiService) |
||
| 76 | { |
||
| 77 | $this->pluginRepository = $pluginRepository; |
||
| 78 | $this->pluginService = $pluginService; |
||
| 79 | $this->BaseInfo = $baseInfoRepository->get(); |
||
| 80 | $this->pluginApiService = $pluginApiService; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * インストール済プラグイン画面 |
||
| 85 | * |
||
| 86 | * @Route("/%eccube_admin_route%/store/plugin", name="admin_store_plugin") |
||
| 87 | * @Template("@admin/Store/plugin.twig") |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | * @throws PluginException |
||
| 91 | */ |
||
| 92 | public function index() |
||
| 93 | { |
||
| 94 | $pluginForms = []; |
||
| 95 | $configPages = []; |
||
| 96 | $Plugins = $this->pluginRepository->findBy([], ['code' => 'ASC']); |
||
| 97 | |||
| 98 | // ファイル設置プラグインの取得. |
||
| 99 | $unregisteredPlugins = $this->getUnregisteredPlugins($Plugins); |
||
| 100 | $unregisteredPluginsConfigPages = []; |
||
| 101 | foreach ($unregisteredPlugins as $unregisteredPlugin) { |
||
| 102 | try { |
||
| 103 | $code = $unregisteredPlugin['code']; |
||
| 104 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
| 105 | $unregisteredPluginsConfigPages[$code] = $this->generateUrl('plugin_'.$code.'_config'); |
||
| 106 | } catch (RouteNotFoundException $e) { |
||
| 107 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $officialPlugins = []; |
||
| 112 | $unofficialPlugins = []; |
||
| 113 | |||
| 114 | foreach ($Plugins as $Plugin) { |
||
| 115 | $form = $this->formFactory |
||
| 116 | ->createNamedBuilder( |
||
| 117 | 'form'.$Plugin->getId(), |
||
| 118 | PluginManagementType::class, |
||
| 119 | null, |
||
| 120 | [ |
||
| 121 | 'plugin_id' => $Plugin->getId(), |
||
| 122 | ] |
||
| 123 | ) |
||
| 124 | ->getForm(); |
||
| 125 | $pluginForms[$Plugin->getId()] = $form->createView(); |
||
| 126 | |||
| 127 | try { |
||
| 128 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
| 129 | $configPages[$Plugin->getCode()] = $this->generateUrl(Container::underscore($Plugin->getCode()).'_admin_config'); |
||
| 130 | } catch (\Exception $e) { |
||
| 131 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
| 132 | } |
||
| 133 | if ($Plugin->getSource() == 0) { |
||
| 134 | // 商品IDが設定されていない場合、非公式プラグイン |
||
| 135 | $unofficialPlugins[] = $Plugin; |
||
| 136 | } else { |
||
| 137 | $officialPlugins[$Plugin->getSource()] = $Plugin; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | // オーナーズストア通信 |
||
| 142 | list($json,) = $this->pluginApiService->getPurchased(); |
||
| 143 | $officialPluginsDetail = []; |
||
| 144 | if ($json) { |
||
| 145 | // 接続成功時 |
||
| 146 | $data = json_decode($json, true); |
||
| 147 | foreach ($data as $item) { |
||
| 148 | if (isset($officialPlugins[$item['id']])) { |
||
| 149 | $Plugin = $officialPlugins[$item['id']]; |
||
| 150 | $officialPluginsDetail[$item['id']] = $item; |
||
| 151 | $officialPluginsDetail[$item['id']]['update_status'] = 0; |
||
| 152 | View Code Duplication | if ($this->pluginService->isUpdate($Plugin->getVersion(), $item['version'])) { |
|
| 153 | $officialPluginsDetail[$item['id']]['update_status'] = 1; |
||
| 154 | } |
||
| 155 | } else { |
||
| 156 | $Plugin = new Plugin(); |
||
| 157 | $Plugin->setName($item['name']); |
||
| 158 | $Plugin->setCode($item['code']); |
||
| 159 | $Plugin->setVersion($item['version']); |
||
| 160 | $Plugin->setSource($item['id']); |
||
| 161 | $Plugin->setEnabled(false); |
||
| 162 | $officialPlugins[$item['id']] = $Plugin; |
||
| 163 | $officialPluginsDetail[$item['id']] = $item; |
||
| 164 | $officialPluginsDetail[$item['id']]['update_status'] = 0; |
||
| 165 | View Code Duplication | if ($this->pluginService->isUpdate($Plugin->getVersion(), $item['version'])) { |
|
| 166 | $officialPluginsDetail[$item['id']]['update_status'] = 1; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | return [ |
||
| 173 | 'plugin_forms' => $pluginForms, |
||
| 174 | 'officialPlugins' => $officialPlugins, |
||
| 175 | 'unofficialPlugins' => $unofficialPlugins, |
||
| 176 | 'configPages' => $configPages, |
||
| 177 | 'unregisteredPlugins' => $unregisteredPlugins, |
||
| 178 | 'unregisteredPluginsConfigPages' => $unregisteredPluginsConfigPages, |
||
| 179 | 'officialPluginsDetail' => $officialPluginsDetail, |
||
| 180 | ]; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * インストール済プラグインからのアップデート |
||
| 185 | * |
||
| 186 | * @Route("/%eccube_admin_route%/store/plugin/{id}/update", requirements={"id" = "\d+"}, name="admin_store_plugin_update", methods={"POST"}) |
||
| 187 | * |
||
| 188 | * @param Request $request |
||
| 189 | * @param Plugin $Plugin |
||
| 190 | * |
||
| 191 | * @return RedirectResponse |
||
| 192 | */ |
||
| 193 | public function update(Request $request, Plugin $Plugin) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * 対象のプラグインを有効にします。 |
||
| 250 | * |
||
| 251 | * @Route("/%eccube_admin_route%/store/plugin/{id}/enable", requirements={"id" = "\d+"}, name="admin_store_plugin_enable", methods={"POST"}) |
||
| 252 | * |
||
| 253 | * @param Plugin $Plugin |
||
| 254 | * |
||
| 255 | * @return RedirectResponse|JsonResponse |
||
| 256 | * |
||
| 257 | * @throws PluginException |
||
| 258 | */ |
||
| 259 | public function enable(Plugin $Plugin, CacheUtil $cacheUtil, Request $request) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * 対象のプラグインを無効にします。 |
||
| 317 | * |
||
| 318 | * @Route("/%eccube_admin_route%/store/plugin/{id}/disable", requirements={"id" = "\d+"}, name="admin_store_plugin_disable", methods={"POST"}) |
||
| 319 | * |
||
| 320 | * @param Request $request |
||
| 321 | * @param Plugin $Plugin |
||
| 322 | * @param CacheUtil $cacheUtil |
||
| 323 | * |
||
| 324 | * @return \Symfony\Component\HttpFoundation\JsonResponse|RedirectResponse |
||
| 325 | */ |
||
| 326 | public function disable(Request $request, Plugin $Plugin, CacheUtil $cacheUtil) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * 対象のプラグインを削除します。 |
||
| 380 | * |
||
| 381 | * @Route("/%eccube_admin_route%/store/plugin/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_uninstall", methods={"DELETE"}) |
||
| 382 | * |
||
| 383 | * @param Plugin $Plugin |
||
| 384 | * |
||
| 385 | * @return RedirectResponse |
||
| 386 | * |
||
| 387 | * @throws \Exception |
||
| 388 | */ |
||
| 389 | public function uninstall(Plugin $Plugin) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * プラグインファイルアップロード画面 |
||
| 422 | * |
||
| 423 | * @Route("/%eccube_admin_route%/store/plugin/install", name="admin_store_plugin_install") |
||
| 424 | * @Template("@admin/Store/plugin_install.twig") |
||
| 425 | * |
||
| 426 | * @param Request $request |
||
| 427 | * |
||
| 428 | * @return array|RedirectResponse |
||
| 429 | */ |
||
| 430 | public function install(Request $request) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * 認証キー設定画面 |
||
| 485 | * |
||
| 486 | * @Route("/%eccube_admin_route%/store/plugin/authentication_setting", name="admin_store_authentication_setting") |
||
| 487 | * @Template("@admin/Store/authentication_setting.twig") |
||
| 488 | */ |
||
| 489 | public function authenticationSetting(Request $request) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * フォルダ設置のみのプラグインを取得する. |
||
| 515 | * |
||
| 516 | * @param array $plugins |
||
| 517 | * |
||
| 518 | * @return array |
||
| 519 | * @throws PluginException |
||
| 520 | */ |
||
| 521 | protected function getUnregisteredPlugins(array $plugins) |
||
| 557 | } |
||
| 558 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: