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 |
||
47 | final class ProductContext implements Context |
||
48 | { |
||
49 | /** |
||
50 | * @var SharedStorageInterface |
||
51 | */ |
||
52 | private $sharedStorage; |
||
53 | |||
54 | /** |
||
55 | * @var ProductRepositoryInterface |
||
56 | */ |
||
57 | private $productRepository; |
||
58 | |||
59 | /** |
||
60 | * @var ProductFactoryInterface |
||
61 | */ |
||
62 | private $productFactory; |
||
63 | |||
64 | /** |
||
65 | * @var FactoryInterface |
||
66 | */ |
||
67 | private $productTranslationFactory; |
||
68 | |||
69 | /** |
||
70 | * @var AttributeFactoryInterface |
||
71 | */ |
||
72 | private $productAttributeFactory; |
||
73 | |||
74 | /** |
||
75 | * @var FactoryInterface |
||
76 | */ |
||
77 | private $productVariantFactory; |
||
78 | |||
79 | /** |
||
80 | * @var FactoryInterface |
||
81 | */ |
||
82 | private $attributeValueFactory; |
||
83 | |||
84 | /** |
||
85 | * @var FactoryInterface |
||
86 | */ |
||
87 | private $productOptionFactory; |
||
88 | |||
89 | /** |
||
90 | * @var FactoryInterface |
||
91 | */ |
||
92 | private $productOptionValueFactory; |
||
93 | |||
94 | /** |
||
95 | * @var FactoryInterface |
||
96 | */ |
||
97 | private $productImageFactory; |
||
98 | |||
99 | /** |
||
100 | * @var ObjectManager |
||
101 | */ |
||
102 | private $objectManager; |
||
103 | |||
104 | /** |
||
105 | * @var ProductVariantResolverInterface |
||
106 | */ |
||
107 | private $defaultVariantResolver; |
||
108 | |||
109 | /** |
||
110 | * @var ImageUploaderInterface |
||
111 | */ |
||
112 | private $imageUploader; |
||
113 | |||
114 | /** |
||
115 | * @var SlugGeneratorInterface |
||
116 | */ |
||
117 | private $slugGenerator; |
||
118 | |||
119 | /** |
||
120 | * @var array |
||
121 | */ |
||
122 | private $minkParameters; |
||
123 | |||
124 | /** |
||
125 | * @param SharedStorageInterface $sharedStorage |
||
126 | * @param ProductRepositoryInterface $productRepository |
||
127 | * @param ProductFactoryInterface $productFactory |
||
128 | * @param FactoryInterface $productTranslationFactory |
||
129 | * @param AttributeFactoryInterface $productAttributeFactory |
||
130 | * @param FactoryInterface $attributeValueFactory |
||
131 | * @param FactoryInterface $productVariantFactory |
||
132 | * @param FactoryInterface $productOptionFactory |
||
133 | * @param FactoryInterface $productOptionValueFactory |
||
134 | * @param FactoryInterface $productImageFactory |
||
135 | * @param ObjectManager $objectManager |
||
136 | * @param ProductVariantResolverInterface $defaultVariantResolver |
||
137 | * @param ImageUploaderInterface $imageUploader |
||
138 | * @param SlugGeneratorInterface $slugGenerator |
||
139 | * @param array $minkParameters |
||
140 | */ |
||
141 | public function __construct( |
||
174 | |||
175 | /** |
||
176 | * @Given the store has a product :productName |
||
177 | * @Given the store has a :productName product |
||
178 | * @Given I added a product :productName |
||
179 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+")$/ |
||
180 | */ |
||
181 | public function storeHasAProductPricedAt($productName, $price = 0) |
||
193 | |||
194 | /** |
||
195 | * @Given the store( also) has a product :productName with code :code |
||
196 | * @Given the store( also) has a product :productName with code :code, created at :date |
||
197 | */ |
||
198 | public function storeHasProductWithCode($productName, $code, $date = null) |
||
210 | |||
211 | /** |
||
212 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") available in (channel "[^"]+") and (channel "[^"]+")$/ |
||
213 | */ |
||
214 | public function storeHasAProductPricedAtAvailableInChannels($productName, $price = 0, ...$channels) |
||
226 | |||
227 | /** |
||
228 | * @Given /^(this product) is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
229 | * @Given /^the (product "[^"]+") is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
230 | */ |
||
231 | public function thisProductIsNamedIn(ProductInterface $product, $name, $locale) |
||
237 | |||
238 | /** |
||
239 | * @Given /^the store has a product named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
||
240 | */ |
||
241 | public function theStoreHasProductNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale) |
||
252 | |||
253 | /** |
||
254 | * @Given the store has a :productName configurable product |
||
255 | */ |
||
256 | public function storeHasAConfigurableProduct($productName) |
||
274 | |||
275 | /** |
||
276 | * @Given the store has( also) :firstProductName and :secondProductName products |
||
277 | * @Given the store has( also) :firstProductName, :secondProductName and :thirdProductName products |
||
278 | * @Given the store has( also) :firstProductName, :secondProductName, :thirdProductName and :fourthProductName products |
||
279 | */ |
||
280 | public function theStoreHasProducts(...$productsNames) |
||
286 | |||
287 | /** |
||
288 | * @Given /^(this channel) has "([^"]+)", "([^"]+)", "([^"]+)" and "([^"]+)" products$/ |
||
289 | */ |
||
290 | public function thisChannelHasProducts(ChannelInterface $channel, ...$productsNames) |
||
299 | |||
300 | /** |
||
301 | * @Given /^the (product "[^"]+") has(?:| a) "([^"]+)" variant priced at ("[^"]+")$/ |
||
302 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/ |
||
303 | */ |
||
304 | public function theProductHasVariantPricedAt(ProductInterface $product, $productVariantName, $price) |
||
313 | |||
314 | /** |
||
315 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") identified by "([^"]+)"$/ |
||
316 | */ |
||
317 | public function theProductHasVariantPricedAtIdentifiedBy( |
||
325 | |||
326 | /** |
||
327 | * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ |
||
328 | * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/ |
||
329 | */ |
||
330 | public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) |
||
339 | |||
340 | /** |
||
341 | * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/ |
||
342 | */ |
||
343 | public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory) |
||
351 | |||
352 | /** |
||
353 | * @Given /^(it) comes in the following variations:$/ |
||
354 | */ |
||
355 | public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table) |
||
370 | |||
371 | /** |
||
372 | * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/ |
||
373 | */ |
||
374 | public function productVariantBelongsToTaxCategory( |
||
381 | |||
382 | /** |
||
383 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with value "([^"]+)"$/ |
||
384 | */ |
||
385 | public function thisProductHasAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
393 | |||
394 | /** |
||
395 | * @Given /^(this product) has percent attribute "([^"]+)" with value ([^"]+)%$/ |
||
396 | */ |
||
397 | public function thisProductHasPercentAttributeWithValue(ProductInterface $product, $productAttributeName, $value) |
||
405 | |||
406 | /** |
||
407 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" set to "([^"]+)"$/ |
||
408 | */ |
||
409 | public function thisProductHasCheckboxAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value) |
||
418 | |||
419 | /** |
||
420 | * @Given /^(this product) has percent attribute "([^"]+)" at position (\d+)$/ |
||
421 | */ |
||
422 | public function thisProductHasPercentAttributeWithValueAtPosition(ProductInterface $product, $productAttributeName, $position) |
||
432 | |||
433 | /** |
||
434 | * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with date "([^"]+)"$/ |
||
435 | */ |
||
436 | public function thisProductHasDateTimeAttributeWithDate(ProductInterface $product, $productAttributeType, $productAttributeName, $date) |
||
445 | |||
446 | /** |
||
447 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/ |
||
448 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/ |
||
449 | */ |
||
450 | public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values) |
||
471 | |||
472 | /** |
||
473 | * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/ |
||
474 | * @When product :product quantity is changed to :quantity |
||
475 | */ |
||
476 | public function thereIsQuantityOfProducts($quantity, ProductInterface $product) |
||
484 | |||
485 | /** |
||
486 | * @Given /^the (product "([^"]+)") is out of stock$/ |
||
487 | */ |
||
488 | public function theProductIsOutOfStock(ProductInterface $product) |
||
497 | |||
498 | /** |
||
499 | * @When other customer has bought :quantity :product products by this time |
||
500 | */ |
||
501 | public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product) |
||
509 | |||
510 | /** |
||
511 | * @Given /^(this product) is tracked by the inventory$/ |
||
512 | * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/ |
||
513 | */ |
||
514 | public function thisProductIsTrackedByTheInventory(ProductInterface $product) |
||
522 | |||
523 | /** |
||
524 | * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/ |
||
525 | */ |
||
526 | public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, $price) |
||
540 | |||
541 | /** |
||
542 | * @Given /^(this product) has (this product option)$/ |
||
543 | * @Given /^(this product) has a ("[^"]+" option)$/ |
||
544 | * @Given /^(this product) has an ("[^"]+" option)$/ |
||
545 | */ |
||
546 | public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option) |
||
552 | |||
553 | /** |
||
554 | * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/ |
||
555 | */ |
||
556 | public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant) |
||
563 | |||
564 | /** |
||
565 | * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/ |
||
566 | */ |
||
567 | public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant) |
||
573 | |||
574 | /** |
||
575 | * @Given /^(this product)'s price is ("[^"]+")$/ |
||
576 | * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/ |
||
577 | */ |
||
578 | public function theProductChangedItsPriceTo(ProductInterface $product, $price) |
||
586 | |||
587 | /** |
||
588 | * @Given /^(this product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
589 | * @Given /^the ("[^"]+" product) has(?:| also) an image "([^"]+)" with a code "([^"]+)"$/ |
||
590 | */ |
||
591 | public function thisProductHasAnImageWithACode(ProductInterface $product, $imagePath, $imageCode) |
||
605 | |||
606 | /** |
||
607 | * @Given /^(it) has different prices for different channels and currencies$/ |
||
608 | */ |
||
609 | public function itHasDifferentPricesForDifferentChannelsAndCurrencies(ProductInterface $product) |
||
616 | |||
617 | /** |
||
618 | * @Given /^(it) has price ("[^"]+") for ("[^"]+" channel) and "([^"]+)" currency$/ |
||
619 | */ |
||
620 | public function itHasPriceForChannelAndCurrency( |
||
636 | |||
637 | /** |
||
638 | * @Given /^(this product) belongs to ("([^"]+)" shipping category)$/ |
||
639 | */ |
||
640 | public function thisProductBelongsToShippingCategory(ProductInterface $product, ShippingCategoryInterface $shippingCategory) |
||
645 | |||
646 | /** |
||
647 | * @param string $type |
||
648 | * @param string $name |
||
649 | * @param string|null $code |
||
650 | * |
||
651 | * @return ProductAttributeInterface |
||
652 | */ |
||
653 | private function createProductAttribute($type, $name, $code = null) |
||
668 | |||
669 | /** |
||
670 | * @param string $value |
||
671 | * @param ProductAttributeInterface $attribute |
||
672 | * |
||
673 | * @return ProductAttributeValueInterface |
||
674 | */ |
||
675 | private function createProductAttributeValue($value, ProductAttributeInterface $attribute) |
||
686 | |||
687 | /** |
||
688 | * @param string $price |
||
689 | * |
||
690 | * @return int |
||
691 | */ |
||
692 | private function getPriceFromString($price) |
||
696 | |||
697 | /** |
||
698 | * @param string $productName |
||
699 | * @param int $price |
||
700 | * @param string $date |
||
701 | * |
||
702 | * @return ProductInterface |
||
703 | */ |
||
704 | private function createProduct($productName, $price = 0, $date = null) |
||
721 | |||
722 | /** |
||
723 | * @param ProductOptionInterface $option |
||
724 | * @param string $value |
||
725 | * @param string $code |
||
726 | * |
||
727 | * @return ProductOptionValueInterface |
||
728 | */ |
||
729 | private function addProductOption(ProductOptionInterface $option, $value, $code) |
||
742 | |||
743 | /** |
||
744 | * @param ProductInterface $product |
||
745 | */ |
||
746 | private function saveProduct(ProductInterface $product) |
||
751 | |||
752 | /** |
||
753 | * @param string $name |
||
754 | * |
||
755 | * @return NodeElement |
||
756 | */ |
||
757 | private function getParameter($name) |
||
761 | |||
762 | /** |
||
763 | * @param ProductInterface $product |
||
764 | * @param $productVariantName |
||
765 | * @param int $price |
||
766 | * @param string $code |
||
767 | */ |
||
768 | private function createProductVariant(ProductInterface $product, $productVariantName, $price, $code) |
||
785 | |||
786 | /** |
||
787 | * @param ProductInterface $product |
||
788 | * @param string $name |
||
789 | * @param string $locale |
||
790 | */ |
||
791 | private function addProductTranslation(ProductInterface $product, $name, $locale) |
||
801 | } |
||
802 |
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: