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 PluginService 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 PluginService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class PluginService |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var EccubeConfig |
||
| 34 | */ |
||
| 35 | protected $eccubeConfig; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var EntityManager |
||
| 39 | */ |
||
| 40 | protected $entityManager; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var PluginRepository |
||
| 44 | */ |
||
| 45 | protected $pluginRepository; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var EntityProxyService |
||
| 49 | */ |
||
| 50 | protected $entityProxyService; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var SchemaService |
||
| 54 | */ |
||
| 55 | protected $schemaService; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ComposerServiceInterface |
||
| 59 | */ |
||
| 60 | protected $composerService; |
||
| 61 | |||
| 62 | const VENDOR_NAME = 'ec-cube'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Plugin type/library of ec-cube |
||
| 66 | */ |
||
| 67 | const ECCUBE_LIBRARY = 1; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Plugin type/library of other (except ec-cube) |
||
| 71 | */ |
||
| 72 | const OTHER_LIBRARY = 2; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string %kernel.project_dir% |
||
| 76 | */ |
||
| 77 | private $projectRoot; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string %kernel.environment% |
||
| 81 | */ |
||
| 82 | private $environment; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var ContainerInterface |
||
| 86 | */ |
||
| 87 | protected $container; |
||
| 88 | |||
| 89 | /** @var CacheUtil */ |
||
| 90 | protected $cacheUtil; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var PluginApiService |
||
| 94 | */ |
||
| 95 | private $pluginApiService; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * PluginService constructor. |
||
| 99 | * |
||
| 100 | * @param EntityManagerInterface $entityManager |
||
| 101 | * @param PluginRepository $pluginRepository |
||
| 102 | * @param EntityProxyService $entityProxyService |
||
| 103 | * @param SchemaService $schemaService |
||
| 104 | * @param EccubeConfig $eccubeConfig |
||
| 105 | * @param ContainerInterface $container |
||
| 106 | * @param CacheUtil $cacheUtil |
||
| 107 | * @param ComposerServiceInterface $composerService |
||
| 108 | * @param PluginApiService $pluginApiService |
||
| 109 | */ |
||
| 110 | public function __construct( |
||
| 133 | |||
| 134 | /** |
||
| 135 | * ファイル指定してのプラグインインストール |
||
| 136 | * |
||
| 137 | * @param string $path path to tar.gz/zip plugin file |
||
| 138 | * @param int $source |
||
| 139 | * |
||
| 140 | * @return boolean |
||
| 141 | * |
||
| 142 | * @throws PluginException |
||
| 143 | * @throws \Exception |
||
| 144 | */ |
||
| 145 | public function install($path, $source = 0) |
||
| 146 | { |
||
| 147 | $pluginBaseDir = null; |
||
| 148 | $tmp = null; |
||
| 149 | try { |
||
| 150 | // プラグイン配置前に実施する処理 |
||
| 151 | $this->preInstall(); |
||
| 152 | $tmp = $this->createTempDir(); |
||
| 153 | |||
| 154 | // 一旦テンポラリに展開 |
||
| 155 | $this->unpackPluginArchive($path, $tmp); |
||
| 156 | $this->checkPluginArchiveContent($tmp); |
||
| 157 | |||
| 158 | $config = $this->readConfig($tmp); |
||
| 159 | // テンポラリのファイルを削除 |
||
| 160 | $this->deleteFile($tmp); |
||
| 161 | |||
| 162 | // 重複していないかチェック |
||
| 163 | $this->checkSamePlugin($config['code']); |
||
| 164 | |||
| 165 | $pluginBaseDir = $this->calcPluginDir($config['code']); |
||
| 166 | // 本来の置き場所を作成 |
||
| 167 | $this->createPluginDir($pluginBaseDir); |
||
| 168 | |||
| 169 | // 問題なければ本当のplugindirへ |
||
| 170 | $this->unpackPluginArchive($path, $pluginBaseDir); |
||
| 171 | |||
| 172 | // Check dependent plugin |
||
| 173 | // Don't install ec-cube library |
||
| 174 | // $dependents = $this->getDependentByCode($config['code'], self::OTHER_LIBRARY); |
||
| 175 | // if (!empty($dependents)) { |
||
| 176 | // $package = $this->parseToComposerCommand($dependents); |
||
| 177 | //FIXME: how to working with ComposerProcessService or ComposerApiService ? |
||
| 178 | // $this->composerService->execRequire($package); |
||
| 179 | // } |
||
| 180 | // リソースファイルをコピー |
||
| 181 | $this->copyAssets($config['code']); |
||
| 182 | // プラグイン配置後に実施する処理 |
||
| 183 | $this->postInstall($config, $source); |
||
| 184 | } catch (PluginException $e) { |
||
| 185 | $this->deleteDirs([$tmp, $pluginBaseDir]); |
||
| 186 | throw $e; |
||
| 187 | } catch (\Exception $e) { |
||
| 188 | // インストーラがどんなExceptionを上げるかわからないので |
||
| 189 | $this->deleteDirs([$tmp, $pluginBaseDir]); |
||
| 190 | throw $e; |
||
| 191 | } |
||
| 192 | |||
| 193 | return true; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param $code string sプラグインコード |
||
| 198 | * |
||
| 199 | * @throws PluginException |
||
| 200 | */ |
||
| 201 | public function installWithCode($code) |
||
| 202 | { |
||
| 203 | $pluginDir = $this->calcPluginDir($code); |
||
| 204 | $this->checkPluginArchiveContent($pluginDir); |
||
| 205 | $config = $this->readConfig($pluginDir); |
||
| 206 | |||
| 207 | if (isset($config['source']) && $config['source']) { |
||
| 208 | // 依存プラグインが有効になっていない場合はエラー |
||
| 209 | $requires = $this->getPluginRequired($config); |
||
| 210 | View Code Duplication | $notInstalledOrDisabled = array_filter($requires, function ($req) { |
|
| 211 | $code = preg_replace('/^ec-cube\//', '', $req['name']); |
||
| 212 | /** @var Plugin $DependPlugin */ |
||
| 213 | $DependPlugin = $this->pluginRepository->findOneBy(['code' => $code]); |
||
| 214 | |||
| 215 | return $DependPlugin ? $DependPlugin->isEnabled() == false : true; |
||
| 216 | }); |
||
| 217 | |||
| 218 | if (!empty($notInstalledOrDisabled)) { |
||
| 219 | $names = array_map(function ($p) { return $p['name']; }, $notInstalledOrDisabled); |
||
| 220 | throw new PluginException(implode(', ', $names).'を有効化してください。'); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | $this->checkSamePlugin($config['code']); |
||
| 225 | $this->copyAssets($config['code']); |
||
| 226 | $this->postInstall($config, $config['source']); |
||
| 227 | } |
||
| 228 | |||
| 229 | // インストール事前処理 |
||
| 230 | public function preInstall() |
||
| 236 | |||
| 237 | // インストール事後処理 |
||
| 238 | public function postInstall($config, $source) |
||
| 274 | |||
| 275 | public function generateProxyAndUpdateSchema(Plugin $plugin, $config) |
||
| 309 | |||
| 310 | public function createTempDir() |
||
| 322 | |||
| 323 | public function deleteDirs($arr) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $archive |
||
| 335 | * @param string $dir |
||
| 336 | * |
||
| 337 | * @throws PluginException |
||
| 338 | */ |
||
| 339 | public function unpackPluginArchive($archive, $dir) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param $dir |
||
| 359 | * @param array $config_cache |
||
| 360 | * |
||
| 361 | * @throws PluginException |
||
| 362 | */ |
||
| 363 | public function checkPluginArchiveContent($dir, array $config_cache = []) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param $pluginDir |
||
| 393 | * |
||
| 394 | * @return array |
||
| 395 | * |
||
| 396 | * @throws PluginException |
||
| 397 | */ |
||
| 398 | public function readConfig($pluginDir) |
||
| 425 | |||
| 426 | public function checkSymbolName($string) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $path |
||
| 436 | */ |
||
| 437 | public function deleteFile($path) |
||
| 442 | |||
| 443 | public function checkSamePlugin($code) |
||
| 451 | |||
| 452 | public function calcPluginDir($code) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param string $d |
||
| 459 | * |
||
| 460 | * @throws PluginException |
||
| 461 | */ |
||
| 462 | public function createPluginDir($d) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param $meta |
||
| 472 | * @param int $source |
||
| 473 | * |
||
| 474 | * @return Plugin |
||
| 475 | * |
||
| 476 | * @throws PluginException |
||
| 477 | */ |
||
| 478 | public function registerPlugin($meta, $source = 0) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param $meta |
||
| 502 | * @param string $method |
||
| 503 | */ |
||
| 504 | public function callPluginManagerMethod($meta, $method) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param Plugin $plugin |
||
| 517 | * @param bool $force |
||
| 518 | * |
||
| 519 | * @return bool |
||
| 520 | * |
||
| 521 | * @throws \Exception |
||
| 522 | */ |
||
| 523 | public function uninstall(Plugin $plugin, $force = true) |
||
| 556 | |||
| 557 | public function unregisterPlugin(Plugin $p) |
||
| 567 | |||
| 568 | public function disable(Plugin $plugin) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Proxyを再生成します. |
||
| 575 | * |
||
| 576 | * @param Plugin $plugin プラグイン |
||
| 577 | * @param boolean $temporary プラグインが無効状態でも一時的に生成するかどうか |
||
| 578 | * @param string|null $outputDir 出力先 |
||
| 579 | * |
||
| 580 | * @return array 生成されたファイルのパス |
||
| 581 | */ |
||
| 582 | private function regenerateProxy(Plugin $plugin, $temporary, $outputDir = null) |
||
| 583 | { |
||
| 584 | if (is_null($outputDir)) { |
||
| 585 | $outputDir = $this->projectRoot.'/app/proxy/entity'; |
||
| 586 | } |
||
| 587 | @mkdir($outputDir); |
||
| 588 | |||
| 589 | $enabledPluginCodes = array_map( |
||
| 590 | function ($p) { return $p->getCode(); }, |
||
| 591 | $temporary ? $this->pluginRepository->findAll() : $this->pluginRepository->findAllEnabled() |
||
| 592 | ); |
||
| 593 | |||
| 594 | $excludes = []; |
||
| 595 | if ($temporary || $plugin->isEnabled()) { |
||
| 596 | $enabledPluginCodes[] = $plugin->getCode(); |
||
| 597 | } else { |
||
| 598 | $index = array_search($plugin->getCode(), $enabledPluginCodes); |
||
| 599 | if ($index >= 0) { |
||
| 600 | array_splice($enabledPluginCodes, $index, 1); |
||
| 601 | $excludes = [$this->projectRoot.'/app/Plugin/'.$plugin->getCode().'/Entity']; |
||
| 602 | } |
||
| 603 | } |
||
| 604 | |||
| 605 | $enabledPluginEntityDirs = array_map(function ($code) { |
||
| 606 | return $this->projectRoot."/app/Plugin/${code}/Entity"; |
||
| 607 | }, $enabledPluginCodes); |
||
| 608 | |||
| 609 | return $this->entityProxyService->generate( |
||
| 610 | array_merge([$this->projectRoot.'/app/Customize/Entity'], $enabledPluginEntityDirs), |
||
| 611 | $excludes, |
||
| 612 | $outputDir |
||
| 613 | ); |
||
| 614 | } |
||
| 615 | |||
| 616 | public function enable(Plugin $plugin, $enable = true) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Update plugin |
||
| 649 | * |
||
| 650 | * @param Plugin $plugin |
||
| 651 | * @param string $path |
||
| 652 | * |
||
| 653 | * @return bool |
||
| 654 | * |
||
| 655 | * @throws PluginException |
||
| 656 | * @throws \Exception |
||
| 657 | */ |
||
| 658 | public function update(Plugin $plugin, $path) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Update plugin |
||
| 704 | * |
||
| 705 | * @param Plugin $plugin |
||
| 706 | * @param array $meta Config data |
||
| 707 | * |
||
| 708 | * @throws \Exception |
||
| 709 | */ |
||
| 710 | public function updatePlugin(Plugin $plugin, $meta) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Get array require by plugin |
||
| 731 | * Todo: need define dependency plugin mechanism |
||
| 732 | * |
||
| 733 | * @param array|Plugin $plugin format as plugin from api |
||
| 734 | * |
||
| 735 | * @return array|mixed |
||
| 736 | * |
||
| 737 | * @throws PluginException |
||
| 738 | */ |
||
| 739 | public function getPluginRequired($plugin) |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Find the dependent plugins that need to be disabled |
||
| 755 | * |
||
| 756 | * @param string $pluginCode |
||
| 757 | * |
||
| 758 | * @return array plugin code |
||
| 759 | */ |
||
| 760 | public function findDependentPluginNeedDisable($pluginCode) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Find the other plugin that has requires on it. |
||
| 767 | * Check in both dtb_plugin table and <PluginCode>/composer.json |
||
| 768 | * |
||
| 769 | * @param string $pluginCode |
||
| 770 | * @param bool $enableOnly |
||
| 771 | * |
||
| 772 | * @return array plugin code |
||
| 773 | */ |
||
| 774 | public function findDependentPlugin($pluginCode, $enableOnly = false) |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Get dependent plugin by code |
||
| 809 | * It's base on composer.json |
||
| 810 | * Return the plugin code and version in the format of the composer |
||
| 811 | * |
||
| 812 | * @param string $pluginCode |
||
| 813 | * @param int|null $libraryType |
||
| 814 | * self::ECCUBE_LIBRARY only return library/plugin of eccube |
||
| 815 | * self::OTHER_LIBRARY only return library/plugin of 3rd part ex: symfony, composer, ... |
||
| 816 | * default : return all library/plugin |
||
| 817 | * |
||
| 818 | * @return array format [packageName1 => version1, packageName2 => version2] |
||
| 819 | */ |
||
| 820 | public function getDependentByCode($pluginCode, $libraryType = null) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Format array dependent plugin to string |
||
| 852 | * It is used for commands. |
||
| 853 | * |
||
| 854 | * @param array $packages format [packageName1 => version1, packageName2 => version2] |
||
| 855 | * @param bool $getVersion |
||
| 856 | * |
||
| 857 | * @return string format if version=true: "packageName1:version1 packageName2:version2", if version=false: "packageName1 packageName2" |
||
| 858 | */ |
||
| 859 | public function parseToComposerCommand(array $packages, $getVersion = true) |
||
| 870 | |||
| 871 | /** |
||
| 872 | * リソースファイル等をコピー |
||
| 873 | * コピー元となるファイルの置き場所は固定であり、 |
||
| 874 | * [プラグインコード]/Resource/assets |
||
| 875 | * 配下に置かれているファイルが所定の位置へコピーされる |
||
| 876 | * |
||
| 877 | * @param $pluginCode |
||
| 878 | */ |
||
| 879 | public function copyAssets($pluginCode) |
||
| 889 | |||
| 890 | /** |
||
| 891 | * コピーしたリソースファイル等を削除 |
||
| 892 | * |
||
| 893 | * @param string $pluginCode |
||
| 894 | */ |
||
| 895 | public function removeAssets($pluginCode) |
||
| 905 | |||
| 906 | /** |
||
| 907 | * Plugin is exist check |
||
| 908 | * |
||
| 909 | * @param array $plugins get from api |
||
| 910 | * @param string $pluginCode |
||
| 911 | * |
||
| 912 | * @return false|int|string |
||
| 913 | */ |
||
| 914 | public function checkPluginExist($plugins, $pluginCode) |
||
| 924 | |||
| 925 | /** |
||
| 926 | * @param string $code |
||
| 927 | * |
||
| 928 | * @return bool |
||
| 929 | */ |
||
| 930 | private function isEnable($code) |
||
| 942 | } |
||
| 943 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.