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 I added a product :productName |
||
178 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+")$/ |
||
179 | */ |
||
180 | public function storeHasAProductPricedAt($productName, $price = 0) |
||
192 | |||
193 | /** |
||
194 | * @Given the store( also) has a product :productName with code :code |
||
195 | * @Given the store( also) has a product :productName with code :code, created at :date |
||
196 | */ |
||
197 | public function storeHasProductWithCode($productName, $code, $date = null) |
||
209 | |||
210 | /** |
||
211 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") available in (channel "[^"]+") and (channel "[^"]+")$/ |
||
212 | */ |
||
213 | public function storeHasAProductPricedAtAvailableInChannels($productName, $price = 0, ...$channels) |
||
225 | |||
226 | /** |
||
227 | * @Given /^(this product) is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
228 | * @Given /^the (product "[^"]+") is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
229 | */ |
||
230 | public function thisProductIsNamedIn(ProductInterface $product, $name, $locale) |
||
242 | |||
243 | /** |
||
244 | * @Given the store has a :productName configurable product |
||
245 | */ |
||
246 | public function storeHasAConfigurableProduct($productName) |
||
264 | |||
265 | /** |
||
266 | * @Given the store has( also) :firstProductName and :secondProductName products |
||
267 | * @Given the store has( also) :firstProductName, :secondProductName and :thirdProductName products |
||
268 | * @Given the store has( also) :firstProductName, :secondProductName, :thirdProductName and :fourthProductName products |
||
269 | */ |
||
270 | public function theStoreHasProducts(...$productsNames) |
||
276 | |||
277 | /** |
||
278 | * @Given /^(this channel) has "([^"]+)", "([^"]+)", "([^"]+)" and "([^"]+)" products$/ |
||
279 | */ |
||
280 | public function thisChannelHasProducts(ChannelInterface $channel, ...$productsNames) |
||
289 | |||
290 | /** |
||
291 | * @Given /^the (product "[^"]+") has(?:| a) "([^"]+)" variant priced at ("[^"]+")$/ |
||
292 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/ |
||
293 | */ |
||
294 | public function theProductHasVariantPricedAt(ProductInterface $product, $productVariantName, $price) |
||
303 | |||
304 | /** |
||
305 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") identified by "([^"]+)"$/ |
||
306 | */ |
||
307 | public function theProductHasVariantPricedAtIdentifiedBy( |
||
315 | |||
316 | /** |
||
317 | * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ |
||
318 | * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/ |
||
319 | */ |
||
320 | public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) |
||
329 | |||
330 | /** |
||
331 | * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/ |
||
332 | */ |
||
333 | public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory) |
||
341 | |||
342 | /** |
||
343 | * @Given /^(it) comes in the following variations:$/ |
||
344 | */ |
||
345 | public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table) |
||
360 | |||
361 | /** |
||
362 | * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/ |
||
363 | */ |
||
364 | public function productVariantBelongsToTaxCategory( |
||
371 | |||
372 | /** |
||
373 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with value "([^"]+)"$/ |
||
374 | */ |
||
375 | public function thisProductHasAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
383 | |||
384 | /** |
||
385 | * @Given /^(this product) has percent attribute "([^"]+)" with value ([^"]+)%$/ |
||
386 | */ |
||
387 | public function thisProductHasPercentAttributeWithValue(ProductInterface $product, $productAttributeName, $value) |
||
395 | |||
396 | /** |
||
397 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" set to "([^"]+)"$/ |
||
398 | */ |
||
399 | public function thisProductHasCheckboxAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
408 | |||
409 | /** |
||
410 | * @Given /^(this product) has percent attribute "([^"]+)" at position (\d+)$/ |
||
411 | */ |
||
412 | public function thisProductHasPercentAttributeWithValueAtPosition(ProductInterface $product, $productAttributeName, $position) |
||
422 | |||
423 | /** |
||
424 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with date "([^"]+)"$/ |
||
425 | */ |
||
426 | public function thisProductHasDateTimeAttributeWithDate(ProductInterface $product, $productAttributeType, $productAttributeName, $date) |
||
435 | |||
436 | /** |
||
437 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/ |
||
438 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/ |
||
439 | */ |
||
440 | public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values) |
||
461 | |||
462 | /** |
||
463 | * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/ |
||
464 | * @When product :product quantity is changed to :quantity |
||
465 | */ |
||
466 | public function thereIsQuantityOfProducts($quantity, ProductInterface $product) |
||
474 | |||
475 | /** |
||
476 | * @Given /^the (product "([^"]+)") is out of stock$/ |
||
477 | */ |
||
478 | public function theProductIsOutOfStock(ProductInterface $product) |
||
487 | |||
488 | /** |
||
489 | * @When other customer has bought :quantity :product products by this time |
||
490 | */ |
||
491 | public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product) |
||
499 | |||
500 | /** |
||
501 | * @Given /^(this product) is tracked by the inventory$/ |
||
502 | * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/ |
||
503 | */ |
||
504 | public function thisProductIsTrackedByTheInventory(ProductInterface $product) |
||
512 | |||
513 | /** |
||
514 | * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/ |
||
515 | */ |
||
516 | public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, $price) |
||
530 | |||
531 | /** |
||
532 | * @Given /^(this product) has (this product option)$/ |
||
533 | * @Given /^(this product) has a ("[^"]+" option)$/ |
||
534 | * @Given /^(this product) has an ("[^"]+" option)$/ |
||
535 | */ |
||
536 | public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option) |
||
542 | |||
543 | /** |
||
544 | * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/ |
||
545 | */ |
||
546 | public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant) |
||
553 | |||
554 | /** |
||
555 | * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/ |
||
556 | */ |
||
557 | public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant) |
||
563 | |||
564 | /** |
||
565 | * @Given /^(this product)'s price is ("[^"]+")$/ |
||
566 | * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/ |
||
567 | */ |
||
568 | public function theProductChangedItsPriceTo(ProductInterface $product, $price) |
||
576 | |||
577 | /** |
||
578 | * @Given /^(this product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
579 | * @Given /^the ("[^"]+" product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
580 | */ |
||
581 | public function thisProductHasAnImageWithACode(ProductInterface $product, $imagePath, $imageCode) |
||
595 | |||
596 | /** |
||
597 | * @Given /^(it) has different prices for different channels and currencies$/ |
||
598 | */ |
||
599 | public function itHasDifferentPricesForDifferentChannelsAndCurrencies(ProductInterface $product) |
||
606 | |||
607 | /** |
||
608 | * @Given /^(it) has price ("[^"]+") for ("[^"]+" channel) and "([^"]+)" currency$/ |
||
609 | */ |
||
610 | public function itHasPriceForChannelAndCurrency( |
||
626 | |||
627 | /** |
||
628 | * @Given /^(this product) belongs to ("([^"]+)" shipping category)$/ |
||
629 | */ |
||
630 | public function thisProductBelongsToShippingCategory(ProductInterface $product, ShippingCategoryInterface $shippingCategory) |
||
635 | |||
636 | /** |
||
637 | * @param string $type |
||
638 | * @param string $name |
||
639 | * @param string|null $code |
||
640 | * |
||
641 | * @return ProductAttributeInterface |
||
642 | */ |
||
643 | private function createProductAttribute($type, $name, $code = null) |
||
658 | |||
659 | /** |
||
660 | * @param string $value |
||
661 | * @param ProductAttributeInterface $attribute |
||
662 | * |
||
663 | * @return ProductAttributeValueInterface |
||
664 | */ |
||
665 | private function createProductAttributeValue($value, ProductAttributeInterface $attribute) |
||
676 | |||
677 | /** |
||
678 | * @param string $price |
||
679 | * |
||
680 | * @return int |
||
681 | */ |
||
682 | private function getPriceFromString($price) |
||
686 | |||
687 | /** |
||
688 | * @param string $productName |
||
689 | * @param int $price |
||
690 | * @param string $date |
||
691 | * |
||
692 | * @return ProductInterface |
||
693 | */ |
||
694 | private function createProduct($productName, $price = 0, $date = null) |
||
711 | |||
712 | /** |
||
713 | * @param ProductOptionInterface $option |
||
714 | * @param string $value |
||
715 | * @param string $code |
||
716 | * |
||
717 | * @return ProductOptionValueInterface |
||
718 | */ |
||
719 | private function addProductOption(ProductOptionInterface $option, $value, $code) |
||
732 | |||
733 | /** |
||
734 | * @param ProductInterface $product |
||
735 | */ |
||
736 | private function saveProduct(ProductInterface $product) |
||
741 | |||
742 | /** |
||
743 | * @param string $name |
||
744 | * |
||
745 | * @return NodeElement |
||
746 | */ |
||
747 | private function getParameter($name) |
||
751 | |||
752 | /** |
||
753 | * @param ProductInterface $product |
||
754 | * @param $productVariantName |
||
755 | * @param int $price |
||
756 | * @param string $code |
||
757 | */ |
||
758 | private function createProductVariant(ProductInterface $product, $productVariantName, $price, $code) |
||
775 | } |
||
776 |
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: