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 | 1 | 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 | 1 | $this->entityProxyService = $entityProxyService; |
|
| 131 | 1 | $this->schemaService = $schemaService; |
|
| 132 | 1 | $this->eccubeConfig = $eccubeConfig; |
|
| 133 | 1 | $this->projectRoot = $eccubeConfig->get('kernel.project_dir'); |
|
| 134 | 1 | $this->environment = $eccubeConfig->get('kernel.environment'); |
|
| 135 | 1 | $this->container = $container; |
|
| 136 | 1 | $this->cacheUtil = $cacheUtil; |
|
| 137 | 1 | $this->composerService = $composerService; |
|
| 138 | 1 | $this->pluginApiService = $pluginApiService; |
|
| 139 | 1 | $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 | 1 | public function install($path, $source = 0) |
|
| 154 | { |
||
| 155 | 1 | $pluginBaseDir = null; |
|
| 156 | 1 | $tmp = null; |
|
| 157 | try { |
||
| 158 | // プラグイン配置前に実施する処理 |
||
| 159 | 1 | $this->preInstall(); |
|
| 160 | 1 | $tmp = $this->createTempDir(); |
|
| 161 | |||
| 162 | // 一旦テンポラリに展開 |
||
| 163 | 1 | $this->unpackPluginArchive($path, $tmp); |
|
| 164 | 1 | $this->checkPluginArchiveContent($tmp); |
|
| 165 | |||
| 166 | 1 | $config = $this->readConfig($tmp); |
|
| 167 | 1 | // テンポラリのファイルを削除 |
|
| 168 | $this->deleteFile($tmp); |
||
| 169 | 1 | ||
| 170 | // 重複していないかチェック |
||
| 171 | $this->checkSamePlugin($config['code']); |
||
| 172 | 1 | ||
| 173 | $pluginBaseDir = $this->calcPluginDir($config['code']); |
||
| 174 | 1 | // 本来の置き場所を作成 |
|
| 175 | $this->createPluginDir($pluginBaseDir); |
||
| 176 | 1 | ||
| 177 | // 問題なければ本当のplugindirへ |
||
| 178 | $this->unpackPluginArchive($path, $pluginBaseDir); |
||
| 179 | 1 | ||
| 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 | 1 | } |
|
| 192 | |||
| 193 | return true; |
||
| 194 | 1 | } |
|
| 195 | 1 | ||
| 196 | 1 | /** |
|
| 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 | 1 | return $DependPlugin ? $DependPlugin->isEnabled() == false : true; |
|
| 216 | }); |
||
| 217 | |||
| 218 | if (!empty($notInstalledOrDisabled)) { |
||
| 219 | 1 | $names = array_map(function ($p) { return $p['name']; }, $notInstalledOrDisabled); |
|
| 220 | 1 | throw new PluginException(implode(', ', $names).'を有効化してください。'); |
|
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | 1 | $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 | /** |
||
| 276 | * プラグインの Proxy ファイルを生成して UpdateSchema を実行する. |
||
| 277 | * |
||
| 278 | * @param Plugin $plugin プラグインオブジェクト |
||
| 279 | 1 | * @param array $config プラグインの composer.json の配列 |
|
| 280 | * @param bool $uninstall アンインストールする場合は true |
||
| 281 | 1 | * @param bool $saveMode SQL を即時実行する場合は true |
|
| 282 | */ |
||
| 283 | public function generateProxyAndUpdateSchema(Plugin $plugin, $config, $uninstall = false, $saveMode = true) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * プラグインの Proxy ファイルを生成してコールバック関数を実行する. |
||
| 292 | * |
||
| 293 | * コールバック関数は主に SchemaTool が利用されます. |
||
| 294 | * Proxy ファイルを出力する一時ディレクトリを指定しない場合は内部で生成し, コールバック関数実行後に削除されます. |
||
| 295 | * |
||
| 296 | * @param callable $callback Proxy ファイルを生成した後に実行されるコールバック関数 |
||
| 297 | * @param Plugin $plugin プラグインオブジェクト |
||
| 298 | * @param array $config プラグインの composer.json の配列 |
||
| 299 | * @param bool $uninstall アンインストールする場合は true |
||
| 300 | * @param string $tmpProxyOutputDir Proxy ファイルを出力する一時ディレクトリ |
||
| 301 | */ |
||
| 302 | public function generateProxyAndCallback(callable $callback, Plugin $plugin, $config, $uninstall = false, $tmpProxyOutputDir = null) |
||
| 303 | { |
||
| 304 | 1 | if ($plugin->isEnabled()) { |
|
| 305 | $generatedFiles = $this->regenerateProxy($plugin, false, $tmpProxyOutputDir ? $tmpProxyOutputDir : $this->projectRoot.'/app/proxy/entity'); |
||
| 306 | |||
| 307 | 1 | call_user_func($callback, $generatedFiles, $tmpProxyOutputDir ? $tmpProxyOutputDir : $this->projectRoot.'/app/proxy/entity'); |
|
| 308 | } else { |
||
| 309 | // Proxyのクラスをロードせずにスキーマを更新するために、 |
||
| 310 | // インストール時には一時的なディレクトリにProxyを生成する |
||
| 311 | $createOutputDir = false; |
||
| 312 | View Code Duplication | if (is_null($tmpProxyOutputDir)) { |
|
| 313 | 1 | $tmpProxyOutputDir = sys_get_temp_dir().'/proxy_'.StringUtil::random(12); |
|
| 314 | @mkdir($tmpProxyOutputDir); |
||
| 315 | $createOutputDir = true; |
||
| 316 | 1 | } |
|
| 317 | |||
| 318 | try { |
||
| 319 | 1 | if (!$uninstall) { |
|
| 320 | // プラグインmetadata定義を追加 |
||
| 321 | $entityDir = $this->eccubeConfig['plugin_realdir'].'/'.$plugin->getCode().'/Entity'; |
||
| 322 | if (file_exists($entityDir)) { |
||
| 323 | 1 | $ormConfig = $this->entityManager->getConfiguration(); |
|
| 324 | $chain = $ormConfig->getMetadataDriverImpl(); |
||
| 325 | $driver = $ormConfig->newDefaultAnnotationDriver([$entityDir], false); |
||
| 326 | 1 | $namespace = 'Plugin\\'.$config['code'].'\\Entity'; |
|
| 327 | $chain->addDriver($driver, $namespace); |
||
| 328 | $ormConfig->addEntityNamespace($plugin->getCode(), $namespace); |
||
| 329 | } |
||
| 330 | 1 | } |
|
| 331 | |||
| 332 | // 一時的に利用するProxyを生成してからスキーマを更新する |
||
| 333 | $generatedFiles = $this->regenerateProxy($plugin, true, $tmpProxyOutputDir, $uninstall); |
||
| 334 | |||
| 335 | 1 | call_user_func($callback, $generatedFiles, $tmpProxyOutputDir); |
|
| 336 | } finally { |
||
| 337 | if ($createOutputDir) { |
||
| 338 | foreach (glob("${tmpProxyOutputDir}/*") as $f) { |
||
| 339 | unlink($f); |
||
| 340 | } |
||
| 341 | rmdir($tmpProxyOutputDir); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | 1 | public function createTempDir() |
|
| 359 | |||
| 360 | public function deleteDirs($arr) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $archive |
||
| 372 | * @param string $dir |
||
| 373 | 1 | * |
|
| 374 | 1 | * @throws PluginException |
|
| 375 | */ |
||
| 376 | public function unpackPluginArchive($archive, $dir) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param $dir |
||
| 396 | * @param array $config_cache |
||
| 397 | * |
||
| 398 | * @throws PluginException |
||
| 399 | */ |
||
| 400 | public function checkPluginArchiveContent($dir, array $config_cache = []) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param $pluginDir |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | * |
||
| 433 | * @throws PluginException |
||
| 434 | */ |
||
| 435 | public function readConfig($pluginDir) |
||
| 462 | |||
| 463 | public function checkSymbolName($string) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param string $path |
||
| 473 | */ |
||
| 474 | public function deleteFile($path) |
||
| 479 | |||
| 480 | public function checkSamePlugin($code) |
||
| 488 | |||
| 489 | public function calcPluginDir($code) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param string $d |
||
| 496 | * |
||
| 497 | * @throws PluginException |
||
| 498 | */ |
||
| 499 | public function createPluginDir($d) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param $meta |
||
| 509 | * @param int $source |
||
| 510 | * |
||
| 511 | * @return Plugin |
||
| 512 | * |
||
| 513 | * @throws PluginException |
||
| 514 | */ |
||
| 515 | public function registerPlugin($meta, $source = 0) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param $meta |
||
| 539 | * @param string $method |
||
| 540 | */ |
||
| 541 | public function callPluginManagerMethod($meta, $method) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param Plugin $plugin |
||
| 554 | * @param bool $force |
||
| 555 | * |
||
| 556 | * @return bool |
||
| 557 | * |
||
| 558 | * @throws \Exception |
||
| 559 | */ |
||
| 560 | public function uninstall(Plugin $plugin, $force = true) |
||
| 592 | |||
| 593 | public function unregisterPlugin(Plugin $p) |
||
| 603 | |||
| 604 | public function disable(Plugin $plugin) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Proxyを再生成します. |
||
| 611 | * |
||
| 612 | * @param Plugin $plugin プラグイン |
||
| 613 | * @param boolean $temporary プラグインが無効状態でも一時的に生成するかどうか |
||
| 614 | * @param string|null $outputDir 出力先 |
||
| 615 | * @param bool $uninstall プラグイン削除の場合はtrue |
||
| 616 | * |
||
| 617 | * @return array 生成されたファイルのパス |
||
| 618 | */ |
||
| 619 | private function regenerateProxy(Plugin $plugin, $temporary, $outputDir = null, $uninstall = false) |
||
| 652 | |||
| 653 | public function enable(Plugin $plugin, $enable = true) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Update plugin |
||
| 687 | * |
||
| 688 | * @param Plugin $plugin |
||
| 689 | * @param string $path |
||
| 690 | * |
||
| 691 | * @return bool |
||
| 692 | * |
||
| 693 | * @throws PluginException |
||
| 694 | * @throws \Exception |
||
| 695 | */ |
||
| 696 | public function update(Plugin $plugin, $path) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Update plugin |
||
| 733 | * |
||
| 734 | * @param Plugin $plugin |
||
| 735 | * @param array $meta Config data |
||
| 736 | * |
||
| 737 | * @throws \Exception |
||
| 738 | */ |
||
| 739 | public function updatePlugin(Plugin $plugin, $meta) |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Get array require by plugin |
||
| 763 | * Todo: need define dependency plugin mechanism |
||
| 764 | * |
||
| 765 | * @param array|Plugin $plugin format as plugin from api |
||
| 766 | * |
||
| 767 | * @return array|mixed |
||
| 768 | * |
||
| 769 | * @throws PluginException |
||
| 770 | */ |
||
| 771 | public function getPluginRequired($plugin) |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Find the dependent plugins that need to be disabled |
||
| 787 | * |
||
| 788 | * @param string $pluginCode |
||
| 789 | * |
||
| 790 | * @return array plugin code |
||
| 791 | */ |
||
| 792 | public function findDependentPluginNeedDisable($pluginCode) |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Find the other plugin that has requires on it. |
||
| 799 | * Check in both dtb_plugin table and <PluginCode>/composer.json |
||
| 800 | * |
||
| 801 | * @param string $pluginCode |
||
| 802 | * @param bool $enableOnly |
||
| 803 | * |
||
| 804 | * @return array plugin code |
||
| 805 | */ |
||
| 806 | public function findDependentPlugin($pluginCode, $enableOnly = false) |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Get dependent plugin by code |
||
| 841 | * It's base on composer.json |
||
| 842 | * Return the plugin code and version in the format of the composer |
||
| 843 | * |
||
| 844 | * @param string $pluginCode |
||
| 845 | * @param int|null $libraryType |
||
| 846 | * self::ECCUBE_LIBRARY only return library/plugin of eccube |
||
| 847 | * self::OTHER_LIBRARY only return library/plugin of 3rd part ex: symfony, composer, ... |
||
| 848 | * default : return all library/plugin |
||
| 849 | * |
||
| 850 | * @return array format [packageName1 => version1, packageName2 => version2] |
||
| 851 | */ |
||
| 852 | public function getDependentByCode($pluginCode, $libraryType = null) |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Format array dependent plugin to string |
||
| 884 | * It is used for commands. |
||
| 885 | * |
||
| 886 | * @param array $packages format [packageName1 => version1, packageName2 => version2] |
||
| 887 | * @param bool $getVersion |
||
| 888 | * |
||
| 889 | * @return string format if version=true: "packageName1:version1 packageName2:version2", if version=false: "packageName1 packageName2" |
||
| 890 | */ |
||
| 891 | public function parseToComposerCommand(array $packages, $getVersion = true) |
||
| 902 | |||
| 903 | /** |
||
| 904 | * リソースファイル等をコピー |
||
| 905 | * コピー元となるファイルの置き場所は固定であり、 |
||
| 906 | * [プラグインコード]/Resource/assets |
||
| 907 | * 配下に置かれているファイルが所定の位置へコピーされる |
||
| 908 | * |
||
| 909 | * @param $pluginCode |
||
| 910 | */ |
||
| 911 | public function copyAssets($pluginCode) |
||
| 921 | |||
| 922 | /** |
||
| 923 | * コピーしたリソースファイル等を削除 |
||
| 924 | * |
||
| 925 | * @param string $pluginCode |
||
| 926 | */ |
||
| 927 | public function removeAssets($pluginCode) |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Plugin is exist check |
||
| 940 | * |
||
| 941 | * @param array $plugins get from api |
||
| 942 | * @param string $pluginCode |
||
| 943 | * |
||
| 944 | * @return false|int|string |
||
| 945 | */ |
||
| 946 | public function checkPluginExist($plugins, $pluginCode) |
||
| 956 | |||
| 957 | /** |
||
| 958 | * @param string $code |
||
| 959 | * |
||
| 960 | * @return bool |
||
| 961 | */ |
||
| 962 | private function isEnable($code) |
||
| 974 | } |
||
| 975 |
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.