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 ([^"]+) attribute "([^"]+)" with date "([^"]+)"$/ |
||
| 410 | */ |
||
| 411 | public function thisProductHasDateTimeAttributeWithDate(ProductInterface $product, $productAttributeType, $productAttributeName, $date) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/ |
||
| 423 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/ |
||
| 424 | */ |
||
| 425 | public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/ |
||
| 449 | * @When product :product quantity is changed to :quantity |
||
| 450 | */ |
||
| 451 | public function thereIsQuantityOfProducts($quantity, ProductInterface $product) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @Given /^the (product "([^"]+)") is out of stock$/ |
||
| 462 | */ |
||
| 463 | public function theProductIsOutOfStock(ProductInterface $product) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @When other customer has bought :quantity :product products by this time |
||
| 475 | */ |
||
| 476 | public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @Given /^(this product) is tracked by the inventory$/ |
||
| 487 | * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/ |
||
| 488 | */ |
||
| 489 | public function thisProductIsTrackedByTheInventory(ProductInterface $product) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/ |
||
| 500 | */ |
||
| 501 | public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, $price) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @Given /^(this product) has (this product option)$/ |
||
| 518 | * @Given /^(this product) has a ("[^"]+" option)$/ |
||
| 519 | * @Given /^(this product) has an ("[^"]+" option)$/ |
||
| 520 | */ |
||
| 521 | public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/ |
||
| 530 | */ |
||
| 531 | public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/ |
||
| 541 | */ |
||
| 542 | public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @Given /^(this product)'s price is ("[^"]+")$/ |
||
| 551 | * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/ |
||
| 552 | */ |
||
| 553 | public function theProductChangedItsPriceTo(ProductInterface $product, $price) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @Given /^(this product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
| 564 | * @Given /^the ("[^"]+" product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
| 565 | */ |
||
| 566 | public function thisProductHasAnImageWithACode(ProductInterface $product, $imagePath, $imageCode) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @Given /^(it) has different prices for different channels and currencies$/ |
||
| 583 | */ |
||
| 584 | public function itHasDifferentPricesForDifferentChannelsAndCurrencies(ProductInterface $product) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @Given /^(it) has price ("[^"]+") for ("[^"]+" channel) and "([^"]+)" currency$/ |
||
| 594 | */ |
||
| 595 | public function itHasPriceForChannelAndCurrency( |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @param string $type |
||
| 614 | * @param string $name |
||
| 615 | * @param string $code |
||
| 616 | * |
||
| 617 | * @return ProductAttributeInterface |
||
| 618 | */ |
||
| 619 | private function createProductAttribute($type, $name, $code = 'PA112') |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @param string $value |
||
| 632 | * |
||
| 633 | * @return ProductAttributeValueInterface |
||
| 634 | */ |
||
| 635 | private function createProductAttributeValue($value, ProductAttributeInterface $attribute) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @param string $price |
||
| 649 | * |
||
| 650 | * @return int |
||
| 651 | */ |
||
| 652 | private function getPriceFromString($price) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @param string $productName |
||
| 659 | * @param int $price |
||
| 660 | * |
||
| 661 | * @return ProductInterface |
||
| 662 | */ |
||
| 663 | private function createProduct($productName, $price = 0, $date = null) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @param ProductOptionInterface $option |
||
| 683 | * @param string $value |
||
| 684 | * @param string $code |
||
| 685 | * |
||
| 686 | * @return ProductOptionValueInterface |
||
| 687 | */ |
||
| 688 | private function addProductOption(ProductOptionInterface $option, $value, $code) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * @param ProductInterface $product |
||
| 704 | */ |
||
| 705 | private function saveProduct(ProductInterface $product) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * @param string $name |
||
| 713 | * |
||
| 714 | * @return NodeElement |
||
| 715 | */ |
||
| 716 | private function getParameter($name) |
||
| 720 | |||
| 721 | /** |
||
| 722 | * @param ProductInterface $product |
||
| 723 | * @param $productVariantName |
||
| 724 | * @param int $price |
||
| 725 | * @param string $code |
||
| 726 | */ |
||
| 727 | private function createProductVariant(ProductInterface $product, $productVariantName, $price, $code) |
||
| 744 | } |
||
| 745 |
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: