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 |
||
| 50 | final class ProductContext implements Context |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * @var SharedStorageInterface |
||
| 54 | */ |
||
| 55 | private $sharedStorage; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ProductRepositoryInterface |
||
| 59 | */ |
||
| 60 | private $productRepository; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var ProductFactoryInterface |
||
| 64 | */ |
||
| 65 | private $productFactory; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var FactoryInterface |
||
| 69 | */ |
||
| 70 | private $productTranslationFactory; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var AttributeFactoryInterface |
||
| 74 | */ |
||
| 75 | private $productAttributeFactory; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var FactoryInterface |
||
| 79 | */ |
||
| 80 | private $productVariantFactory; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var FactoryInterface |
||
| 84 | */ |
||
| 85 | private $productVariantTranslationFactory; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var FactoryInterface |
||
| 89 | */ |
||
| 90 | private $channelPricingFactory; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var FactoryInterface |
||
| 94 | */ |
||
| 95 | private $attributeValueFactory; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var FactoryInterface |
||
| 99 | */ |
||
| 100 | private $productOptionFactory; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var FactoryInterface |
||
| 104 | */ |
||
| 105 | private $productOptionValueFactory; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var FactoryInterface |
||
| 109 | */ |
||
| 110 | private $productImageFactory; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var ObjectManager |
||
| 114 | */ |
||
| 115 | private $objectManager; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var ProductVariantResolverInterface |
||
| 119 | */ |
||
| 120 | private $defaultVariantResolver; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var ImageUploaderInterface |
||
| 124 | */ |
||
| 125 | private $imageUploader; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var SlugGeneratorInterface |
||
| 129 | */ |
||
| 130 | private $slugGenerator; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | private $minkParameters; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param SharedStorageInterface $sharedStorage |
||
| 139 | * @param ProductRepositoryInterface $productRepository |
||
| 140 | * @param ProductFactoryInterface $productFactory |
||
| 141 | * @param FactoryInterface $productTranslationFactory |
||
| 142 | * @param AttributeFactoryInterface $productAttributeFactory |
||
| 143 | * @param FactoryInterface $attributeValueFactory |
||
| 144 | * @param FactoryInterface $productVariantFactory |
||
| 145 | * @param FactoryInterface $productVariantTranslationFactory |
||
| 146 | * @param FactoryInterface $channelPricingFactory |
||
| 147 | * @param FactoryInterface $productOptionFactory |
||
| 148 | * @param FactoryInterface $productOptionValueFactory |
||
| 149 | * @param FactoryInterface $productImageFactory |
||
| 150 | * @param ObjectManager $objectManager |
||
| 151 | * @param ProductVariantResolverInterface $defaultVariantResolver |
||
| 152 | * @param ImageUploaderInterface $imageUploader |
||
| 153 | * @param SlugGeneratorInterface $slugGenerator |
||
| 154 | * @param array $minkParameters |
||
| 155 | */ |
||
| 156 | public function __construct( |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @Given the store has a product :productName |
||
| 196 | * @Given the store has a :productName product |
||
| 197 | * @Given I added a product :productName |
||
| 198 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+")$/ |
||
| 199 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") in ("[^"]+" channel)$/ |
||
| 200 | */ |
||
| 201 | public function storeHasAProductPricedAt($productName, $price = 100, ChannelInterface $channel = null) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @Given /^(this product) is also priced at ("[^"]+") in ("[^"]+" channel)$/ |
||
| 218 | */ |
||
| 219 | public function thisProductIsAlsoPricedAtInChannel(ProductInterface $product, $price, ChannelInterface $channel) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @Given the store( also) has a product :productName with code :code |
||
| 238 | * @Given the store( also) has a product :productName with code :code, created at :date |
||
| 239 | */ |
||
| 240 | public function storeHasProductWithCode($productName, $code, $date = null) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") available in (channel "[^"]+") and (channel "[^"]+")$/ |
||
| 255 | */ |
||
| 256 | public function storeHasAProductPricedAtAvailableInChannels($productName, $price = 100, ...$channels) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @Given /^(this product) is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
| 276 | * @Given /^the (product "[^"]+") is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
| 277 | */ |
||
| 278 | public function thisProductIsNamedIn(ProductInterface $product, $name, $locale) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @Given /^the store has a product named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
||
| 287 | */ |
||
| 288 | public function theStoreHasProductNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @Given the store has a :productName configurable product |
||
| 302 | */ |
||
| 303 | public function storeHasAConfigurableProduct($productName) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @Given the store has( also) :firstProductName and :secondProductName products |
||
| 324 | * @Given the store has( also) :firstProductName, :secondProductName and :thirdProductName products |
||
| 325 | * @Given the store has( also) :firstProductName, :secondProductName, :thirdProductName and :fourthProductName products |
||
| 326 | */ |
||
| 327 | public function theStoreHasProducts(...$productsNames) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @Given /^(this channel) has "([^"]+)", "([^"]+)", "([^"]+)" and "([^"]+)" products$/ |
||
| 336 | */ |
||
| 337 | public function thisChannelHasProducts(ChannelInterface $channel, ...$productsNames) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @Given /^the (product "[^"]+") has(?:| a) "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 349 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 350 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") in ("[^"]+" channel)$/ |
||
| 351 | */ |
||
| 352 | public function theProductHasVariantPricedAt( |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Given /^(it|this product) has(?:| also) variant named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
||
| 369 | */ |
||
| 370 | public function itHasVariantNamedInAndIn(ProductInterface $product, $firstName, $firstLocale, $secondName, $secondLocale) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") identified by "([^"]+)"$/ |
||
| 390 | */ |
||
| 391 | public function theProductHasVariantPricedAtIdentifiedBy( |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ |
||
| 402 | * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/ |
||
| 403 | */ |
||
| 404 | public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/ |
||
| 416 | */ |
||
| 417 | public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @Given /^(it) comes in the following variations:$/ |
||
| 428 | */ |
||
| 429 | public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/ |
||
| 450 | */ |
||
| 451 | public function productVariantBelongsToTaxCategory( |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with value "([^"]+)"$/ |
||
| 461 | */ |
||
| 462 | public function thisProductHasAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @Given /^(this product) has percent attribute "([^"]+)" with value ([^"]+)%$/ |
||
| 473 | */ |
||
| 474 | public function thisProductHasPercentAttributeWithValue(ProductInterface $product, $productAttributeName, $value) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" set to "([^"]+)"$/ |
||
| 485 | */ |
||
| 486 | public function thisProductHasCheckboxAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @Given /^(this product) has percent attribute "([^"]+)" at position (\d+)$/ |
||
| 498 | */ |
||
| 499 | public function thisProductHasPercentAttributeWithValueAtPosition(ProductInterface $product, $productAttributeName, $position) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with date "([^"]+)"$/ |
||
| 512 | */ |
||
| 513 | public function thisProductHasDateTimeAttributeWithDate(ProductInterface $product, $productAttributeType, $productAttributeName, $date) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/ |
||
| 525 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/ |
||
| 526 | */ |
||
| 527 | public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/ |
||
| 551 | * @When product :product quantity is changed to :quantity |
||
| 552 | */ |
||
| 553 | public function thereIsQuantityOfProducts($quantity, ProductInterface $product) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @Given /^the (product "([^"]+)") is out of stock$/ |
||
| 564 | */ |
||
| 565 | public function theProductIsOutOfStock(ProductInterface $product) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @When other customer has bought :quantity :product products by this time |
||
| 577 | */ |
||
| 578 | public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @Given /^(this product) is tracked by the inventory$/ |
||
| 589 | * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/ |
||
| 590 | */ |
||
| 591 | public function thisProductIsTrackedByTheInventory(ProductInterface $product) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/ |
||
| 602 | */ |
||
| 603 | public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, $price) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @Given the :product product's :optionValueName size belongs to :shippingCategory shipping category |
||
| 621 | * @Given /^(this product) "([^"]+)" size belongs to ("([^"]+)" shipping category)$/ |
||
| 622 | */ |
||
| 623 | public function thisProductSizeBelongsToShippingCategory(ProductInterface $product, $optionValueName, ShippingCategoryInterface $shippingCategory) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @Given /^(this product) has (this product option)$/ |
||
| 639 | * @Given /^(this product) has a ("[^"]+" option)$/ |
||
| 640 | * @Given /^(this product) has an ("[^"]+" option)$/ |
||
| 641 | */ |
||
| 642 | public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/ |
||
| 651 | */ |
||
| 652 | public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/ |
||
| 662 | */ |
||
| 663 | public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @Given /^(this product)'s price is ("[^"]+")$/ |
||
| 672 | * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/ |
||
| 673 | */ |
||
| 674 | public function theProductChangedItsPriceTo(ProductInterface $product, $price) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @Given /^(this product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
| 686 | * @Given /^the ("[^"]+" product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
| 687 | */ |
||
| 688 | public function thisProductHasAnImageWithACode(ProductInterface $product, $imagePath, $imageCode) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @Given /^(it) has different prices for different channels and currencies$/ |
||
| 705 | */ |
||
| 706 | public function itHasDifferentPricesForDifferentChannelsAndCurrencies(ProductInterface $product) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @Given /^(it) has price ("[^"]+") for ("[^"]+" channel) and "([^"]+)" currency$/ |
||
| 716 | */ |
||
| 717 | public function itHasPriceForChannelAndCurrency( |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @Given /^(this product) belongs to ("([^"]+)" shipping category)$/ |
||
| 736 | */ |
||
| 737 | public function thisProductBelongsToShippingCategory(ProductInterface $product, ShippingCategoryInterface $shippingCategory) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @param string $type |
||
| 745 | * @param string $name |
||
| 746 | * @param string|null $code |
||
| 747 | * |
||
| 748 | * @return ProductAttributeInterface |
||
| 749 | */ |
||
| 750 | private function createProductAttribute($type, $name, $code = null) |
||
| 765 | |||
| 766 | /** |
||
| 767 | * @param string $value |
||
| 768 | * @param ProductAttributeInterface $attribute |
||
| 769 | * |
||
| 770 | * @return ProductAttributeValueInterface |
||
| 771 | */ |
||
| 772 | private function createProductAttributeValue($value, ProductAttributeInterface $attribute) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * @param string $price |
||
| 786 | * |
||
| 787 | * @return int |
||
| 788 | */ |
||
| 789 | private function getPriceFromString($price) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * @param string $productName |
||
| 796 | * @param int $price |
||
| 797 | * @param string|null $date |
||
| 798 | * @param ChannelInterface|null $channel |
||
| 799 | * |
||
| 800 | * @return ProductInterface |
||
| 801 | */ |
||
| 802 | private function createProduct($productName, $price = 100, $date = null, ChannelInterface $channel = null) |
||
| 827 | |||
| 828 | /** |
||
| 829 | * @param ProductOptionInterface $option |
||
| 830 | * @param string $value |
||
| 831 | * @param string $code |
||
| 832 | * |
||
| 833 | * @return ProductOptionValueInterface |
||
| 834 | */ |
||
| 835 | private function addProductOption(ProductOptionInterface $option, $value, $code) |
||
| 848 | |||
| 849 | /** |
||
| 850 | * @param ProductInterface $product |
||
| 851 | */ |
||
| 852 | private function saveProduct(ProductInterface $product) |
||
| 857 | |||
| 858 | /** |
||
| 859 | * @param string $name |
||
| 860 | * |
||
| 861 | * @return NodeElement |
||
| 862 | */ |
||
| 863 | private function getParameter($name) |
||
| 867 | |||
| 868 | /** |
||
| 869 | * @param ProductInterface $product |
||
| 870 | * @param $productVariantName |
||
| 871 | * @param int $price |
||
| 872 | * @param string $code |
||
| 873 | * @param ChannelInterface $channel |
||
| 874 | * |
||
| 875 | * @return ProductVariantInterface |
||
| 876 | */ |
||
| 877 | private function createProductVariant( |
||
| 901 | |||
| 902 | /** |
||
| 903 | * @param ProductInterface $product |
||
| 904 | * @param string $name |
||
| 905 | * @param string $locale |
||
| 906 | */ |
||
| 907 | private function addProductTranslation(ProductInterface $product, $name, $locale) |
||
| 917 | |||
| 918 | /** |
||
| 919 | * @param ProductVariantInterface $productVariant |
||
| 920 | * @param string $name |
||
| 921 | * @param string $locale |
||
| 922 | */ |
||
| 923 | private function addProductVariantTranslation(ProductVariantInterface $productVariant, $name, $locale) |
||
| 932 | |||
| 933 | /** |
||
| 934 | * @param int $price |
||
| 935 | * @param ChannelInterface|null $channel |
||
| 936 | * |
||
| 937 | * @return ChannelPricingInterface |
||
| 938 | */ |
||
| 939 | private function createChannelPricingForChannel($price, ChannelInterface $channel = null) |
||
| 948 | } |
||
| 949 |
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: