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 Config 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 Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Config |
||
| 20 | { |
||
| 21 | const UPDATE_MANUAL = 0; |
||
| 22 | const UPDATE_AUTO = 1; |
||
| 23 | const UPDATE_CRON_JOB = 2; |
||
| 24 | const MARKETPLACE_URL = 'sn.connect.shopware.com'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var ModelManager |
||
| 28 | */ |
||
| 29 | private $manager; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \Shopware\CustomModels\Connect\ConfigRepository |
||
| 33 | */ |
||
| 34 | private $repository; |
||
| 35 | |||
| 36 | /** @var \Shopware\Models\Shop\Shop */ |
||
| 37 | private $shopRepository; |
||
| 38 | |||
| 39 | private $customerGroupRepository; |
||
| 40 | |||
| 41 | private $priceGateway; |
||
| 42 | |||
| 43 | private $connectGateway; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param ModelManager $manager |
||
| 47 | */ |
||
| 48 | public function __construct(ModelManager $manager) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param $name |
||
| 55 | * @param null $default |
||
| 56 | * @param null $shopId |
||
| 57 | * @return null |
||
| 58 | */ |
||
| 59 | public function getConfig($name, $default = null, $shopId = null) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function hasSsl() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function isCronActive() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function isCronPluginActive() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public function isDynamicStreamCronActive() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param $name |
||
| 166 | * @param null $default |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | private function getPluginConfig($name, $default = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param $name |
||
| 176 | * @param null $default |
||
| 177 | * @return null |
||
| 178 | */ |
||
| 179 | private function getMainConfig($name, $default = null) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param null $name |
||
| 198 | * @param null $shopId |
||
| 199 | * @param null $groupName |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function getConfigs($name = null, $shopId = null, $groupName = null) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param $name |
||
| 211 | * @param $value |
||
| 212 | * @param null $shopId |
||
| 213 | * @param null $groupName |
||
| 214 | */ |
||
| 215 | public function setConfig($name, $value, $shopId = null, $groupName = null) |
||
| 237 | |||
| 238 | public function deleteConfig($name, $shopId = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Helper function which returns general configuration |
||
| 257 | * for each shop. |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | public function getGeneralConfig() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Stores data general config |
||
| 274 | * data into database. |
||
| 275 | * |
||
| 276 | * @param array $data |
||
| 277 | */ |
||
| 278 | public function setGeneralConfigs($data) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Helper function which returns import configuration. |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | public function getImportConfig() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Stores data import config |
||
| 329 | * data into database. |
||
| 330 | * |
||
| 331 | * @param array $data |
||
| 332 | */ |
||
| 333 | View Code Duplication | public function setImportConfigs($data) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Helper function which returns export configuration. |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | public function getExportConfig() |
||
| 369 | { |
||
| 370 | $query = "SELECT `name`, `value` FROM s_plugin_connect_config |
||
| 371 | WHERE `shopId` IS NULL AND `groupName` = 'export'"; |
||
| 372 | |||
| 373 | $result = Shopware()->Db()->fetchPairs($query); |
||
| 374 | |||
| 375 | foreach ($result as $key => $value) { |
||
| 376 | $decodedString = json_decode($value, true); |
||
| 377 | if ($decodedString !== null) { |
||
| 378 | $result[$key] = $decodedString; |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | return $result; |
||
| 383 | } |
||
| 384 | |||
| 385 | View Code Duplication | public function setExportConfigs($data) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Stores units mapping |
||
| 418 | * data into database. |
||
| 419 | * @param $units |
||
| 420 | */ |
||
| 421 | public function setUnitsMapping($units) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Returns units mapping from Connect config table |
||
| 444 | * |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | public function getUnitsMappings() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Compare given export price configuration |
||
| 457 | * and current export price configuration |
||
| 458 | * @param array $config |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function compareExportConfiguration($config) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param $priceExportMode |
||
| 481 | * @param $customerGroupKey |
||
| 482 | * @return array |
||
| 483 | */ |
||
| 484 | public function collectExportPrice($priceExportMode, $customerGroupKey) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns config entity by value |
||
| 521 | * @param $value |
||
| 522 | * @return \Shopware\CustomModels\Connect\Config |
||
| 523 | */ |
||
| 524 | public function getConfigByValue($value) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Saves array with marketplace settings to config table |
||
| 533 | * |
||
| 534 | * @param MarketplaceSettings $settings |
||
| 535 | */ |
||
| 536 | public function setMarketplaceSettings(MarketplaceSettings $settings) |
||
| 543 | |||
| 544 | public function getMarketplaceUrl() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @return int |
||
| 555 | */ |
||
| 556 | View Code Duplication | public function getDefaultShopId() |
|
| 566 | |||
| 567 | /** |
||
| 568 | * @return \Shopware\Models\Category\Category |
||
| 569 | */ |
||
| 570 | View Code Duplication | public function getDefaultShopCategory() |
|
| 582 | |||
| 583 | /** |
||
| 584 | * @return \Shopware\Components\Model\ModelRepository|\Shopware\CustomModels\Connect\ConfigRepository |
||
| 585 | */ |
||
| 586 | private function getConfigRepository() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @return \Shopware\Components\Model\ModelRepository|\Shopware\Models\Shop\Shop |
||
| 597 | */ |
||
| 598 | private function getShopRepository() |
||
| 606 | |||
| 607 | private function getPriceGateway() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @return \Shopware\Components\Model\ModelRepository |
||
| 620 | */ |
||
| 621 | private function getCustomerGroupRepository() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @return \Shopware\Connect\Gateway\PDO |
||
| 632 | */ |
||
| 633 | View Code Duplication | private function getConnectPDOGateway() |
|
| 641 | } |
||
| 642 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.