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) |
||
| 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 | $Plugin = null; |
||
| 149 | |||
| 150 | if (isset($officialPlugins[$item['id']])) { |
||
| 151 | $Plugin = $officialPlugins[$item['id']]; |
||
|
|
|||
| 152 | } else { |
||
| 153 | $Plugin = new Plugin(); |
||
| 154 | $Plugin->setName($item['name']); |
||
| 155 | $Plugin->setCode($item['code']); |
||
| 156 | $Plugin->setVersion($item['version']); |
||
| 157 | $Plugin->setSource($item['id']); |
||
| 158 | $Plugin->setEnabled(false); |
||
| 159 | $officialPlugins[$item['id']] = $Plugin; |
||
| 160 | } |
||
| 161 | |||
| 162 | $officialPluginsDetail[$item['id']] = $item; |
||
| 163 | } |
||
| 164 | } catch (PluginApiException $e) { |
||
| 165 | $this->addWarning($e->getMessage(), 'admin'); |
||
| 166 | } |
||
| 167 | |||
| 168 | return [ |
||
| 169 | 'plugin_forms' => $pluginForms, |
||
| 170 | 'officialPlugins' => $officialPlugins, |
||
| 171 | 'unofficialPlugins' => $unofficialPlugins, |
||
| 172 | 'configPages' => $configPages, |
||
| 173 | 'unregisteredPlugins' => $unregisteredPlugins, |
||
| 174 | 'unregisteredPluginsConfigPages' => $unregisteredPluginsConfigPages, |
||
| 175 | 'officialPluginsDetail' => $officialPluginsDetail, |
||
| 176 | ]; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * インストール済プラグインからのアップデート |
||
| 181 | * |
||
| 182 | * @Route("/%eccube_admin_route%/store/plugin/{id}/update", requirements={"id" = "\d+"}, name="admin_store_plugin_update", methods={"POST"}) |
||
| 183 | * |
||
| 184 | * @param Request $request |
||
| 185 | * @param Plugin $Plugin |
||
| 186 | * |
||
| 187 | * @return RedirectResponse |
||
| 188 | */ |
||
| 189 | public function update(Request $request, Plugin $Plugin) |
||
| 190 | { |
||
| 191 | $form = $this->formFactory |
||
| 192 | ->createNamedBuilder( |
||
| 193 | 'form'.$Plugin->getId(), |
||
| 194 | PluginManagementType::class, |
||
| 195 | null, |
||
| 196 | [ |
||
| 197 | 'plugin_id' => null, // placeHolder |
||
| 198 | ] |
||
| 199 | ) |
||
| 200 | ->getForm(); |
||
| 201 | |||
| 202 | $message = ''; |
||
| 203 | $form->handleRequest($request); |
||
| 204 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 205 | $tmpDir = null; |
||
| 206 | try { |
||
| 207 | $formFile = $form['plugin_archive']->getData(); |
||
| 208 | $tmpDir = $this->pluginService->createTempDir(); |
||
| 209 | $tmpFile = sha1(StringUtil::random(32)).'.'.$formFile->getClientOriginalExtension(); |
||
| 210 | $formFile->move($tmpDir, $tmpFile); |
||
| 211 | $this->pluginService->update($Plugin, $tmpDir.'/'.$tmpFile); |
||
| 212 | $fs = new Filesystem(); |
||
| 213 | $fs->remove($tmpDir); |
||
| 214 | $this->addSuccess('admin.plugin.update.complete', 'admin'); |
||
| 215 | |||
| 216 | return $this->redirectToRoute('admin_store_plugin'); |
||
| 217 | } catch (PluginException $e) { |
||
| 218 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
| 219 | $fs = new Filesystem(); |
||
| 220 | $fs->remove($tmpDir); |
||
| 221 | } |
||
| 222 | $message = $e->getMessage(); |
||
| 223 | } catch (\Exception $er) { |
||
| 224 | // Catch composer install error | Other error |
||
| 225 | if (!empty($tmpDir) && file_exists($tmpDir)) { |
||
| 226 | $fs = new Filesystem(); |
||
| 227 | $fs->remove($tmpDir); |
||
| 228 | } |
||
| 229 | log_error('plugin install failed.', ['original-message' => $er->getMessage()]); |
||
| 230 | $message = 'admin.plugin.install.fail'; |
||
| 231 | } |
||
| 232 | } else { |
||
| 233 | $errors = $form->getErrors(true); |
||
| 234 | foreach ($errors as $error) { |
||
| 235 | $message = $error->getMessage(); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | $this->addError($message, 'admin'); |
||
| 240 | |||
| 241 | return $this->redirectToRoute('admin_store_plugin'); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * 対象のプラグインを有効にします。 |
||
| 246 | * |
||
| 247 | * @Route("/%eccube_admin_route%/store/plugin/{id}/enable", requirements={"id" = "\d+"}, name="admin_store_plugin_enable", methods={"POST"}) |
||
| 248 | * |
||
| 249 | * @param Plugin $Plugin |
||
| 250 | * |
||
| 251 | * @return RedirectResponse|JsonResponse |
||
| 252 | * |
||
| 253 | * @throws PluginException |
||
| 254 | */ |
||
| 255 | public function enable(Plugin $Plugin, CacheUtil $cacheUtil, Request $request) |
||
| 256 | { |
||
| 257 | $this->isTokenValid(); |
||
| 258 | |||
| 259 | $log = null; |
||
| 260 | |||
| 261 | if ($Plugin->isEnabled()) { |
||
| 262 | if ($request->isXmlHttpRequest()) { |
||
| 263 | return $this->json(['success' => true]); |
||
| 264 | } else { |
||
| 265 | $this->addError('admin.plugin.already.enable', 'admin'); |
||
| 266 | } |
||
| 267 | } else { |
||
| 268 | // ストアからインストールしたプラグインは依存プラグインが有効化されているかを確認 |
||
| 269 | if ($Plugin->getSource()) { |
||
| 270 | $requires = $this->pluginService->getPluginRequired($Plugin); |
||
| 271 | View Code Duplication | $requires = array_filter($requires, function ($req) { |
|
| 272 | $code = preg_replace('/^ec-cube\//', '', $req['name']); |
||
| 273 | /** @var Plugin $DependPlugin */ |
||
| 274 | $DependPlugin = $this->pluginRepository->findOneBy(['code' => $code]); |
||
| 275 | |||
| 276 | return $DependPlugin->isEnabled() == false; |
||
| 277 | }); |
||
| 278 | if (!empty($requires)) { |
||
| 279 | $names = array_map(function ($req) { |
||
| 280 | return "「${req['description']}」"; |
||
| 281 | }, $requires); |
||
| 282 | $message = trans('%depend_name%を先に有効化してください。', ['%name%' => $Plugin->getName(), '%depend_name%' => implode(', ', $names)]); |
||
| 283 | |||
| 284 | if ($request->isXmlHttpRequest()) { |
||
| 285 | return $this->json(['success' => false, 'message' => $message], 400); |
||
| 286 | } else { |
||
| 287 | $this->addError($message, 'admin'); |
||
| 288 | |||
| 289 | return $this->redirectToRoute('admin_store_plugin'); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | ob_start(); |
||
| 295 | |||
| 296 | if (!$Plugin->isInitialized()) { |
||
| 297 | $this->pluginService->installWithCode($Plugin->getCode()); |
||
| 298 | } |
||
| 299 | |||
| 300 | $this->pluginService->enable($Plugin); |
||
| 301 | $log = ob_get_clean(); |
||
| 302 | ob_end_flush(); |
||
| 303 | } |
||
| 304 | |||
| 305 | $cacheUtil->clearCache(); |
||
| 306 | |||
| 307 | View Code Duplication | if ($request->isXmlHttpRequest()) { |
|
| 308 | return $this->json(['success' => true, 'log' => $log]); |
||
| 309 | } else { |
||
| 310 | $this->addSuccess(trans('「%plugin_name%」を有効にしました。', ['%plugin_name%' => $Plugin->getName()]), 'admin'); |
||
| 311 | |||
| 312 | return $this->redirectToRoute('admin_store_plugin'); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * 対象のプラグインを無効にします。 |
||
| 318 | * |
||
| 319 | * @Route("/%eccube_admin_route%/store/plugin/{id}/disable", requirements={"id" = "\d+"}, name="admin_store_plugin_disable", methods={"POST"}) |
||
| 320 | * |
||
| 321 | * @param Request $request |
||
| 322 | * @param Plugin $Plugin |
||
| 323 | * @param CacheUtil $cacheUtil |
||
| 324 | * |
||
| 325 | * @return \Symfony\Component\HttpFoundation\JsonResponse|RedirectResponse |
||
| 326 | */ |
||
| 327 | public function disable(Request $request, Plugin $Plugin, CacheUtil $cacheUtil) |
||
| 328 | { |
||
| 329 | $this->isTokenValid(); |
||
| 330 | |||
| 331 | $log = null; |
||
| 332 | if ($Plugin->isEnabled()) { |
||
| 333 | $dependents = $this->pluginService->findDependentPluginNeedDisable($Plugin->getCode()); |
||
| 334 | if (!empty($dependents)) { |
||
| 335 | $dependName = $dependents[0]; |
||
| 336 | $DependPlugin = $this->pluginRepository->findOneBy(['code' => $dependents[0]]); |
||
| 337 | if ($DependPlugin) { |
||
| 338 | $dependName = $DependPlugin->getName(); |
||
| 339 | } |
||
| 340 | $message = trans('admin.plugin.disable.depend', ['%name%' => $Plugin->getName(), '%depend_name%' => $dependName]); |
||
| 341 | |||
| 342 | if ($request->isXmlHttpRequest()) { |
||
| 343 | return $this->json(['message' => $message], 400); |
||
| 344 | } else { |
||
| 345 | $this->addError($message, 'admin'); |
||
| 346 | |||
| 347 | return $this->redirectToRoute('admin_store_plugin'); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | ob_start(); |
||
| 352 | $this->pluginService->disable($Plugin); |
||
| 353 | $log = ob_get_clean(); |
||
| 354 | ob_end_flush(); |
||
| 355 | View Code Duplication | } else { |
|
| 356 | if ($request->isXmlHttpRequest()) { |
||
| 357 | return $this->json(['success' => true, 'log' => $log]); |
||
| 358 | } else { |
||
| 359 | $this->addError('admin.plugin.already.disable', 'admin'); |
||
| 360 | |||
| 361 | return $this->redirectToRoute('admin_store_plugin'); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | $cacheUtil->clearCache(); |
||
| 366 | |||
| 367 | View Code Duplication | if ($request->isXmlHttpRequest()) { |
|
| 368 | return $this->json(['success' => true, 'log' => $log]); |
||
| 369 | } else { |
||
| 370 | $this->addSuccess('admin.plugin.disable.complete', 'admin'); |
||
| 371 | |||
| 372 | return $this->redirectToRoute('admin_store_plugin'); |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * 対象のプラグインを削除します。 |
||
| 378 | * |
||
| 379 | * @Route("/%eccube_admin_route%/store/plugin/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_uninstall", methods={"DELETE"}) |
||
| 380 | * |
||
| 381 | * @param Plugin $Plugin |
||
| 382 | * |
||
| 383 | * @return RedirectResponse |
||
| 384 | * |
||
| 385 | * @throws \Exception |
||
| 386 | */ |
||
| 387 | public function uninstall(Plugin $Plugin) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * プラグインファイルアップロード画面 |
||
| 420 | * |
||
| 421 | * @Route("/%eccube_admin_route%/store/plugin/install", name="admin_store_plugin_install") |
||
| 422 | * @Template("@admin/Store/plugin_install.twig") |
||
| 423 | * |
||
| 424 | * @param Request $request |
||
| 425 | * |
||
| 426 | * @return array|RedirectResponse |
||
| 427 | */ |
||
| 428 | public function install(Request $request) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * 認証キー設定画面 |
||
| 483 | * |
||
| 484 | * @Route("/%eccube_admin_route%/store/plugin/authentication_setting", name="admin_store_authentication_setting") |
||
| 485 | * @Template("@admin/Store/authentication_setting.twig") |
||
| 486 | */ |
||
| 487 | public function authenticationSetting(Request $request) |
||
| 488 | { |
||
| 489 | $builder = $this->formFactory |
||
| 490 | ->createBuilder(AuthenticationType::class, $this->BaseInfo); |
||
| 491 | |||
| 492 | $form = $builder->getForm(); |
||
| 493 | $form->handleRequest($request); |
||
| 494 | |||
| 495 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 496 | // 認証キーの登録 and PHP path |
||
| 497 | $this->BaseInfo = $form->getData(); |
||
| 498 | $this->entityManager->persist($this->BaseInfo); |
||
| 499 | $this->entityManager->flush(); |
||
| 500 | |||
| 501 | $this->addSuccess('admin.common.save_complete', 'admin'); |
||
| 502 | } |
||
| 503 | |||
| 504 | return [ |
||
| 505 | 'form' => $form->createView(), |
||
| 506 | 'eccubeUrl' => $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL), |
||
| 507 | 'eccubeShopName' => $this->BaseInfo->getShopName(), |
||
| 508 | ]; |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * フォルダ設置のみのプラグインを取得する. |
||
| 513 | * |
||
| 514 | * @param array $plugins |
||
| 515 | * |
||
| 516 | * @return array |
||
| 517 | * |
||
| 518 | * @throws PluginException |
||
| 519 | */ |
||
| 520 | protected function getUnregisteredPlugins(array $plugins) |
||
| 556 | } |
||
| 557 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.