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 |
||
| 43 | class PluginController extends AbstractController |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @var PluginService |
||
| 47 | */ |
||
| 48 | protected $pluginService; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var BaseInfo |
||
| 52 | */ |
||
| 53 | protected $BaseInfo; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var PluginRepository |
||
| 57 | */ |
||
| 58 | protected $pluginRepository; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var PluginApiService |
||
| 62 | */ |
||
| 63 | protected $pluginApiService; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * PluginController constructor. |
||
| 67 | * |
||
| 68 | * @param PluginRepository $pluginRepository |
||
| 69 | * @param PluginService $pluginService |
||
| 70 | * @param BaseInfoRepository $baseInfoRepository |
||
| 71 | * @param PluginApiService $pluginApiService |
||
| 72 | * |
||
| 73 | * @throws \Doctrine\ORM\NoResultException |
||
| 74 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 75 | */ |
||
| 76 | public function __construct(PluginRepository $pluginRepository, PluginService $pluginService, BaseInfoRepository $baseInfoRepository, PluginApiService $pluginApiService) |
||
| 77 | { |
||
| 78 | $this->pluginRepository = $pluginRepository; |
||
| 79 | $this->pluginService = $pluginService; |
||
| 80 | $this->BaseInfo = $baseInfoRepository->get(); |
||
| 81 | $this->pluginApiService = $pluginApiService; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * インストール済プラグイン画面 |
||
| 86 | * |
||
| 87 | * @Route("/%eccube_admin_route%/store/plugin", name="admin_store_plugin") |
||
| 88 | * @Template("@admin/Store/plugin.twig") |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | * |
||
| 92 | * @throws PluginException |
||
| 93 | */ |
||
| 94 | public function index() |
||
| 95 | { |
||
| 96 | $pluginForms = []; |
||
| 97 | $configPages = []; |
||
| 98 | $Plugins = $this->pluginRepository->findBy([], ['code' => 'ASC']); |
||
| 99 | |||
| 100 | // ファイル設置プラグインの取得. |
||
| 101 | $unregisteredPlugins = $this->getUnregisteredPlugins($Plugins); |
||
| 102 | $unregisteredPluginsConfigPages = []; |
||
| 103 | foreach ($unregisteredPlugins as $unregisteredPlugin) { |
||
| 104 | try { |
||
| 105 | $code = $unregisteredPlugin['code']; |
||
| 106 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
| 107 | $unregisteredPluginsConfigPages[$code] = $this->generateUrl('plugin_'.$code.'_config'); |
||
| 108 | } catch (RouteNotFoundException $e) { |
||
| 109 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | $officialPlugins = []; |
||
| 114 | $unofficialPlugins = []; |
||
| 115 | |||
| 116 | foreach ($Plugins as $Plugin) { |
||
| 117 | $form = $this->formFactory |
||
| 118 | ->createNamedBuilder( |
||
| 119 | 'form'.$Plugin->getId(), |
||
| 120 | PluginManagementType::class, |
||
| 121 | null, |
||
| 122 | [ |
||
| 123 | 'plugin_id' => $Plugin->getId(), |
||
| 124 | ] |
||
| 125 | ) |
||
| 126 | ->getForm(); |
||
| 127 | $pluginForms[$Plugin->getId()] = $form->createView(); |
||
| 128 | |||
| 129 | try { |
||
| 130 | // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか) |
||
| 131 | $configPages[$Plugin->getCode()] = $this->generateUrl(Container::underscore($Plugin->getCode()).'_admin_config'); |
||
| 132 | } catch (\Exception $e) { |
||
| 133 | // プラグインで設定画面のルートが定義されていない場合は無視 |
||
| 134 | } |
||
| 135 | if ($Plugin->getSource() == 0) { |
||
| 136 | // 商品IDが設定されていない場合、非公式プラグイン |
||
| 137 | $unofficialPlugins[] = $Plugin; |
||
| 138 | } else { |
||
| 139 | $officialPlugins[$Plugin->getSource()] = $Plugin; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | // オーナーズストア通信 |
||
| 144 | $officialPluginsDetail = []; |
||
| 145 | try { |
||
| 146 | $data = $this->pluginApiService->getPurchased(); |
||
| 147 | foreach ($data as $item) { |
||
| 148 | if (isset($officialPlugins[$item['id']]) === false) { |
||
| 149 | $Plugin = new Plugin(); |
||
| 150 | $Plugin->setName($item['name']); |
||
| 151 | $Plugin->setCode($item['code']); |
||
| 152 | $Plugin->setVersion($item['version']); |
||
| 153 | $Plugin->setSource($item['id']); |
||
| 154 | $Plugin->setEnabled(false); |
||
| 155 | $officialPlugins[$item['id']] = $Plugin; |
||
| 156 | } |
||
| 157 | $officialPluginsDetail[$item['id']] = $item; |
||
| 158 | } |
||
| 159 | } catch (PluginApiException $e) { |
||
| 160 | $this->addWarning($e->getMessage(), 'admin'); |
||
| 161 | } |
||
| 162 | |||
| 163 | return [ |
||
| 164 | 'plugin_forms' => $pluginForms, |
||
| 165 | 'officialPlugins' => $officialPlugins, |
||
| 166 | 'unofficialPlugins' => $unofficialPlugins, |
||
| 167 | 'configPages' => $configPages, |
||
| 168 | 'unregisteredPlugins' => $unregisteredPlugins, |
||
| 169 | 'unregisteredPluginsConfigPages' => $unregisteredPluginsConfigPages, |
||
| 170 | 'officialPluginsDetail' => $officialPluginsDetail, |
||
| 171 | ]; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * インストール済プラグインからのアップデート |
||
| 176 | * |
||
| 177 | * @Route("/%eccube_admin_route%/store/plugin/{id}/update", requirements={"id" = "\d+"}, name="admin_store_plugin_update", methods={"POST"}) |
||
| 178 | * |
||
| 179 | * @param Request $request |
||
| 180 | * @param Plugin $Plugin |
||
| 181 | * |
||
| 182 | * @return RedirectResponse |
||
| 183 | */ |
||
| 184 | public function update(Request $request, Plugin $Plugin) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * 対象のプラグインを有効にします。 |
||
| 241 | * |
||
| 242 | * @Route("/%eccube_admin_route%/store/plugin/{id}/enable", requirements={"id" = "\d+"}, name="admin_store_plugin_enable", methods={"POST"}) |
||
| 243 | * |
||
| 244 | * @param Plugin $Plugin |
||
| 245 | * |
||
| 246 | * @return RedirectResponse|JsonResponse |
||
| 247 | * |
||
| 248 | * @throws PluginException |
||
| 249 | */ |
||
| 250 | public function enable(Plugin $Plugin, CacheUtil $cacheUtil, Request $request) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * 対象のプラグインを無効にします。 |
||
| 313 | * |
||
| 314 | * @Route("/%eccube_admin_route%/store/plugin/{id}/disable", requirements={"id" = "\d+"}, name="admin_store_plugin_disable", methods={"POST"}) |
||
| 315 | * |
||
| 316 | * @param Request $request |
||
| 317 | * @param Plugin $Plugin |
||
| 318 | * @param CacheUtil $cacheUtil |
||
| 319 | * |
||
| 320 | * @return \Symfony\Component\HttpFoundation\JsonResponse|RedirectResponse |
||
| 321 | */ |
||
| 322 | public function disable(Request $request, Plugin $Plugin, CacheUtil $cacheUtil) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * 対象のプラグインを削除します。 |
||
| 373 | * |
||
| 374 | * @Route("/%eccube_admin_route%/store/plugin/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_uninstall", methods={"DELETE"}) |
||
| 375 | * |
||
| 376 | * @param Plugin $Plugin |
||
| 377 | * |
||
| 378 | * @return RedirectResponse |
||
| 379 | * |
||
| 380 | * @throws \Exception |
||
| 381 | */ |
||
| 382 | public function uninstall(Plugin $Plugin) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * プラグインファイルアップロード画面 |
||
| 415 | * |
||
| 416 | * @Route("/%eccube_admin_route%/store/plugin/install", name="admin_store_plugin_install") |
||
| 417 | * @Template("@admin/Store/plugin_install.twig") |
||
| 418 | * |
||
| 419 | * @param Request $request |
||
| 420 | * |
||
| 421 | * @return array|RedirectResponse |
||
| 422 | */ |
||
| 423 | public function install(Request $request) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * 認証キー設定画面 |
||
| 478 | * |
||
| 479 | * @Route("/%eccube_admin_route%/store/plugin/authentication_setting", name="admin_store_authentication_setting") |
||
| 480 | * @Template("@admin/Store/authentication_setting.twig") |
||
| 481 | */ |
||
| 482 | public function authenticationSetting(Request $request) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * フォルダ設置のみのプラグインを取得する. |
||
| 508 | * |
||
| 509 | * @param array $plugins |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | * |
||
| 513 | * @throws PluginException |
||
| 514 | */ |
||
| 515 | protected function getUnregisteredPlugins(array $plugins) |
||
| 551 | } |
||
| 552 |
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: