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 |
||
| 18 | class SubscriberRegistration |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var ModelManager |
||
| 22 | */ |
||
| 23 | private $modelManager; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Config |
||
| 27 | */ |
||
| 28 | private $config; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Enlight_Components_Db_Adapter_Pdo_Mysql |
||
| 32 | */ |
||
| 33 | private $db; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @TODO: Subscribers should not depend on the Bootstrap class. If you see a possible solution refactor it please. |
||
| 37 | * |
||
| 38 | * @var \Shopware_Plugins_Backend_SwagConnect_Bootstrap |
||
| 39 | */ |
||
| 40 | private $pluginBootstrap; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \Enlight_Event_EventManager |
||
| 44 | */ |
||
| 45 | private $eventManager; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var SDK |
||
| 49 | */ |
||
| 50 | private $SDK; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var ConnectFactory |
||
| 54 | */ |
||
| 55 | private $connectFactory; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Helper |
||
| 59 | */ |
||
| 60 | private $helper; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * This property saves all product updates and will be inserted back later |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $productUpdates = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Lifecycle |
||
| 71 | */ |
||
| 72 | private $lifecycle; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param Config $config |
||
| 76 | * @param ModelManager $modelManager |
||
| 77 | * @param Enlight_Components_Db_Adapter_Pdo_Mysql $db |
||
| 78 | * @param \Shopware_Plugins_Backend_SwagConnect_Bootstrap $pluginBootstrap |
||
| 79 | * @param \Enlight_Event_EventManager $eventManager |
||
| 80 | * @param SDK $SDK |
||
| 81 | * @param ConnectFactory $connectFactory |
||
| 82 | * @param Helper $helper |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function __construct( |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param boolean $isShopware52 |
||
| 106 | */ |
||
| 107 | public function registerSubscribers($isShopware52) |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Default subscribers can safely be used, even if the api key wasn't verified, yet |
||
| 142 | * |
||
| 143 | * @param bool $isShopware52 |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | private function getDefaultSubscribers($isShopware52) |
||
| 194 | |||
| 195 | private function getSubscribersForUnverifiedKeys() |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * These subscribers will only be used, once the user has verified his api key |
||
| 206 | * This will prevent the users from having shopware Connect extensions in their frontend |
||
| 207 | * even if they cannot use shopware Connect due to the missing / wrong api key |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | private function getSubscribersForVerifiedKeys() |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * Creates checkout subscriber |
||
| 231 | * |
||
| 232 | * @return Checkout |
||
| 233 | */ |
||
| 234 | private function createCheckoutSubscriber() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Generate changes for updated Articles and Details. |
||
| 251 | * On postFlush all related entities are updated and product can |
||
| 252 | * be fetched from DB correctly. |
||
| 253 | * |
||
| 254 | * @param \Doctrine\ORM\Event\PostFlushEventArgs $eventArgs |
||
| 255 | */ |
||
| 256 | public function postFlush(\Doctrine\ORM\Event\PostFlushEventArgs $eventArgs) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return Lifecycle |
||
| 267 | */ |
||
| 268 | private function getLifecycleSubscriber() |
||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * Collect updated Articles and Details |
||
| 283 | * Lifecycle events don't work correctly, because products will be fetched via query builder, |
||
| 284 | * but related entities like price are not updated yet. |
||
| 285 | * |
||
| 286 | * @param \Doctrine\ORM\Event\OnFlushEventArgs $eventArgs |
||
| 287 | */ |
||
| 288 | public function onFlush(\Doctrine\ORM\Event\OnFlushEventArgs $eventArgs) |
||
| 305 | } |
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.