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 |
||
| 43 | final class ProductContext implements Context |
||
| 44 | { |
||
| 45 | /** @var SharedStorageInterface */ |
||
| 46 | private $sharedStorage; |
||
| 47 | |||
| 48 | /** @var ProductRepositoryInterface */ |
||
| 49 | private $productRepository; |
||
| 50 | |||
| 51 | /** @var ProductFactoryInterface */ |
||
| 52 | private $productFactory; |
||
| 53 | |||
| 54 | /** @var FactoryInterface */ |
||
| 55 | private $productTranslationFactory; |
||
| 56 | |||
| 57 | /** @var FactoryInterface */ |
||
| 58 | private $productVariantFactory; |
||
| 59 | |||
| 60 | /** @var FactoryInterface */ |
||
| 61 | private $productVariantTranslationFactory; |
||
| 62 | |||
| 63 | /** @var FactoryInterface */ |
||
| 64 | private $channelPricingFactory; |
||
| 65 | |||
| 66 | /** @var FactoryInterface */ |
||
| 67 | private $productOptionFactory; |
||
| 68 | |||
| 69 | /** @var FactoryInterface */ |
||
| 70 | private $productOptionValueFactory; |
||
| 71 | |||
| 72 | /** @var FactoryInterface */ |
||
| 73 | private $productImageFactory; |
||
| 74 | |||
| 75 | /** @var ObjectManager */ |
||
| 76 | private $objectManager; |
||
| 77 | |||
| 78 | /** @var ProductVariantGeneratorInterface */ |
||
| 79 | private $productVariantGenerator; |
||
| 80 | |||
| 81 | /** @var ProductVariantResolverInterface */ |
||
| 82 | private $defaultVariantResolver; |
||
| 83 | |||
| 84 | /** @var ImageUploaderInterface */ |
||
| 85 | private $imageUploader; |
||
| 86 | |||
| 87 | /** @var SlugGeneratorInterface */ |
||
| 88 | private $slugGenerator; |
||
| 89 | |||
| 90 | /** @var array */ |
||
| 91 | private $minkParameters; |
||
| 92 | |||
| 93 | public function __construct( |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @Given the store has a product :productName |
||
| 131 | * @Given the store has a :productName product |
||
| 132 | * @Given I added a product :productName |
||
| 133 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+")$/ |
||
| 134 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") in ("[^"]+" channel)$/ |
||
| 135 | */ |
||
| 136 | public function storeHasAProductPricedAt($productName, int $price = 100, ChannelInterface $channel = null) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @Given /^(this product) is(?:| also) priced at ("[^"]+") in ("[^"]+" channel)$/ |
||
| 145 | */ |
||
| 146 | public function thisProductIsAlsoPricedAtInChannel(ProductInterface $product, int $price, ChannelInterface $channel) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @Given /^(this product) is(?:| also) available in ("[^"]+" channel)$/ |
||
| 159 | */ |
||
| 160 | public function thisProductIsAlsoAvailableInChannel(ProductInterface $product, ChannelInterface $channel): void |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @Given the store( also) has a product :productName with code :code |
||
| 167 | * @Given the store( also) has a product :productName with code :code, created at :date |
||
| 168 | */ |
||
| 169 | public function storeHasProductWithCode($productName, $code, $date = 'now') |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") available in (channel "[^"]+") and (channel "[^"]+")$/ |
||
| 180 | */ |
||
| 181 | public function storeHasAProductPricedAtAvailableInChannels($productName, int $price = 100, ...$channels) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @Given /^(this product) is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
| 199 | * @Given /^the (product "[^"]+") is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
| 200 | */ |
||
| 201 | public function thisProductIsNamedIn(ProductInterface $product, $name, $locale) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @Given /^the store has a product named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
||
| 210 | */ |
||
| 211 | public function theStoreHasProductNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @Given /^the store has(?:| a| an) "([^"]+)" configurable product$/ |
||
| 225 | * @Given /^the store has(?:| a| an) "([^"]+)" configurable product with "([^"]+)" slug$/ |
||
| 226 | */ |
||
| 227 | public function storeHasAConfigurableProduct($productName, $slug = null) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @Given the store has( also) :firstProductName and :secondProductName products |
||
| 256 | * @Given the store has( also) :firstProductName, :secondProductName and :thirdProductName products |
||
| 257 | * @Given the store has( also) :firstProductName, :secondProductName, :thirdProductName and :fourthProductName products |
||
| 258 | */ |
||
| 259 | public function theStoreHasProducts(...$productsNames) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @Given /^(this channel) has "([^"]+)", "([^"]+)", "([^"]+)" and "([^"]+)" products$/ |
||
| 268 | */ |
||
| 269 | public function thisChannelHasProducts(ChannelInterface $channel, ...$productsNames) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @Given /^the (product "[^"]+") has(?:| a) "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 280 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 281 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") in ("([^"]+)" channel)$/ |
||
| 282 | */ |
||
| 283 | public function theProductHasVariantPricedAt( |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @Given /^the (product "[^"]+") has(?:| a| an) "([^"]+)" variant$/ |
||
| 300 | * @Given /^(this product) has(?:| a| an) "([^"]+)" variant$/ |
||
| 301 | * @Given /^(this product) has "([^"]+)", "([^"]+)" and "([^"]+)" variants$/ |
||
| 302 | */ |
||
| 303 | public function theProductHasVariants(ProductInterface $product, ...$variantNames) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @Given /^the (product "[^"]+")(?:| also) has a nameless variant with code "([^"]+)"$/ |
||
| 320 | * @Given /^(this product)(?:| also) has a nameless variant with code "([^"]+)"$/ |
||
| 321 | * @Given /^(it)(?:| also) has a nameless variant with code "([^"]+)"$/ |
||
| 322 | */ |
||
| 323 | public function theProductHasNamelessVariantWithCode(ProductInterface $product, $variantCode) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @Given /^the (product "[^"]+")(?:| also) has(?:| a| an) "([^"]+)" variant with code "([^"]+)"$/ |
||
| 332 | * @Given /^(this product)(?:| also) has(?:| a| an) "([^"]+)" variant with code "([^"]+)"$/ |
||
| 333 | * @Given /^(it)(?:| also) has(?:| a| an) "([^"]+)" variant with code "([^"]+)"$/ |
||
| 334 | */ |
||
| 335 | public function theProductHasVariantWithCode(ProductInterface $product, $variantName, $variantCode) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") which does not require shipping$/ |
||
| 344 | */ |
||
| 345 | public function theProductHasVariantWhichDoesNotRequireShipping( |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @Given /^the (product "[^"]+") has(?:| also)(?:| a| an) "([^"]+)" variant$/ |
||
| 363 | * @Given /^the (product "[^"]+") has(?:| also)(?:| a| an) "([^"]+)" variant at position ([^"]+)$/ |
||
| 364 | * @Given /^(this product) has(?:| also)(?:| a| an) "([^"]+)" variant at position ([^"]+)$/ |
||
| 365 | */ |
||
| 366 | public function theProductHasVariantAtPosition( |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @Given /^(this variant) is also priced at ("[^"]+") in ("([^"]+)" channel)$/ |
||
| 383 | */ |
||
| 384 | public function thisVariantIsAlsoPricedAtInChannel(ProductVariantInterface $productVariant, string $price, ChannelInterface $channel) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Given /^(it|this product) has(?:| also) variant named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
||
| 396 | */ |
||
| 397 | public function itHasVariantNamedInAndIn(ProductInterface $product, $firstName, $firstLocale, $secondName, $secondLocale) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") identified by "([^"]+)"$/ |
||
| 417 | */ |
||
| 418 | public function theProductHasVariantPricedAtIdentifiedBy( |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @Given /^(this product) only variant was renamed to "([^"]+)"$/ |
||
| 429 | */ |
||
| 430 | public function productOnlyVariantWasRenamed(ProductInterface $product, $variantName) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ |
||
| 443 | * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/ |
||
| 444 | */ |
||
| 445 | public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/ |
||
| 454 | */ |
||
| 455 | public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @Given /^(it) comes in the following variations:$/ |
||
| 466 | */ |
||
| 467 | public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/ |
||
| 491 | */ |
||
| 492 | public function productVariantBelongsToTaxCategory( |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/ |
||
| 504 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/ |
||
| 505 | */ |
||
| 506 | public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/ |
||
| 530 | */ |
||
| 531 | public function thereIsQuantityOfProducts($quantity, ProductInterface $product) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @Given /^the (product "([^"]+)") is out of stock$/ |
||
| 542 | */ |
||
| 543 | public function theProductIsOutOfStock(ProductInterface $product) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @When other customer has bought :quantity :product products by this time |
||
| 555 | */ |
||
| 556 | public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @Given /^(this product) is tracked by the inventory$/ |
||
| 567 | * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/ |
||
| 568 | */ |
||
| 569 | public function thisProductIsTrackedByTheInventory(ProductInterface $product) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/ |
||
| 580 | */ |
||
| 581 | public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, int $price) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @Given the :product product's :optionValueName size belongs to :shippingCategory shipping category |
||
| 599 | */ |
||
| 600 | public function thisProductSizeBelongsToShippingCategory(ProductInterface $product, $optionValueName, ShippingCategoryInterface $shippingCategory) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @Given /^(this product) has (this product option)$/ |
||
| 616 | * @Given /^(this product) has (?:a|an) ("[^"]+" option)$/ |
||
| 617 | */ |
||
| 618 | public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @Given /^(this product) has all possible variants$/ |
||
| 627 | */ |
||
| 628 | public function thisProductHasAllPossibleVariants(ProductInterface $product) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/ |
||
| 660 | */ |
||
| 661 | public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/ |
||
| 671 | */ |
||
| 672 | public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * @Given /^(this product)'s price is ("[^"]+")$/ |
||
| 681 | * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/ |
||
| 682 | * @Given /^(this product) price has been changed to ("[^"]+")$/ |
||
| 683 | */ |
||
| 684 | public function theProductChangedItsPriceTo(ProductInterface $product, int $price) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @Given /^(this product)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
||
| 696 | * @Given /^the ("[^"]+" product)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
||
| 697 | * @Given /^(it)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
||
| 698 | */ |
||
| 699 | public function thisProductHasAnImageWithType(ProductInterface $product, $imagePath, $imageType) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @Given /^(this product) belongs to ("([^"]+)" shipping category)$/ |
||
| 717 | * @Given product :product shipping category has been changed to :shippingCategory |
||
| 718 | */ |
||
| 719 | public function thisProductBelongsToShippingCategory(ProductInterface $product, ShippingCategoryInterface $shippingCategory) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @Given /^(this product) has been disabled$/ |
||
| 727 | */ |
||
| 728 | public function thisProductHasBeenDisabled(ProductInterface $product) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @Given the product :product was renamed to :productName |
||
| 736 | */ |
||
| 737 | public function theProductWasRenamedTo(ProductInterface $product, string $productName): void |
||
| 743 | |||
| 744 | /** |
||
| 745 | * @Given /^(this product) does not require shipping$/ |
||
| 746 | */ |
||
| 747 | public function thisProductDoesNotRequireShipping(ProductInterface $product): void |
||
| 756 | |||
| 757 | private function getPriceFromString(string $price): int |
||
| 761 | |||
| 762 | /** |
||
| 763 | * @param string $productName |
||
| 764 | * |
||
| 765 | * @return ProductInterface |
||
| 766 | */ |
||
| 767 | private function createProduct($productName, int $price = 100, ChannelInterface $channel = null) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @param string $value |
||
| 807 | * @param string $code |
||
| 808 | * |
||
| 809 | * @return ProductOptionValueInterface |
||
| 810 | */ |
||
| 811 | private function addProductOption(ProductOptionInterface $option, $value, $code) |
||
| 824 | |||
| 825 | private function saveProduct(ProductInterface $product) |
||
| 830 | |||
| 831 | /** |
||
| 832 | * @param string $name |
||
| 833 | * |
||
| 834 | * @return NodeElement |
||
| 835 | */ |
||
| 836 | private function getParameter($name) |
||
| 840 | |||
| 841 | /** |
||
| 842 | * @param string $productVariantName |
||
| 843 | * @param int $price |
||
| 844 | * @param string $code |
||
| 845 | * @param ChannelInterface $channel |
||
|
|
|||
| 846 | * @param int $position |
||
| 847 | * @param bool $shippingRequired |
||
| 848 | * |
||
| 849 | * @return ProductVariantInterface |
||
| 850 | */ |
||
| 851 | private function createProductVariant( |
||
| 879 | |||
| 880 | /** |
||
| 881 | * @param string $name |
||
| 882 | * @param string $locale |
||
| 883 | */ |
||
| 884 | private function addProductTranslation(ProductInterface $product, $name, $locale) |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @param string $name |
||
| 902 | * @param string $locale |
||
| 903 | */ |
||
| 904 | private function addProductVariantTranslation(ProductVariantInterface $productVariant, $name, $locale) |
||
| 913 | |||
| 914 | /** |
||
| 915 | * @return ChannelPricingInterface |
||
| 916 | */ |
||
| 917 | private function createChannelPricingForChannel(int $price, ChannelInterface $channel = null) |
||
| 926 | } |
||
| 927 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.