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 VariantConfigurator 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 VariantConfigurator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class VariantConfigurator |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var ModelManager |
||
| 28 | */ |
||
| 29 | private $manager; |
||
| 30 | |||
| 31 | private $translationGateway; |
||
| 32 | |||
| 33 | private $localeRepository; |
||
| 34 | |||
| 35 | private $shopRepository; |
||
| 36 | |||
| 37 | public function __construct(ModelManager $manager, ProductTranslationsGateway $translationsGateway) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Configure variant group, options and configurator set |
||
| 45 | * |
||
| 46 | * @param \Shopware\Connect\Struct\Product $product |
||
| 47 | * @param \Shopware\Models\Article\Detail $detail |
||
| 48 | */ |
||
| 49 | public function configureVariantAttributes(Product $product, Detail $detail) |
||
| 50 | { |
||
| 51 | if (count($product->variant) === 0) { |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | $article = $detail->getArticle(); |
||
| 56 | $detailOptions = $detail->getConfiguratorOptions(); |
||
| 57 | if (!$article->getConfiguratorSet()) { |
||
| 58 | $configSet = new Set(); |
||
| 59 | $configSet->setName('Set-' . $article->getName()); |
||
| 60 | $configSet->setArticles([$article]); |
||
| 61 | $configSet->setType(0); |
||
| 62 | $article->setConfiguratorSet($configSet); |
||
| 63 | } else { |
||
| 64 | $configSet = $article->getConfiguratorSet(); |
||
| 65 | } |
||
| 66 | if ($product->configuratorSetType !== null) { |
||
| 67 | $configSet->setType($product->configuratorSetType); |
||
| 68 | } |
||
| 69 | |||
| 70 | foreach ($product->variant as $key => $value) { |
||
| 71 | $group = $this->getGroupByName($configSet, $key); |
||
| 72 | |||
| 73 | $option = $this->getOrCreateOptionByName($configSet, $group, $value); |
||
| 74 | |||
| 75 | $configSet = $this->addGroupToConfiguratorSet($configSet, $group); |
||
| 76 | $configSet = $this->addOptionToConfiguratorSet($configSet, $option); |
||
|
|
|||
| 77 | |||
| 78 | $this->manager->persist($option); |
||
| 79 | $this->manager->persist($group); |
||
| 80 | if (!$detailOptions->contains($option)) { |
||
| 81 | $detailOptions->add($option); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->manager->persist($configSet); |
||
| 86 | |||
| 87 | $detail->setConfiguratorOptions($detailOptions); |
||
| 88 | $this->manager->persist($detail); |
||
| 89 | $this->manager->persist($article); |
||
| 90 | |||
| 91 | $this->deleteUnusedOptions($product, $detailOptions, $configSet); |
||
| 92 | |||
| 93 | $this->manager->flush(); |
||
| 94 | |||
| 95 | foreach ($product->variant as $key => $value) { |
||
| 96 | $group = $this->getGroupByName($configSet, $key); |
||
| 97 | $option = $this->getOrCreateOptionByName($configSet, $group, $value); |
||
| 98 | |||
| 99 | //translate configurator groups and configurator options |
||
| 100 | $this->addGroupTranslation($group, $product); |
||
| 101 | $this->addOptionTranslation($option, $product); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * @param Product $product |
||
| 108 | * @param $detailOptions |
||
| 109 | * @param Set $configSet |
||
| 110 | * @throws \Doctrine\DBAL\DBALException |
||
| 111 | */ |
||
| 112 | private function deleteUnusedOptions(Product $product, $detailOptions, Set $configSet) |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @param $detailOption |
||
| 134 | * @return bool |
||
| 135 | * @throws \Doctrine\DBAL\DBALException |
||
| 136 | */ |
||
| 137 | private function isOptionUsed(Option $detailOption) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Creates variant configurator group |
||
| 154 | * |
||
| 155 | * @param string $name |
||
| 156 | * @return \Shopware\Models\Article\Configurator\Group |
||
| 157 | */ |
||
| 158 | public function createConfiguratorGroup($name) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Adds group to configurator set if it does not exist |
||
| 175 | * |
||
| 176 | * @param Set $set |
||
| 177 | * @param Group $group |
||
| 178 | * @return Set |
||
| 179 | */ |
||
| 180 | private function addGroupToConfiguratorSet(Set $set, Group $group) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Adds option to configurator set if it does not exist |
||
| 199 | * |
||
| 200 | * @param Set $set |
||
| 201 | * @param Option $option |
||
| 202 | * @return Set |
||
| 203 | */ |
||
| 204 | private function addOptionToConfiguratorSet(Set $set, Option $option) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Finds group in already assigned configurator set groups. |
||
| 223 | * If it does not exist, then create it. |
||
| 224 | * |
||
| 225 | * @param Set $set |
||
| 226 | * @param $groupName |
||
| 227 | * @return Group |
||
| 228 | */ |
||
| 229 | private function getGroupByName(Set $set, $groupName) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Find option in already assigned configurator set options. |
||
| 250 | * If it does not exist, then create it. |
||
| 251 | * |
||
| 252 | * @param Set $set |
||
| 253 | * @param Group $group |
||
| 254 | * @param $optionName |
||
| 255 | * @return null|object|Option |
||
| 256 | */ |
||
| 257 | private function getOrCreateOptionByName(Set $set, Group $group, $optionName) |
||
| 289 | |||
| 290 | View Code Duplication | private function addGroupTranslation(Group $group, Product $product) |
|
| 314 | |||
| 315 | View Code Duplication | private function addOptionTranslation(Option $option, Product $product) |
|
| 339 | |||
| 340 | private function getShopRepository() |
||
| 348 | |||
| 349 | private function getLocaleRepository() |
||
| 357 | } |
||
| 358 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: