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 |
||
| 31 | class PluginService |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var EccubeConfig |
||
| 35 | */ |
||
| 36 | protected $eccubeConfig; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var EntityManager |
||
| 40 | */ |
||
| 41 | protected $entityManager; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var PluginRepository |
||
| 45 | */ |
||
| 46 | protected $pluginRepository; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var EntityProxyService |
||
| 50 | */ |
||
| 51 | protected $entityProxyService; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var SchemaService |
||
| 55 | */ |
||
| 56 | protected $schemaService; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var ComposerServiceInterface |
||
| 60 | */ |
||
| 61 | protected $composerService; |
||
| 62 | |||
| 63 | const VENDOR_NAME = 'ec-cube'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Plugin type/library of ec-cube |
||
| 67 | */ |
||
| 68 | const ECCUBE_LIBRARY = 1; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Plugin type/library of other (except ec-cube) |
||
| 72 | */ |
||
| 73 | const OTHER_LIBRARY = 2; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string %kernel.project_dir% |
||
| 77 | */ |
||
| 78 | private $projectRoot; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string %kernel.environment% |
||
| 82 | */ |
||
| 83 | private $environment; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var ContainerInterface |
||
| 87 | */ |
||
| 88 | protected $container; |
||
| 89 | |||
| 90 | /** @var CacheUtil */ |
||
| 91 | protected $cacheUtil; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var PluginApiService |
||
| 95 | */ |
||
| 96 | private $pluginApiService; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var SystemService |
||
| 100 | */ |
||
| 101 | private $systemService; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * PluginService constructor. |
||
| 105 | * |
||
| 106 | * @param EntityManagerInterface $entityManager |
||
| 107 | * @param PluginRepository $pluginRepository |
||
| 108 | * @param EntityProxyService $entityProxyService |
||
| 109 | * @param SchemaService $schemaService |
||
| 110 | * @param EccubeConfig $eccubeConfig |
||
| 111 | * @param ContainerInterface $container |
||
| 112 | * @param CacheUtil $cacheUtil |
||
| 113 | * @param ComposerServiceInterface $composerService |
||
| 114 | * @param PluginApiService $pluginApiService |
||
| 115 | */ |
||
| 116 | public function __construct( |
||
| 117 | EntityManagerInterface $entityManager, |
||
| 118 | PluginRepository $pluginRepository, |
||
| 119 | EntityProxyService $entityProxyService, |
||
| 120 | SchemaService $schemaService, |
||
| 121 | EccubeConfig $eccubeConfig, |
||
| 122 | ContainerInterface $container, |
||
| 123 | CacheUtil $cacheUtil, |
||
| 124 | ComposerServiceInterface $composerService, |
||
| 125 | PluginApiService $pluginApiService, |
||
| 126 | SystemService $systemService |
||
| 127 | ) { |
||
| 128 | $this->entityManager = $entityManager; |
||
|
|
|||
| 129 | $this->pluginRepository = $pluginRepository; |
||
| 130 | $this->entityProxyService = $entityProxyService; |
||
| 131 | $this->schemaService = $schemaService; |
||
| 132 | $this->eccubeConfig = $eccubeConfig; |
||
| 133 | $this->projectRoot = $eccubeConfig->get('kernel.project_dir'); |
||
| 134 | $this->environment = $eccubeConfig->get('kernel.environment'); |
||
| 135 | $this->container = $container; |
||
| 136 | $this->cacheUtil = $cacheUtil; |
||
| 137 | $this->composerService = $composerService; |
||
| 138 | $this->pluginApiService = $pluginApiService; |
||
| 139 | $this->systemService = $systemService; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * ファイル指定してのプラグインインストール |
||
| 144 | * |
||
| 145 | * @param string $path path to tar.gz/zip plugin file |
||
| 146 | * @param int $source |
||
| 147 | * |
||
| 148 | * @return boolean |
||
| 149 | * |
||
| 150 | * @throws PluginException |
||
| 151 | * @throws \Exception |
||
| 152 | */ |
||
| 153 | public function install($path, $source = 0) |
||
| 154 | { |
||
| 155 | $pluginBaseDir = null; |
||
| 156 | $tmp = null; |
||
| 157 | try { |
||
| 158 | // プラグイン配置前に実施する処理 |
||
| 159 | $this->preInstall(); |
||
| 160 | $tmp = $this->createTempDir(); |
||
| 161 | |||
| 162 | // 一旦テンポラリに展開 |
||
| 163 | $this->unpackPluginArchive($path, $tmp); |
||
| 164 | $this->checkPluginArchiveContent($tmp); |
||
| 165 | |||
| 166 | $config = $this->readConfig($tmp); |
||
| 167 | // テンポラリのファイルを削除 |
||
| 168 | $this->deleteFile($tmp); |
||
| 169 | |||
| 170 | // 重複していないかチェック |
||
| 171 | $this->checkSamePlugin($config['code']); |
||
| 172 | |||
| 173 | $pluginBaseDir = $this->calcPluginDir($config['code']); |
||
| 174 | // 本来の置き場所を作成 |
||
| 175 | $this->createPluginDir($pluginBaseDir); |
||
| 176 | |||
| 177 | // 問題なければ本当のplugindirへ |
||
| 178 | $this->unpackPluginArchive($path, $pluginBaseDir); |
||
| 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) |
||
| 228 | |||
| 229 | // インストール事前処理 |
||
| 230 | public function preInstall() |
||
| 236 | |||
| 237 | // インストール事後処理 |
||
| 238 | public function postInstall($config, $source) |
||
| 274 | |||
| 275 | public function generateProxyAndUpdateSchema(Plugin $plugin, $config, $uninstall = false) |
||
| 311 | |||
| 312 | public function createTempDir() |
||
| 324 | |||
| 325 | public function deleteDirs($arr) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $archive |
||
| 337 | * @param string $dir |
||
| 338 | * |
||
| 339 | * @throws PluginException |
||
| 340 | */ |
||
| 341 | public function unpackPluginArchive($archive, $dir) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param $dir |
||
| 361 | * @param array $config_cache |
||
| 362 | * |
||
| 363 | * @throws PluginException |
||
| 364 | */ |
||
| 365 | public function checkPluginArchiveContent($dir, array $config_cache = []) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param $pluginDir |
||
| 395 | * |
||
| 396 | * @return array |
||
| 397 | * |
||
| 398 | * @throws PluginException |
||
| 399 | */ |
||
| 400 | public function readConfig($pluginDir) |
||
| 427 | |||
| 428 | public function checkSymbolName($string) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $path |
||
| 438 | */ |
||
| 439 | public function deleteFile($path) |
||
| 444 | |||
| 445 | public function checkSamePlugin($code) |
||
| 453 | |||
| 454 | public function calcPluginDir($code) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param string $d |
||
| 461 | * |
||
| 462 | * @throws PluginException |
||
| 463 | */ |
||
| 464 | public function createPluginDir($d) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param $meta |
||
| 474 | * @param int $source |
||
| 475 | * |
||
| 476 | * @return Plugin |
||
| 477 | * |
||
| 478 | * @throws PluginException |
||
| 479 | */ |
||
| 480 | public function registerPlugin($meta, $source = 0) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param $meta |
||
| 504 | * @param string $method |
||
| 505 | */ |
||
| 506 | public function callPluginManagerMethod($meta, $method) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param Plugin $plugin |
||
| 519 | * @param bool $force |
||
| 520 | * |
||
| 521 | * @return bool |
||
| 522 | * |
||
| 523 | * @throws \Exception |
||
| 524 | */ |
||
| 525 | public function uninstall(Plugin $plugin, $force = true) |
||
| 557 | |||
| 558 | public function unregisterPlugin(Plugin $p) |
||
| 568 | |||
| 569 | public function disable(Plugin $plugin) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Proxyを再生成します. |
||
| 576 | * |
||
| 577 | * @param Plugin $plugin プラグイン |
||
| 578 | * @param boolean $temporary プラグインが無効状態でも一時的に生成するかどうか |
||
| 579 | * @param string|null $outputDir 出力先 |
||
| 580 | * @param bool $uninstall プラグイン削除の場合はtrue |
||
| 581 | * |
||
| 582 | * @return array 生成されたファイルのパス |
||
| 583 | */ |
||
| 584 | private function regenerateProxy(Plugin $plugin, $temporary, $outputDir = null, $uninstall = false) |
||
| 617 | |||
| 618 | public function enable(Plugin $plugin, $enable = true) |
||
| 619 | { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Update plugin |
||
| 652 | * |
||
| 653 | * @param Plugin $plugin |
||
| 654 | * @param string $path |
||
| 655 | * |
||
| 656 | * @return bool |
||
| 657 | * |
||
| 658 | * @throws PluginException |
||
| 659 | * @throws \Exception |
||
| 660 | */ |
||
| 661 | public function update(Plugin $plugin, $path) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Update plugin |
||
| 698 | * |
||
| 699 | * @param Plugin $plugin |
||
| 700 | * @param array $meta Config data |
||
| 701 | * |
||
| 702 | * @throws \Exception |
||
| 703 | */ |
||
| 704 | public function updatePlugin(Plugin $plugin, $meta) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Get array require by plugin |
||
| 728 | * Todo: need define dependency plugin mechanism |
||
| 729 | * |
||
| 730 | * @param array|Plugin $plugin format as plugin from api |
||
| 731 | * |
||
| 732 | * @return array|mixed |
||
| 733 | * |
||
| 734 | * @throws PluginException |
||
| 735 | */ |
||
| 736 | public function getPluginRequired($plugin) |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Find the dependent plugins that need to be disabled |
||
| 752 | * |
||
| 753 | * @param string $pluginCode |
||
| 754 | * |
||
| 755 | * @return array plugin code |
||
| 756 | */ |
||
| 757 | public function findDependentPluginNeedDisable($pluginCode) |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Find the other plugin that has requires on it. |
||
| 764 | * Check in both dtb_plugin table and <PluginCode>/composer.json |
||
| 765 | * |
||
| 766 | * @param string $pluginCode |
||
| 767 | * @param bool $enableOnly |
||
| 768 | * |
||
| 769 | * @return array plugin code |
||
| 770 | */ |
||
| 771 | public function findDependentPlugin($pluginCode, $enableOnly = false) |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Get dependent plugin by code |
||
| 806 | * It's base on composer.json |
||
| 807 | * Return the plugin code and version in the format of the composer |
||
| 808 | * |
||
| 809 | * @param string $pluginCode |
||
| 810 | * @param int|null $libraryType |
||
| 811 | * self::ECCUBE_LIBRARY only return library/plugin of eccube |
||
| 812 | * self::OTHER_LIBRARY only return library/plugin of 3rd part ex: symfony, composer, ... |
||
| 813 | * default : return all library/plugin |
||
| 814 | * |
||
| 815 | * @return array format [packageName1 => version1, packageName2 => version2] |
||
| 816 | */ |
||
| 817 | public function getDependentByCode($pluginCode, $libraryType = null) |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Format array dependent plugin to string |
||
| 849 | * It is used for commands. |
||
| 850 | * |
||
| 851 | * @param array $packages format [packageName1 => version1, packageName2 => version2] |
||
| 852 | * @param bool $getVersion |
||
| 853 | * |
||
| 854 | * @return string format if version=true: "packageName1:version1 packageName2:version2", if version=false: "packageName1 packageName2" |
||
| 855 | */ |
||
| 856 | public function parseToComposerCommand(array $packages, $getVersion = true) |
||
| 867 | |||
| 868 | /** |
||
| 869 | * リソースファイル等をコピー |
||
| 870 | * コピー元となるファイルの置き場所は固定であり、 |
||
| 871 | * [プラグインコード]/Resource/assets |
||
| 872 | * 配下に置かれているファイルが所定の位置へコピーされる |
||
| 873 | * |
||
| 874 | * @param $pluginCode |
||
| 875 | */ |
||
| 876 | public function copyAssets($pluginCode) |
||
| 886 | |||
| 887 | /** |
||
| 888 | * コピーしたリソースファイル等を削除 |
||
| 889 | * |
||
| 890 | * @param string $pluginCode |
||
| 891 | */ |
||
| 892 | public function removeAssets($pluginCode) |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Plugin is exist check |
||
| 905 | * |
||
| 906 | * @param array $plugins get from api |
||
| 907 | * @param string $pluginCode |
||
| 908 | * |
||
| 909 | * @return false|int|string |
||
| 910 | */ |
||
| 911 | public function checkPluginExist($plugins, $pluginCode) |
||
| 921 | |||
| 922 | /** |
||
| 923 | * @param string $code |
||
| 924 | * |
||
| 925 | * @return bool |
||
| 926 | */ |
||
| 927 | private function isEnable($code) |
||
| 939 | |||
| 940 | public function installComposer($code) { |
||
| 954 | } |
||
| 955 |
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.