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:
| 1 | <?php |
||
| 22 | class CronJob implements SubscriberInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var \ShopwarePlugins\Connect\Components\Config |
||
| 26 | */ |
||
| 27 | private $configComponent; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var SDK |
||
| 31 | */ |
||
| 32 | protected $sdk; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ProductStreamService |
||
| 36 | */ |
||
| 37 | protected $streamService; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ConnectExport |
||
| 41 | */ |
||
| 42 | protected $connectExport; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Helper |
||
| 46 | */ |
||
| 47 | private $helper; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param SDK $sdk |
||
| 51 | * @param ConnectExport $connectExport |
||
| 52 | * @param Config $configComponent |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function __construct( |
|
|
|
|||
| 55 | SDK $sdk, |
||
| 56 | ConnectExport $connectExport, |
||
| 57 | Config $configComponent, |
||
| 58 | Helper $helper |
||
| 59 | ) { |
||
| 60 | $this->connectExport = $connectExport; |
||
| 61 | $this->sdk = $sdk; |
||
| 62 | $this->configComponent = $configComponent; |
||
| 63 | $this->helper = $helper; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public static function getSubscribedEvents() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return ImageImport |
||
| 80 | */ |
||
| 81 | public function getImageImport() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Import images of new products |
||
| 93 | * |
||
| 94 | * @param \Shopware_Components_Cron_CronJob $job |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public function importImages(\Shopware_Components_Cron_CronJob $job) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Collect all own products and send them |
||
| 107 | * to Connect system. |
||
| 108 | * |
||
| 109 | * Used to update products with many variants. |
||
| 110 | * |
||
| 111 | * @param \Shopware_Components_Cron_CronJob $job |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function updateProducts(\Shopware_Components_Cron_CronJob $job) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param \Shopware_Components_Cron_CronJob $job |
||
| 138 | */ |
||
| 139 | public function exportDynamicStreams(\Shopware_Components_Cron_CronJob $job) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * If article is not taking part of any shopware stream it will be removed |
||
| 207 | * @param array $articleIds |
||
| 208 | */ |
||
| 209 | private function removeArticlesFromStream(array $articleIds) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return ProductStreamService $streamService |
||
| 224 | */ |
||
| 225 | private function getStreamService() |
||
| 233 | } |
||
| 234 |
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.