Complex classes like ProductContext 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 ProductContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | final class ProductContext implements Context |
||
45 | { |
||
46 | /** @var SharedStorageInterface */ |
||
47 | private $sharedStorage; |
||
48 | |||
49 | /** @var ProductRepositoryInterface */ |
||
50 | private $productRepository; |
||
51 | |||
52 | /** @var ProductFactoryInterface */ |
||
53 | private $productFactory; |
||
54 | |||
55 | /** @var FactoryInterface */ |
||
56 | private $productTranslationFactory; |
||
57 | |||
58 | /** @var FactoryInterface */ |
||
59 | private $productVariantFactory; |
||
60 | |||
61 | /** @var FactoryInterface */ |
||
62 | private $productVariantTranslationFactory; |
||
63 | |||
64 | /** @var FactoryInterface */ |
||
65 | private $channelPricingFactory; |
||
66 | |||
67 | /** @var FactoryInterface */ |
||
68 | private $productOptionFactory; |
||
69 | |||
70 | /** @var FactoryInterface */ |
||
71 | private $productOptionValueFactory; |
||
72 | |||
73 | /** @var FactoryInterface */ |
||
74 | private $productImageFactory; |
||
75 | |||
76 | /** @var ObjectManager */ |
||
77 | private $objectManager; |
||
78 | |||
79 | /** @var ProductVariantGeneratorInterface */ |
||
80 | private $productVariantGenerator; |
||
81 | |||
82 | /** @var ProductVariantResolverInterface */ |
||
83 | private $defaultVariantResolver; |
||
84 | |||
85 | /** @var ImageUploaderInterface */ |
||
86 | private $imageUploader; |
||
87 | |||
88 | /** @var SlugGeneratorInterface */ |
||
89 | private $slugGenerator; |
||
90 | |||
91 | /** @var array */ |
||
92 | private $minkParameters; |
||
93 | |||
94 | public function __construct( |
||
137 | |||
138 | /** |
||
139 | * @Given the store has a product :productName |
||
140 | * @Given the store has a :productName product |
||
141 | * @Given I added a product :productName |
||
142 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+")$/ |
||
143 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") in ("[^"]+" channel)$/ |
||
144 | */ |
||
145 | public function storeHasAProductPricedAt($productName, int $price = 100, ChannelInterface $channel = null) |
||
151 | |||
152 | /** |
||
153 | * @Given /^(this product) is(?:| also) priced at ("[^"]+") in ("[^"]+" channel)$/ |
||
154 | */ |
||
155 | public function thisProductIsAlsoPricedAtInChannel(ProductInterface $product, int $price, ChannelInterface $channel) |
||
165 | |||
166 | /** |
||
167 | * @Given /^(this product) is(?:| also) available in ("[^"]+" channel)$/ |
||
168 | * @Given /^(this product) is(?:| also) available in the ("[^"]+" channel)$/ |
||
169 | */ |
||
170 | public function thisProductIsAlsoAvailableInChannel(ProductInterface $product, ChannelInterface $channel): void |
||
174 | |||
175 | /** |
||
176 | * @Given /^(this product) is(?:| also) unavailable in ("[^"]+" channel)$/ |
||
177 | */ |
||
178 | public function thisProductIsAlsoUnavailableInChannel(ProductInterface $product, ChannelInterface $channel): void |
||
183 | |||
184 | /** |
||
185 | * @Given the store( also) has a product :productName with code :code |
||
186 | * @Given the store( also) has a product :productName with code :code, created at :date |
||
187 | */ |
||
188 | public function storeHasProductWithCode($productName, $code, $date = 'now') |
||
196 | |||
197 | /** |
||
198 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") available in (channel "[^"]+") and (channel "[^"]+")$/ |
||
199 | */ |
||
200 | public function storeHasAProductPricedAtAvailableInChannels($productName, int $price = 100, ...$channels) |
||
215 | |||
216 | /** |
||
217 | * @Given /^(this product) is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
218 | * @Given /^the (product "[^"]+") is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
219 | */ |
||
220 | public function thisProductIsNamedIn(ProductInterface $product, $name, $locale) |
||
226 | |||
227 | /** |
||
228 | * @Given /^the store has a product named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
||
229 | */ |
||
230 | public function theStoreHasProductNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale) |
||
241 | |||
242 | /** |
||
243 | * @Given /^the store has(?:| a| an) "([^"]+)" configurable product$/ |
||
244 | * @Given /^the store has(?:| a| an) "([^"]+)" configurable product with "([^"]+)" slug$/ |
||
245 | */ |
||
246 | public function storeHasAConfigurableProduct($productName, $slug = null) |
||
272 | |||
273 | /** |
||
274 | * @Given the store has( also) :firstProductName and :secondProductName products |
||
275 | * @Given the store has( also) :firstProductName, :secondProductName and :thirdProductName products |
||
276 | * @Given the store has( also) :firstProductName, :secondProductName, :thirdProductName and :fourthProductName products |
||
277 | */ |
||
278 | public function theStoreHasProducts(...$productsNames) |
||
284 | |||
285 | /** |
||
286 | * @Given /^(this channel) has "([^"]+)", "([^"]+)", "([^"]+)" and "([^"]+)" products$/ |
||
287 | */ |
||
288 | public function thisChannelHasProducts(ChannelInterface $channel, ...$productsNames) |
||
296 | |||
297 | /** |
||
298 | * @Given /^the (product "[^"]+") has(?:| a) "([^"]+)" variant priced at ("[^"]+")$/ |
||
299 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/ |
||
300 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") in ("([^"]+)" channel)$/ |
||
301 | */ |
||
302 | public function theProductHasVariantPricedAt( |
||
316 | |||
317 | /** |
||
318 | * @Given /^the (product "[^"]+") has(?:| a| an) "([^"]+)" variant$/ |
||
319 | * @Given /^(this product) has(?:| a| an) "([^"]+)" variant$/ |
||
320 | * @Given /^(this product) has "([^"]+)", "([^"]+)" and "([^"]+)" variants$/ |
||
321 | */ |
||
322 | public function theProductHasVariants(ProductInterface $product, ...$variantNames) |
||
336 | |||
337 | /** |
||
338 | * @Given /^the (product "[^"]+")(?:| also) has a nameless variant with code "([^"]+)"$/ |
||
339 | * @Given /^(this product)(?:| also) has a nameless variant with code "([^"]+)"$/ |
||
340 | * @Given /^(it)(?:| also) has a nameless variant with code "([^"]+)"$/ |
||
341 | */ |
||
342 | public function theProductHasNamelessVariantWithCode(ProductInterface $product, $variantCode) |
||
348 | |||
349 | /** |
||
350 | * @Given /^the (product "[^"]+")(?:| also) has(?:| a| an) "([^"]+)" variant with code "([^"]+)"$/ |
||
351 | * @Given /^(this product)(?:| also) has(?:| a| an) "([^"]+)" variant with code "([^"]+)"$/ |
||
352 | * @Given /^(it)(?:| also) has(?:| a| an) "([^"]+)" variant with code "([^"]+)"$/ |
||
353 | */ |
||
354 | public function theProductHasVariantWithCode(ProductInterface $product, $variantName, $variantCode) |
||
360 | |||
361 | /** |
||
362 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") which does not require shipping$/ |
||
363 | */ |
||
364 | public function theProductHasVariantWhichDoesNotRequireShipping( |
||
379 | |||
380 | /** |
||
381 | * @Given /^the (product "[^"]+") has(?:| also)(?:| a| an) "([^"]+)" variant$/ |
||
382 | * @Given /^the (product "[^"]+") has(?:| also)(?:| a| an) "([^"]+)" variant at position ([^"]+)$/ |
||
383 | * @Given /^(this product) has(?:| also)(?:| a| an) "([^"]+)" variant at position ([^"]+)$/ |
||
384 | */ |
||
385 | public function theProductHasVariantAtPosition( |
||
399 | |||
400 | /** |
||
401 | * @Given /^(this variant) is also priced at ("[^"]+") in ("([^"]+)" channel)$/ |
||
402 | */ |
||
403 | public function thisVariantIsAlsoPricedAtInChannel(ProductVariantInterface $productVariant, string $price, ChannelInterface $channel) |
||
412 | |||
413 | /** |
||
414 | * @Given /^(it|this product) has(?:| also) variant named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
||
415 | */ |
||
416 | public function itHasVariantNamedInAndIn(ProductInterface $product, $firstName, $firstLocale, $secondName, $secondLocale) |
||
433 | |||
434 | /** |
||
435 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") identified by "([^"]+)"$/ |
||
436 | */ |
||
437 | public function theProductHasVariantPricedAtIdentifiedBy( |
||
445 | |||
446 | /** |
||
447 | * @Given /^(this product) only variant was renamed to "([^"]+)"$/ |
||
448 | */ |
||
449 | public function productOnlyVariantWasRenamed(ProductInterface $product, $variantName) |
||
459 | |||
460 | /** |
||
461 | * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ |
||
462 | * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/ |
||
463 | */ |
||
464 | public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) |
||
470 | |||
471 | /** |
||
472 | * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/ |
||
473 | * @Given the product :product belongs to :taxCategory tax category |
||
474 | */ |
||
475 | public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory) |
||
483 | |||
484 | /** |
||
485 | * @Given /^(it) comes in the following variations:$/ |
||
486 | */ |
||
487 | public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table) |
||
508 | |||
509 | /** |
||
510 | * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/ |
||
511 | */ |
||
512 | public function productVariantBelongsToTaxCategory( |
||
521 | |||
522 | /** |
||
523 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/ |
||
524 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/ |
||
525 | */ |
||
526 | public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values): void |
||
530 | |||
531 | /** |
||
532 | * @Given /^(this product) has an option "([^"]*)" without any values$/ |
||
533 | */ |
||
534 | public function thisProductHasAnOptionWithoutAnyValues(ProductInterface $product, string $optionName): void |
||
538 | |||
539 | /** |
||
540 | * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/ |
||
541 | */ |
||
542 | public function thereIsQuantityOfProducts($quantity, ProductInterface $product) |
||
550 | |||
551 | /** |
||
552 | * @Given /^the (product "([^"]+)") is out of stock$/ |
||
553 | */ |
||
554 | public function theProductIsOutOfStock(ProductInterface $product) |
||
563 | |||
564 | /** |
||
565 | * @When other customer has bought :quantity :product products by this time |
||
566 | */ |
||
567 | public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product) |
||
575 | |||
576 | /** |
||
577 | * @Given /^(this product) is tracked by the inventory$/ |
||
578 | * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/ |
||
579 | */ |
||
580 | public function thisProductIsTrackedByTheInventory(ProductInterface $product) |
||
588 | |||
589 | /** |
||
590 | * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/ |
||
591 | */ |
||
592 | public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, int $price) |
||
607 | |||
608 | /** |
||
609 | * @Given the :product product's :optionValueName size belongs to :shippingCategory shipping category |
||
610 | */ |
||
611 | public function thisProductSizeBelongsToShippingCategory(ProductInterface $product, $optionValueName, ShippingCategoryInterface $shippingCategory) |
||
624 | |||
625 | /** |
||
626 | * @Given /^(this product) has (this product option)$/ |
||
627 | * @Given /^(this product) has (?:a|an) ("[^"]+" option)$/ |
||
628 | */ |
||
629 | public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option) |
||
635 | |||
636 | /** |
||
637 | * @Given /^(this product) has all possible variants$/ |
||
638 | */ |
||
639 | public function thisProductHasAllPossibleVariants(ProductInterface $product) |
||
668 | |||
669 | /** |
||
670 | * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/ |
||
671 | */ |
||
672 | public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant) |
||
679 | |||
680 | /** |
||
681 | * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/ |
||
682 | */ |
||
683 | public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant) |
||
689 | |||
690 | /** |
||
691 | * @Given /^(this product)'s price is ("[^"]+")$/ |
||
692 | * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/ |
||
693 | * @Given /^(this product) price has been changed to ("[^"]+")$/ |
||
694 | */ |
||
695 | public function theProductChangedItsPriceTo(ProductInterface $product, int $price) |
||
704 | |||
705 | /** |
||
706 | * @Given /^(this product)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
||
707 | * @Given /^the ("[^"]+" product)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
||
708 | * @Given /^(it)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
||
709 | */ |
||
710 | public function thisProductHasAnImageWithType(ProductInterface $product, $imagePath, $imageType) |
||
714 | |||
715 | /** |
||
716 | * @Given /^(this product) has an image "([^"]+)" with "([^"]+)" type for ("[^"]+" variant)$/ |
||
717 | */ |
||
718 | public function thisProductHasAnImageWithTypeForVariant( |
||
726 | |||
727 | /** |
||
728 | * @Given /^(this product) belongs to ("([^"]+)" shipping category)$/ |
||
729 | * @Given product :product shipping category has been changed to :shippingCategory |
||
730 | */ |
||
731 | public function thisProductBelongsToShippingCategory(ProductInterface $product, ShippingCategoryInterface $shippingCategory) |
||
736 | |||
737 | /** |
||
738 | * @Given /^(this product) has been disabled$/ |
||
739 | */ |
||
740 | public function thisProductHasBeenDisabled(ProductInterface $product) |
||
745 | |||
746 | /** |
||
747 | * @Given the product :product was renamed to :productName |
||
748 | */ |
||
749 | public function theProductWasRenamedTo(ProductInterface $product, string $productName): void |
||
755 | |||
756 | /** |
||
757 | * @Given /^(this product) does not require shipping$/ |
||
758 | */ |
||
759 | public function thisProductDoesNotRequireShipping(ProductInterface $product): void |
||
768 | |||
769 | /** |
||
770 | * @Given product's :product code is :code |
||
771 | */ |
||
772 | public function productCodeIs(ProductInterface $product, string $code): void |
||
778 | |||
779 | /** |
||
780 | * @Given the product :product has height :height, width :width, depth :depth, weight :weight |
||
781 | */ |
||
782 | public function productHasDimensions(ProductInterface $product, float $height, float $width, float $depth, float $weight): void |
||
793 | |||
794 | /** |
||
795 | * @Given the product :product has the slug :slug |
||
796 | */ |
||
797 | public function productHasSlug(ProductInterface $product, string $slug): void |
||
803 | |||
804 | /** |
||
805 | * @Given the description of product :product is :description |
||
806 | */ |
||
807 | public function descriptionOfProductIs(ProductInterface $product, string $description): void |
||
813 | |||
814 | /** |
||
815 | * @Given the meta keywords of product :product is :metaKeywords |
||
816 | */ |
||
817 | public function metaKeywordsOfProductIs(ProductInterface $product, string $metaKeywords): void |
||
823 | |||
824 | /** |
||
825 | * @Given the short description of product :product is :shortDescription |
||
826 | */ |
||
827 | public function shortDescriptionOfProductIs(ProductInterface $product, string $shortDescription): void |
||
833 | |||
834 | /** |
||
835 | * @Given the product :product has original price :originalPrice |
||
836 | */ |
||
837 | public function theProductHasOriginalPrice(ProductInterface $product, string $originalPrice): void |
||
848 | |||
849 | /** |
||
850 | * @Given the product :product has option :productOption named :optionValue with code :optionCode |
||
851 | */ |
||
852 | public function productHasOption( |
||
868 | |||
869 | /** |
||
870 | * @Given the product :product has :productVariantName variant with code :code, price :price, current stock :currentStock |
||
871 | */ |
||
872 | public function productHasVariant(ProductInterface $product, string $productVariantName, string $code, string $price, int $currentStock): void |
||
880 | |||
881 | /** |
||
882 | * @Given the store has a product :productName in channel :channel |
||
883 | * @Given the store also has a product :productName in channel :channel |
||
884 | */ |
||
885 | public function theStoreHasAProductWithChannel(string $productName, ChannelInterface $channel): void |
||
891 | |||
892 | private function getPriceFromString(string $price): int |
||
896 | |||
897 | /** |
||
898 | * @param string $productName |
||
899 | * |
||
900 | * @return ProductInterface |
||
901 | */ |
||
902 | private function createProduct($productName, int $price = 100, ChannelInterface $channel = null) |
||
939 | |||
940 | /** |
||
941 | * @param string $value |
||
942 | * @param string $code |
||
943 | * |
||
944 | * @return ProductOptionValueInterface |
||
945 | */ |
||
946 | private function addProductOption(ProductOptionInterface $option, $value, $code) |
||
959 | |||
960 | private function saveProduct(ProductInterface $product) |
||
965 | |||
966 | /** |
||
967 | * @param string $name |
||
968 | * |
||
969 | * @return NodeElement |
||
970 | */ |
||
971 | private function getParameter($name) |
||
975 | |||
976 | /** |
||
977 | * @param string $productVariantName |
||
978 | * @param int $price |
||
979 | * @param string $code |
||
980 | * @param ChannelInterface $channel |
||
981 | * @param int $position |
||
982 | * @param bool $shippingRequired |
||
983 | * |
||
984 | * @return ProductVariantInterface |
||
985 | */ |
||
986 | private function createProductVariant( |
||
1016 | |||
1017 | /** |
||
1018 | * @param string $name |
||
1019 | * @param string $locale |
||
1020 | */ |
||
1021 | private function addProductTranslation(ProductInterface $product, $name, $locale) |
||
1036 | |||
1037 | /** |
||
1038 | * @param string $name |
||
1039 | * @param string $locale |
||
1040 | */ |
||
1041 | private function addProductVariantTranslation(ProductVariantInterface $productVariant, $name, $locale) |
||
1050 | |||
1051 | /** |
||
1052 | * @return ChannelPricingInterface |
||
1053 | */ |
||
1054 | private function createChannelPricingForChannel(int $price, ChannelInterface $channel = null) |
||
1063 | |||
1064 | private function addOptionToProduct(ProductInterface $product, string $optionName, array $values): void |
||
1085 | |||
1086 | private function createProductImage( |
||
1110 | } |
||
1111 |
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 mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.