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 |
||
| 49 | final class ProductContext implements Context |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * @var SharedStorageInterface |
||
| 53 | */ |
||
| 54 | private $sharedStorage; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var ProductRepositoryInterface |
||
| 58 | */ |
||
| 59 | private $productRepository; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var ProductFactoryInterface |
||
| 63 | */ |
||
| 64 | private $productFactory; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var FactoryInterface |
||
| 68 | */ |
||
| 69 | private $productTranslationFactory; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var FactoryInterface |
||
| 73 | */ |
||
| 74 | private $productVariantFactory; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var FactoryInterface |
||
| 78 | */ |
||
| 79 | private $productVariantTranslationFactory; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var FactoryInterface |
||
| 83 | */ |
||
| 84 | private $channelPricingFactory; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var FactoryInterface |
||
| 88 | */ |
||
| 89 | private $productOptionFactory; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var FactoryInterface |
||
| 93 | */ |
||
| 94 | private $productOptionValueFactory; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var FactoryInterface |
||
| 98 | */ |
||
| 99 | private $productImageFactory; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var ObjectManager |
||
| 103 | */ |
||
| 104 | private $objectManager; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var ProductVariantGeneratorInterface |
||
| 108 | */ |
||
| 109 | private $productVariantGenerator; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var ProductVariantResolverInterface |
||
| 113 | */ |
||
| 114 | private $defaultVariantResolver; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var ImageUploaderInterface |
||
| 118 | */ |
||
| 119 | private $imageUploader; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var SlugGeneratorInterface |
||
| 123 | */ |
||
| 124 | private $slugGenerator; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | private $minkParameters; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param SharedStorageInterface $sharedStorage |
||
| 133 | * @param ProductRepositoryInterface $productRepository |
||
| 134 | * @param ProductFactoryInterface $productFactory |
||
| 135 | * @param FactoryInterface $productTranslationFactory |
||
| 136 | * @param FactoryInterface $productVariantFactory |
||
| 137 | * @param FactoryInterface $productVariantTranslationFactory |
||
| 138 | * @param FactoryInterface $channelPricingFactory |
||
| 139 | * @param FactoryInterface $productOptionFactory |
||
| 140 | * @param FactoryInterface $productOptionValueFactory |
||
| 141 | * @param FactoryInterface $productImageFactory |
||
| 142 | * @param ObjectManager $objectManager |
||
| 143 | * @param ProductVariantGeneratorInterface $productVariantGenerator |
||
| 144 | * @param ProductVariantResolverInterface $defaultVariantResolver |
||
| 145 | * @param ImageUploaderInterface $imageUploader |
||
| 146 | * @param SlugGeneratorInterface $slugGenerator |
||
| 147 | * @param array $minkParameters |
||
| 148 | */ |
||
| 149 | public function __construct( |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @Given the store has a product :productName |
||
| 187 | * @Given the store has a :productName product |
||
| 188 | * @Given I added a product :productName |
||
| 189 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+")$/ |
||
| 190 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") in ("[^"]+" channel)$/ |
||
| 191 | */ |
||
| 192 | public function storeHasAProductPricedAt($productName, $price = 100, ChannelInterface $channel = null) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @Given /^(this product) is also priced at ("[^"]+") in ("[^"]+" channel)$/ |
||
| 201 | */ |
||
| 202 | public function thisProductIsAlsoPricedAtInChannel(ProductInterface $product, $price, ChannelInterface $channel) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @Given the store( also) has a product :productName with code :code |
||
| 215 | * @Given the store( also) has a product :productName with code :code, created at :date |
||
| 216 | */ |
||
| 217 | public function storeHasProductWithCode($productName, $code, $date = null) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @Given /^the store(?:| also) has a product "([^"]+)" priced at ("[^"]+") available in (channel "[^"]+") and (channel "[^"]+")$/ |
||
| 228 | */ |
||
| 229 | public function storeHasAProductPricedAtAvailableInChannels($productName, $price = 100, ...$channels) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @Given /^(this product) is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
| 247 | * @Given /^the (product "[^"]+") is named "([^"]+)" (in the "([^"]+)" locale)$/ |
||
| 248 | */ |
||
| 249 | public function thisProductIsNamedIn(ProductInterface $product, $name, $locale) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @Given /^the store has a product named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
||
| 258 | */ |
||
| 259 | public function theStoreHasProductNamedInAndIn($firstName, $firstLocale, $secondName, $secondLocale) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @Given /^the store has(?:| a| an) "([^"]+)" configurable product$/ |
||
| 273 | * @Given /^the store has(?:| a| an) "([^"]+)" configurable product with "([^"]+)" slug$/ |
||
| 274 | */ |
||
| 275 | public function storeHasAConfigurableProduct($productName, $slug = null) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @Given the store has( also) :firstProductName and :secondProductName products |
||
| 304 | * @Given the store has( also) :firstProductName, :secondProductName and :thirdProductName products |
||
| 305 | * @Given the store has( also) :firstProductName, :secondProductName, :thirdProductName and :fourthProductName products |
||
| 306 | */ |
||
| 307 | public function theStoreHasProducts(...$productsNames) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @Given /^(this channel) has "([^"]+)", "([^"]+)", "([^"]+)" and "([^"]+)" products$/ |
||
| 316 | */ |
||
| 317 | public function thisChannelHasProducts(ChannelInterface $channel, ...$productsNames) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @Given /^the (product "[^"]+") has(?:| a) "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 328 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/ |
||
| 329 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") in ("([^"]+)" channel)$/ |
||
| 330 | */ |
||
| 331 | public function theProductHasVariantPricedAt( |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @Given /^the (product "[^"]+") has(?:| a| an) "([^"]+)" variant$/ |
||
| 348 | * @Given /^(this product) has(?:| a| an) "([^"]+)" variant$/ |
||
| 349 | * @Given /^(this product) has "([^"]+)", "([^"]+)" and "([^"]+)" variants$/ |
||
| 350 | */ |
||
| 351 | public function theProductHasVariants(ProductInterface $product, ...$variantNames) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @Given /^the (product "[^"]+")(?:| also) has a nameless variant with code "([^"]+)"$/ |
||
| 368 | * @Given /^(this product)(?:| also) has a nameless variant with code "([^"]+)"$/ |
||
| 369 | * @Given /^(it)(?:| also) has a nameless variant with code "([^"]+)"$/ |
||
| 370 | */ |
||
| 371 | public function theProductHasNamelessVariantWithCode(ProductInterface $product, $variantCode) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @Given /^the (product "[^"]+")(?:| also) has(?:| a| an) "([^"]+)" variant with code "([^"]+)"$/ |
||
| 380 | * @Given /^(this product)(?:| also) has(?:| a| an) "([^"]+)" variant with code "([^"]+)"$/ |
||
| 381 | * @Given /^(it)(?:| also) has(?:| a| an) "([^"]+)" variant with code "([^"]+)"$/ |
||
| 382 | */ |
||
| 383 | public function theProductHasVariantWithCode(ProductInterface $product, $variantName, $variantCode) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") which does not require shipping$/ |
||
| 392 | */ |
||
| 393 | public function theProductHasVariantWhichDoesNotRequireShipping( |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @Given /^the (product "[^"]+") has(?:| also)(?:| a| an) "([^"]+)" variant$/ |
||
| 411 | * @Given /^the (product "[^"]+") has(?:| also)(?:| a| an) "([^"]+)" variant at position ([^"]+)$/ |
||
| 412 | * @Given /^(this product) has(?:| also)(?:| a| an) "([^"]+)" variant at position ([^"]+)$/ |
||
| 413 | */ |
||
| 414 | public function theProductHasVariantAtPosition( |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @Given /^(this variant) is also priced at ("[^"]+") in ("([^"]+)" channel)$/ |
||
| 431 | */ |
||
| 432 | public function thisVariantIsAlsoPricedAtInChannel(ProductVariantInterface $productVariant, $price, ChannelInterface $channel) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @Given /^(it|this product) has(?:| also) variant named "([^"]+)" in ("[^"]+" locale) and "([^"]+)" in ("[^"]+" locale)$/ |
||
| 444 | */ |
||
| 445 | public function itHasVariantNamedInAndIn(ProductInterface $product, $firstName, $firstLocale, $secondName, $secondLocale) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+") identified by "([^"]+)"$/ |
||
| 465 | */ |
||
| 466 | public function theProductHasVariantPricedAtIdentifiedBy( |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @Given /^(this product) only variant was renamed to "([^"]+)"$/ |
||
| 477 | */ |
||
| 478 | public function productOnlyVariantWasRenamed(ProductInterface $product, $variantName) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ |
||
| 491 | * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/ |
||
| 492 | */ |
||
| 493 | public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/ |
||
| 502 | */ |
||
| 503 | public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @Given /^(it) comes in the following variations:$/ |
||
| 514 | */ |
||
| 515 | public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/ |
||
| 539 | */ |
||
| 540 | public function productVariantBelongsToTaxCategory( |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/ |
||
| 550 | * @Given /^(this product) has option "([^"]+)" with values "([^"]+)", "([^"]+)" and "([^"]+)"$/ |
||
| 551 | */ |
||
| 552 | public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, ...$values) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @Given /^there (?:is|are) (\d+) unit(?:|s) of (product "([^"]+)") available in the inventory$/ |
||
| 576 | */ |
||
| 577 | public function thereIsQuantityOfProducts($quantity, ProductInterface $product) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * @Given /^the (product "([^"]+)") is out of stock$/ |
||
| 588 | */ |
||
| 589 | public function theProductIsOutOfStock(ProductInterface $product) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @When other customer has bought :quantity :product products by this time |
||
| 601 | */ |
||
| 602 | public function otherCustomerHasBoughtProductsByThisTime($quantity, ProductInterface $product) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @Given /^(this product) is tracked by the inventory$/ |
||
| 613 | * @Given /^(?:|the )("[^"]+" product) is(?:| also) tracked by the inventory$/ |
||
| 614 | */ |
||
| 615 | public function thisProductIsTrackedByTheInventory(ProductInterface $product) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @Given /^(this product) is available in "([^"]+)" ([^"]+) priced at ("[^"]+")$/ |
||
| 626 | */ |
||
| 627 | public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $optionName, $price) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @Given the :product product's :optionValueName size belongs to :shippingCategory shipping category |
||
| 645 | */ |
||
| 646 | public function thisProductSizeBelongsToShippingCategory(ProductInterface $product, $optionValueName, ShippingCategoryInterface $shippingCategory) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @Given /^(this product) has (this product option)$/ |
||
| 662 | * @Given /^(this product) has (?:a|an) ("[^"]+" option)$/ |
||
| 663 | */ |
||
| 664 | public function thisProductHasThisProductOption(ProductInterface $product, ProductOptionInterface $option) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @Given /^(this product) has all possible variants$/ |
||
| 673 | */ |
||
| 674 | public function thisProductHasAllPossibleVariants(ProductInterface $product) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @Given /^there are ([^"]+) units of ("[^"]+" variant of product "[^"]+") available in the inventory$/ |
||
| 706 | */ |
||
| 707 | public function thereAreItemsOfProductInVariantAvailableInTheInventory($quantity, ProductVariantInterface $productVariant) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @Given /^the ("[^"]+" product variant) is tracked by the inventory$/ |
||
| 717 | */ |
||
| 718 | public function theProductVariantIsTrackedByTheInventory(ProductVariantInterface $productVariant) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @Given /^(this product)'s price is ("[^"]+")$/ |
||
| 727 | * @Given /^the (product "[^"]+") changed its price to ("[^"]+")$/ |
||
| 728 | * @Given /^(this product) price has been changed to ("[^"]+")$/ |
||
| 729 | */ |
||
| 730 | public function theProductChangedItsPriceTo(ProductInterface $product, $price) |
||
| 739 | |||
| 740 | /** |
||
| 741 | * @Given /^(this product)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
||
| 742 | * @Given /^the ("[^"]+" product)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
||
| 743 | * @Given /^(it)(?:| also) has an image "([^"]+)" with "([^"]+)" type$/ |
||
| 744 | */ |
||
| 745 | public function thisProductHasAnImageWithType(ProductInterface $product, $imagePath, $imageType) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * @Given /^(this product) belongs to ("([^"]+)" shipping category)$/ |
||
| 762 | * @Given product :product shipping category has been changed to :shippingCategory |
||
| 763 | */ |
||
| 764 | public function thisProductBelongsToShippingCategory(ProductInterface $product, ShippingCategoryInterface $shippingCategory) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * @Given /^(this product) has been disabled$/ |
||
| 772 | */ |
||
| 773 | public function thisProductHasBeenDisabled(ProductInterface $product) |
||
| 778 | |||
| 779 | /** |
||
| 780 | * @param string $price |
||
| 781 | * |
||
| 782 | * @return int |
||
| 783 | */ |
||
| 784 | private function getPriceFromString($price) |
||
| 788 | |||
| 789 | /** |
||
| 790 | * @param string $productName |
||
| 791 | * @param int $price |
||
| 792 | * @param ChannelInterface|null $channel |
||
| 793 | * |
||
| 794 | * @return ProductInterface |
||
| 795 | */ |
||
| 796 | private function createProduct($productName, $price = 100, ChannelInterface $channel = null) |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @param ProductOptionInterface $option |
||
| 836 | * @param string $value |
||
| 837 | * @param string $code |
||
| 838 | * |
||
| 839 | * @return ProductOptionValueInterface |
||
| 840 | */ |
||
| 841 | private function addProductOption(ProductOptionInterface $option, $value, $code) |
||
| 854 | |||
| 855 | /** |
||
| 856 | * @param ProductInterface $product |
||
| 857 | */ |
||
| 858 | private function saveProduct(ProductInterface $product) |
||
| 863 | |||
| 864 | /** |
||
| 865 | * @param string $name |
||
| 866 | * |
||
| 867 | * @return NodeElement |
||
| 868 | */ |
||
| 869 | private function getParameter($name) |
||
| 873 | |||
| 874 | /** |
||
| 875 | * @param ProductInterface $product |
||
| 876 | * @param $productVariantName |
||
| 877 | * @param int $price |
||
| 878 | * @param string $code |
||
| 879 | * @param ChannelInterface $channel |
||
|
|
|||
| 880 | * @param int $position |
||
| 881 | * @param bool $shippingRequired |
||
| 882 | * |
||
| 883 | * @return ProductVariantInterface |
||
| 884 | */ |
||
| 885 | private function createProductVariant( |
||
| 913 | |||
| 914 | /** |
||
| 915 | * @param ProductInterface $product |
||
| 916 | * @param string $name |
||
| 917 | * @param string $locale |
||
| 918 | */ |
||
| 919 | private function addProductTranslation(ProductInterface $product, $name, $locale) |
||
| 933 | |||
| 934 | /** |
||
| 935 | * @param ProductVariantInterface $productVariant |
||
| 936 | * @param string $name |
||
| 937 | * @param string $locale |
||
| 938 | */ |
||
| 939 | private function addProductVariantTranslation(ProductVariantInterface $productVariant, $name, $locale) |
||
| 948 | |||
| 949 | /** |
||
| 950 | * @param int $price |
||
| 951 | * @param ChannelInterface|null $channel |
||
| 952 | * |
||
| 953 | * @return ChannelPricingInterface |
||
| 954 | */ |
||
| 955 | private function createChannelPricingForChannel($price, ChannelInterface $channel = null) |
||
| 964 | } |
||
| 965 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.