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 |
||
| 46 | final class ProductContext implements Context |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * @var SharedStorageInterface |
||
| 50 | */ |
||
| 51 | private $sharedStorage; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var ProductRepositoryInterface |
||
| 55 | */ |
||
| 56 | private $productRepository; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var ProductFactoryInterface |
||
| 60 | */ |
||
| 61 | private $productFactory; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var FactoryInterface |
||
| 65 | */ |
||
| 66 | private $productTranslationFactory; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var AttributeFactoryInterface |
||
| 70 | */ |
||
| 71 | private $productAttributeFactory; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var FactoryInterface |
||
| 75 | */ |
||
| 76 | private $productVariantFactory; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var FactoryInterface |
||
| 80 | */ |
||
| 81 | private $attributeValueFactory; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var FactoryInterface |
||
| 85 | */ |
||
| 86 | private $productOptionFactory; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var FactoryInterface |
||
| 90 | */ |
||
| 91 | private $productOptionValueFactory; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var FactoryInterface |
||
| 95 | */ |
||
| 96 | private $productImageFactory; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var ObjectManager |
||
| 100 | */ |
||
| 101 | private $objectManager; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var ProductVariantResolverInterface |
||
| 105 | */ |
||
| 106 | private $defaultVariantResolver; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var ImageUploaderInterface |
||
| 110 | */ |
||
| 111 | private $imageUploader; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var SlugGeneratorInterface |
||
| 115 | */ |
||
| 116 | private $slugGenerator; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private $minkParameters; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param SharedStorageInterface $sharedStorage |
||
| 125 | * @param ProductRepositoryInterface $productRepository |
||
| 126 | * @param ProductFactoryInterface $productFactory |
||
| 127 | * @param FactoryInterface $productTranslationFactory |
||
| 128 | * @param AttributeFactoryInterface $productAttributeFactory |
||
| 129 | * @param FactoryInterface $attributeValueFactory |
||
| 130 | * @param FactoryInterface $productVariantFactory |
||
| 131 | * @param FactoryInterface $productOptionFactory |
||
| 132 | * @param FactoryInterface $productOptionValueFactory |
||
| 133 | * @param FactoryInterface $productImageFactory |
||
| 134 | * @param ObjectManager $objectManager |
||
| 135 | * @param ProductVariantResolverInterface $defaultVariantResolver |
||
| 136 | * @param ImageUploaderInterface $imageUploader |
||
| 137 | * @param SlugGeneratorInterface $slugGenerator |
||
| 138 | * @param array $minkParameters |
||
| 139 | */ |
||
| 140 | public function __construct( |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @Given the store has a product :productName |
||
| 176 | * @Given the store has a :productName product |
||
| 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 | */ |
||
| 228 | public function thisProductIsNamedIn(ProductInterface $product, $name, $locale) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @Given the store has a :productName configurable product |
||
| 243 | */ |
||
| 244 | public function storeHasAConfigurableProduct($productName) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @Given the store has( also) :firstProductName and :secondProductName products |
||
| 265 | * @Given the store has( also) :firstProductName, :secondProductName and :thirdProductName products |
||
| 266 | * @Given the store has( also) :firstProductName, :secondProductName, :thirdProductName and :fourthProductName products |
||
| 267 | */ |
||
| 268 | public function theStoreHasProducts(...$productsNames) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @Given /^the (product "[^"]+") has(?:| a) "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 277 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 278 | */ |
||
| 279 | public function theProductHasVariantPricedAt(ProductInterface $product, $productVariantName, $price) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ |
||
| 299 | * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/ |
||
| 300 | */ |
||
| 301 | public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/ |
||
| 313 | */ |
||
| 314 | public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @Given /^(it) comes in the following variations:$/ |
||
| 325 | */ |
||
| 326 | public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/ |
||
| 344 | */ |
||
| 345 | public function productVariantBelongsToTaxCategory( |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with value "([^"]+)"$/ |
||
| 355 | */ |
||
| 356 | public function thisProductHasAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @Given /^(this product) has percent attribute "([^"]+)" with value ([^"]+)%$/ |
||
| 367 | */ |
||
| 368 | public function thisProductHasPercentAttributeWithValue(ProductInterface $product, $productAttributeName, $value) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" set to "([^"]+)"$/ |
||
| 379 | */ |
||
| 380 | public function thisProductHasCheckboxAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with date "([^"]+)"$/ |
||
| 392 | */ |
||
| 393 | public function thisProductHasDateTimeAttributeWithDate(ProductInterface $product, $productAttributeType, $productAttributeName, $date) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/ |
||
| 405 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/ |
||
| 406 | */ |
||
| 407 | public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/ |
||
| 431 | * @When product :product quantity is changed to :quantity |
||
| 432 | */ |
||
| 433 | public function thereIsQuantityOfProducts($quantity, ProductInterface $product) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @Given /^the (product "([^"]+)") is out of stock$/ |
||
| 444 | */ |
||
| 445 | public function theProductIsOutOfStock(ProductInterface $product) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @When other customer has bought :quantity :product products by this time |
||
| 457 | */ |
||
| 458 | public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @Given /^(this product) is tracked by the inventory$/ |
||
| 469 | * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/ |
||
| 470 | */ |
||
| 471 | public function thisProductIsTrackedByTheInventory(ProductInterface $product) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/ |
||
| 482 | */ |
||
| 483 | public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, $price) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @Given /^(this product) has (this product option)$/ |
||
| 500 | * @Given /^(this product) has a ("[^"]+" option)$/ |
||
| 501 | * @Given /^(this product) has an ("[^"]+" option)$/ |
||
| 502 | */ |
||
| 503 | public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/ |
||
| 512 | */ |
||
| 513 | public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/ |
||
| 523 | */ |
||
| 524 | public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @Given /^(this product)'s price is ("[^"]+")$/ |
||
| 533 | * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/ |
||
| 534 | */ |
||
| 535 | public function theProductChangedItsPriceTo(ProductInterface $product, $price) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @Given /^(this product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
| 546 | * @Given /^the ("[^"]+" product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
| 547 | */ |
||
| 548 | public function thisProductHasAnImageWithACode(ProductInterface $product, $imagePath, $imageCode) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @Given /^(it) has different prices for different channels and currencies$/ |
||
| 565 | */ |
||
| 566 | public function itHasDifferentPricesForDifferentChannelsAndCurrencies(ProductInterface $product) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @Given /^(it) has price ("[^"]+") for ("[^"]+" channel) and "([^"]+)" currency$/ |
||
| 576 | */ |
||
| 577 | public function itHasPriceForChannelAndCurrency( |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @param string $type |
||
| 596 | * @param string $name |
||
| 597 | * @param string $code |
||
| 598 | * |
||
| 599 | * @return ProductAttributeInterface |
||
| 600 | */ |
||
| 601 | private function createProductAttribute($type, $name, $code = 'PA112') |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @param string $value |
||
| 614 | * |
||
| 615 | * @return ProductAttributeValueInterface |
||
| 616 | */ |
||
| 617 | private function createProductAttributeValue($value, ProductAttributeInterface $attribute) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @param string $price |
||
| 631 | * |
||
| 632 | * @return int |
||
| 633 | */ |
||
| 634 | private function getPriceFromString($price) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @param string $productName |
||
| 641 | * @param int $price |
||
| 642 | * |
||
| 643 | * @return ProductInterface |
||
| 644 | */ |
||
| 645 | private function createProduct($productName, $price = 0, $date = null) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * @param ProductOptionInterface $option |
||
| 665 | * @param string $value |
||
| 666 | * @param string $code |
||
| 667 | * |
||
| 668 | * @return ProductOptionValueInterface |
||
| 669 | */ |
||
| 670 | private function addProductOption(ProductOptionInterface $option, $value, $code) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @param ProductInterface $product |
||
| 686 | */ |
||
| 687 | private function saveProduct(ProductInterface $product) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @param string $name |
||
| 695 | * |
||
| 696 | * @return NodeElement |
||
| 697 | */ |
||
| 698 | private function getParameter($name) |
||
| 702 | } |
||
| 703 |
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: