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 |
||
| 45 | final class ProductContext implements Context |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var SharedStorageInterface |
||
| 49 | */ |
||
| 50 | private $sharedStorage; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var ProductRepositoryInterface |
||
| 54 | */ |
||
| 55 | private $productRepository; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ProductFactoryInterface |
||
| 59 | */ |
||
| 60 | private $productFactory; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var FactoryInterface |
||
| 64 | */ |
||
| 65 | private $productTranslationFactory; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var AttributeFactoryInterface |
||
| 69 | */ |
||
| 70 | private $productAttributeFactory; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var FactoryInterface |
||
| 74 | */ |
||
| 75 | private $productVariantFactory; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var FactoryInterface |
||
| 79 | */ |
||
| 80 | private $attributeValueFactory; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var FactoryInterface |
||
| 84 | */ |
||
| 85 | private $productOptionFactory; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var FactoryInterface |
||
| 89 | */ |
||
| 90 | private $productOptionValueFactory; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var FactoryInterface |
||
| 94 | */ |
||
| 95 | private $productImageFactory; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var ObjectManager |
||
| 99 | */ |
||
| 100 | private $objectManager; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var ProductVariantResolverInterface |
||
| 104 | */ |
||
| 105 | private $defaultVariantResolver; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var ImageUploaderInterface |
||
| 109 | */ |
||
| 110 | private $imageUploader; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var SlugGeneratorInterface |
||
| 114 | */ |
||
| 115 | private $slugGenerator; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | private $minkParameters; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param SharedStorageInterface $sharedStorage |
||
| 124 | * @param ProductRepositoryInterface $productRepository |
||
| 125 | * @param ProductFactoryInterface $productFactory |
||
| 126 | * @param FactoryInterface $productTranslationFactory |
||
| 127 | * @param AttributeFactoryInterface $productAttributeFactory |
||
| 128 | * @param FactoryInterface $attributeValueFactory |
||
| 129 | * @param FactoryInterface $productVariantFactory |
||
| 130 | * @param FactoryInterface $productOptionFactory |
||
| 131 | * @param FactoryInterface $productOptionValueFactory |
||
| 132 | * @param FactoryInterface $productImageFactory |
||
| 133 | * @param ObjectManager $objectManager |
||
| 134 | * @param ProductVariantResolverInterface $defaultVariantResolver |
||
| 135 | * @param ImageUploaderInterface $imageUploader |
||
| 136 | * @param SlugGeneratorInterface $slugGenerator |
||
| 137 | * @param array $minkParameters |
||
| 138 | */ |
||
| 139 | public function __construct( |
||
| 140 | SharedStorageInterface $sharedStorage, |
||
| 141 | ProductRepositoryInterface $productRepository, |
||
| 142 | ProductFactoryInterface $productFactory, |
||
| 143 | FactoryInterface $productTranslationFactory, |
||
| 144 | AttributeFactoryInterface $productAttributeFactory, |
||
| 145 | FactoryInterface $attributeValueFactory, |
||
| 146 | FactoryInterface $productVariantFactory, |
||
| 147 | FactoryInterface $productOptionFactory, |
||
| 148 | FactoryInterface $productOptionValueFactory, |
||
| 149 | FactoryInterface $productImageFactory, |
||
| 150 | ObjectManager $objectManager, |
||
| 151 | ProductVariantResolverInterface $defaultVariantResolver, |
||
| 152 | ImageUploaderInterface $imageUploader, |
||
| 153 | SlugGeneratorInterface $slugGenerator, |
||
| 154 | array $minkParameters |
||
| 155 | ) { |
||
| 156 | $this->sharedStorage = $sharedStorage; |
||
| 157 | $this->productRepository = $productRepository; |
||
| 158 | $this->productFactory = $productFactory; |
||
| 159 | $this->productTranslationFactory = $productTranslationFactory; |
||
| 160 | $this->productAttributeFactory = $productAttributeFactory; |
||
| 161 | $this->attributeValueFactory = $attributeValueFactory; |
||
| 162 | $this->productVariantFactory = $productVariantFactory; |
||
| 163 | $this->productOptionFactory = $productOptionFactory; |
||
| 164 | $this->productOptionValueFactory = $productOptionValueFactory; |
||
| 165 | $this->productImageFactory = $productImageFactory; |
||
| 166 | $this->objectManager = $objectManager; |
||
| 167 | $this->defaultVariantResolver = $defaultVariantResolver; |
||
| 168 | $this->imageUploader = $imageUploader; |
||
| 169 | $this->slugGenerator = $slugGenerator; |
||
| 170 | $this->minkParameters = $minkParameters; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @Given the store has a product :productName |
||
| 175 | * @Given the store has a :productName product |
||
| 176 | * @Given I added a product :productName |
||
| 177 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+")$/ |
||
| 178 | */ |
||
| 179 | public function storeHasAProductPricedAt($productName, $price = 0) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @Given the store( also) has a product :productName with code :code |
||
| 194 | * @Given the store( also) has a product :productName with code :code, created at :date |
||
| 195 | */ |
||
| 196 | public function storeHasProductWithCode($productName, $code, $date = null) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") available in (channel "[^"]+") and (channel "[^"]+")$/ |
||
| 211 | */ |
||
| 212 | public function storeHasAProductPricedAtAvailableInChannels($productName, $price = 0, ...$channels) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @Given /^(this product) is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
| 227 | * @Given /^the (product "[^"]+") is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
| 228 | */ |
||
| 229 | public function thisProductIsNamedIn(ProductInterface $product, $name, $locale) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @Given the store has a :productName configurable product |
||
| 244 | */ |
||
| 245 | public function storeHasAConfigurableProduct($productName) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @Given the store has( also) :firstProductName and :secondProductName products |
||
| 266 | * @Given the store has( also) :firstProductName, :secondProductName and :thirdProductName products |
||
| 267 | * @Given the store has( also) :firstProductName, :secondProductName, :thirdProductName and :fourthProductName products |
||
| 268 | */ |
||
| 269 | public function theStoreHasProducts(...$productsNames) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @Given /^(this channel) has "([^"]+)", "([^"]+)", "([^"]+)" and "([^"]+)" products$/ |
||
| 278 | */ |
||
| 279 | public function thisChannelHasProducts(ChannelInterface $channel, ...$productsNames) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @Given /^the (product "[^"]+") has(?:| a) "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 291 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 292 | */ |
||
| 293 | public function theProductHasVariantPricedAt(ProductInterface $product, $productVariantName, $price) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") identified by "([^"]+)"$/ |
||
| 305 | */ |
||
| 306 | public function theProductHasVariantPricedAtIdentifiedBy( |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ |
||
| 317 | * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/ |
||
| 318 | */ |
||
| 319 | public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/ |
||
| 331 | */ |
||
| 332 | public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @Given /^(it) comes in the following variations:$/ |
||
| 343 | */ |
||
| 344 | public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/ |
||
| 362 | */ |
||
| 363 | public function productVariantBelongsToTaxCategory( |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with value "([^"]+)"$/ |
||
| 373 | */ |
||
| 374 | public function thisProductHasAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @Given /^(this product) has percent attribute "([^"]+)" with value ([^"]+)%$/ |
||
| 385 | */ |
||
| 386 | public function thisProductHasPercentAttributeWithValue(ProductInterface $product, $productAttributeName, $value) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" set to "([^"]+)"$/ |
||
| 397 | */ |
||
| 398 | public function thisProductHasCheckboxAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @Given /^(this product) has percent attribute "([^"]+)" at position (\d+)$/ |
||
| 410 | */ |
||
| 411 | public function thisProductHasPercentAttributeWithValueAtPosition(ProductInterface $product, $productAttributeName, $position) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with date "([^"]+)"$/ |
||
| 424 | */ |
||
| 425 | public function thisProductHasDateTimeAttributeWithDate(ProductInterface $product, $productAttributeType, $productAttributeName, $date) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/ |
||
| 437 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/ |
||
| 438 | */ |
||
| 439 | public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/ |
||
| 463 | * @When product :product quantity is changed to :quantity |
||
| 464 | */ |
||
| 465 | public function thereIsQuantityOfProducts($quantity, ProductInterface $product) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @Given /^the (product "([^"]+)") is out of stock$/ |
||
| 476 | */ |
||
| 477 | public function theProductIsOutOfStock(ProductInterface $product) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @When other customer has bought :quantity :product products by this time |
||
| 489 | */ |
||
| 490 | public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @Given /^(this product) is tracked by the inventory$/ |
||
| 501 | * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/ |
||
| 502 | */ |
||
| 503 | public function thisProductIsTrackedByTheInventory(ProductInterface $product) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/ |
||
| 514 | */ |
||
| 515 | public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, $price) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @Given /^(this product) has (this product option)$/ |
||
| 532 | * @Given /^(this product) has a ("[^"]+" option)$/ |
||
| 533 | * @Given /^(this product) has an ("[^"]+" option)$/ |
||
| 534 | */ |
||
| 535 | public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/ |
||
| 544 | */ |
||
| 545 | public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/ |
||
| 555 | */ |
||
| 556 | public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @Given /^(this product)'s price is ("[^"]+")$/ |
||
| 565 | * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/ |
||
| 566 | */ |
||
| 567 | public function theProductChangedItsPriceTo(ProductInterface $product, $price) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @Given /^(this product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
| 578 | * @Given /^the ("[^"]+" product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
| 579 | */ |
||
| 580 | public function thisProductHasAnImageWithACode(ProductInterface $product, $imagePath, $imageCode) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @Given /^(it) has different prices for different channels and currencies$/ |
||
| 597 | */ |
||
| 598 | public function itHasDifferentPricesForDifferentChannelsAndCurrencies(ProductInterface $product) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @Given /^(it) has price ("[^"]+") for ("[^"]+" channel) and "([^"]+)" currency$/ |
||
| 608 | */ |
||
| 609 | public function itHasPriceForChannelAndCurrency( |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @param string $type |
||
| 628 | * @param string $name |
||
| 629 | * @param string|null $code |
||
| 630 | * |
||
| 631 | * @return ProductAttributeInterface |
||
| 632 | */ |
||
| 633 | private function createProductAttribute($type, $name, $code = null) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param string $value |
||
| 651 | * @param ProductAttributeInterface $attribute |
||
| 652 | * |
||
| 653 | * @return ProductAttributeValueInterface |
||
| 654 | */ |
||
| 655 | private function createProductAttributeValue($value, ProductAttributeInterface $attribute) |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @param string $price |
||
| 669 | * |
||
| 670 | * @return int |
||
| 671 | */ |
||
| 672 | private function getPriceFromString($price) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @param string $productName |
||
| 679 | * @param int $price |
||
| 680 | * @param string $date |
||
| 681 | * |
||
| 682 | * @return ProductInterface |
||
| 683 | */ |
||
| 684 | private function createProduct($productName, $price = 0, $date = null) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * @param ProductOptionInterface $option |
||
| 704 | * @param string $value |
||
| 705 | * @param string $code |
||
| 706 | * |
||
| 707 | * @return ProductOptionValueInterface |
||
| 708 | */ |
||
| 709 | private function addProductOption(ProductOptionInterface $option, $value, $code) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * @param ProductInterface $product |
||
| 725 | */ |
||
| 726 | private function saveProduct(ProductInterface $product) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * @param string $name |
||
| 734 | * |
||
| 735 | * @return NodeElement |
||
| 736 | */ |
||
| 737 | private function getParameter($name) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * @param ProductInterface $product |
||
| 744 | * @param $productVariantName |
||
| 745 | * @param int $price |
||
| 746 | * @param string $code |
||
| 747 | */ |
||
| 748 | private function createProductVariant(ProductInterface $product, $productVariantName, $price, $code) |
||
| 765 | } |
||
| 766 |
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: