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 |
||
| 27 | final class ProductContext implements Context |
||
| 28 | { |
||
| 29 | /** @var ShowPageInterface */ |
||
| 30 | private $showPage; |
||
| 31 | |||
| 32 | /** @var IndexPageInterface */ |
||
| 33 | private $indexPage; |
||
| 34 | |||
| 35 | /** @var ProductReviewIndexPageInterface */ |
||
| 36 | private $productReviewsIndexPage; |
||
| 37 | |||
| 38 | /** @var ErrorPageInterface */ |
||
| 39 | private $errorPage; |
||
| 40 | |||
| 41 | public function __construct( |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @Then I should be able to access product :product |
||
| 55 | */ |
||
| 56 | public function iShouldBeAbleToAccessProduct(ProductInterface $product) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @Then I should not be able to access product :product |
||
| 65 | */ |
||
| 66 | public function iShouldNotBeAbleToAccessProduct(ProductInterface $product) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @When /^I check (this product)'s details$/ |
||
| 75 | * @When /^I check (this product)'s details in the ("([^"]+)" locale)$/ |
||
| 76 | * @When I view product :product |
||
| 77 | * @When I view product :product in the :localeCode locale |
||
| 78 | */ |
||
| 79 | public function iOpenProductPage(ProductInterface $product, $localeCode = 'en_US') |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @When /^I try to check (this product)'s details in the ("([^"]+)" locale)$/ |
||
| 86 | */ |
||
| 87 | public function iTryToOpenProductPage(ProductInterface $product, $localeCode = 'en_US') |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @When I try to reach unexistent product |
||
| 97 | */ |
||
| 98 | public function iTryToReachUnexistentProductPage($localeCode = 'en_US') |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @Then /^I should not be able to view (this product) in the ("([^"]+)" locale)$/ |
||
| 108 | */ |
||
| 109 | public function iShouldNotBeAbleToViewThisProductInLocale(ProductInterface $product, $localeCode = 'en_US') |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @Then I should see the product name :name |
||
| 121 | */ |
||
| 122 | public function iShouldSeeProductName($name) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @When I open page :url |
||
| 129 | */ |
||
| 130 | public function iOpenPage($url) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @Then I should be on :product product detailed page |
||
| 137 | * @Then I should still be on product :product page |
||
| 138 | */ |
||
| 139 | public function iShouldBeOnProductDetailedPage(ProductInterface $product) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @Then I should (also) see the product attribute :attributeName with value :expectedAttribute |
||
| 146 | */ |
||
| 147 | public function iShouldSeeTheProductAttributeWithValue($attributeName, $expectedAttribute) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @Then I should not see the product attribute :attributeName |
||
| 154 | */ |
||
| 155 | public function iShouldNotSeeTheProductAttribute(string $attributeName): void |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @Then I should (also) see the product attribute :attributeName with date :expectedAttribute |
||
| 162 | */ |
||
| 163 | public function iShouldSeeTheProductAttributeWithDate($attributeName, $expectedAttribute) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @Then I should see :count attributes |
||
| 173 | */ |
||
| 174 | public function iShouldSeeAttributes($count) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @Then the first attribute should be :name |
||
| 181 | */ |
||
| 182 | public function theFirstAttributeShouldBe($name) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @Then the last attribute should be :name |
||
| 191 | */ |
||
| 192 | public function theLastAttributeShouldBe($name) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @When /^I browse products from (taxon "([^"]+)")$/ |
||
| 201 | */ |
||
| 202 | public function iCheckListOfProductsForTaxon(TaxonInterface $taxon) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @When I search for products with name :name |
||
| 209 | */ |
||
| 210 | public function iSearchForProductsWithName($name) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @When I sort products by the lowest price first |
||
| 217 | */ |
||
| 218 | public function iSortProductsByTheLowestPriceFirst() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @When I sort products by the highest price first |
||
| 225 | */ |
||
| 226 | public function iSortProductsByTheHighestPriceFisrt() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @When I sort products alphabetically from a to z |
||
| 233 | */ |
||
| 234 | public function iSortProductsAlphabeticallyFromAToZ() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @When I sort products alphabetically from z to a |
||
| 241 | */ |
||
| 242 | public function iSortProductsAlphabeticallyFromZToA() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @When I clear filter |
||
| 249 | */ |
||
| 250 | public function iClearFilter() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @Then I should see the product :productName |
||
| 257 | */ |
||
| 258 | public function iShouldSeeProduct($productName) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @Then I should not see the product :productName |
||
| 265 | */ |
||
| 266 | public function iShouldNotSeeProduct($productName) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @Then I should see empty list of products |
||
| 273 | */ |
||
| 274 | public function iShouldSeeEmptyListOfProducts() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @Then I should see that it is out of stock |
||
| 281 | */ |
||
| 282 | public function iShouldSeeItIsOutOfStock() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @Then I should be unable to add it to the cart |
||
| 289 | */ |
||
| 290 | public function iShouldBeUnableToAddItToTheCart() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @Then the product price should be :price |
||
| 297 | * @Then I should see the product price :price |
||
| 298 | * @Then I should see that the combination is :price |
||
| 299 | */ |
||
| 300 | public function iShouldSeeTheProductPrice($price) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @Then the product original price should be :price |
||
| 307 | * @Then I should see the product original price :price |
||
| 308 | */ |
||
| 309 | public function iShouldSeeTheProductOriginalPrice($price) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @Then I should not see any original price |
||
| 317 | */ |
||
| 318 | public function iShouldNotSeeTheProductOriginalPrice(): void |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @When I set its :optionName to :optionValue |
||
| 325 | */ |
||
| 326 | public function iSetItsOptionTo($optionName, $optionValue) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @When I select :variantName variant |
||
| 333 | */ |
||
| 334 | public function iSelectVariant($variantName) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @Then its current variant should be named :name |
||
| 341 | */ |
||
| 342 | public function itsCurrentVariantShouldBeNamed($name) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @Then I should see the product :productName with price :productPrice |
||
| 349 | */ |
||
| 350 | public function iShouldSeeTheProductWithPrice($productName, $productPrice) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @Then /^I should be notified that (this product) does not have sufficient stock$/ |
||
| 357 | */ |
||
| 358 | public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @Then /^I should not be notified that (this product) does not have sufficient stock$/ |
||
| 365 | */ |
||
| 366 | public function iShouldNotBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @Then I should see a main image |
||
| 373 | */ |
||
| 374 | public function iShouldSeeAMainImage() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @When /^I view (oldest|newest) products from (taxon "([^"]+)")$/ |
||
| 381 | */ |
||
| 382 | public function iViewSortedProductsFromTaxon($sortDirection, TaxonInterface $taxon) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @Then I should see :numberOfProducts products in the list |
||
| 391 | */ |
||
| 392 | public function iShouldSeeProductsInTheList($numberOfProducts) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @Then I should see a product with name :name |
||
| 399 | */ |
||
| 400 | public function iShouldSeeProductWithName($name) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @Then the first product on the list should have name :name |
||
| 407 | */ |
||
| 408 | public function theFirstProductOnTheListShouldHaveName($name) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @Then the first product on the list should have name :name and price :price |
||
| 415 | */ |
||
| 416 | public function theFirstProductOnTheListShouldHaveNameAndPrice($name, $price) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @Then the last product on the list should have name :name |
||
| 424 | */ |
||
| 425 | public function theLastProductOnTheListShouldHaveName($name) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @Then the last product on the list should have name :name and price :price |
||
| 432 | */ |
||
| 433 | public function theLastProductOnTheListShouldHaveNameAndPrice($name, $price) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @Then I should see :count product reviews |
||
| 441 | */ |
||
| 442 | public function iShouldSeeProductReviews($count) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @Then I should see reviews titled :firstReview, :secondReview and :thirdReview |
||
| 449 | */ |
||
| 450 | public function iShouldSeeReviewsTitled(...$reviews) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @Then I should not see review titled :title |
||
| 462 | */ |
||
| 463 | public function iShouldNotSeeReviewTitled($title) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @When /^I check (this product)'s reviews$/ |
||
| 470 | */ |
||
| 471 | public function iCheckThisProductSReviews(ProductInterface $product) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @Then /^I should see (\d+) product reviews in the list$/ |
||
| 478 | */ |
||
| 479 | public function iShouldSeeNumberOfProductReviewsInTheList($count) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @Then I should not see review titled :title in the list |
||
| 486 | */ |
||
| 487 | public function iShouldNotSeeReviewTitledInTheList($title) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @Then /^I should be notified that there are no reviews$/ |
||
| 494 | */ |
||
| 495 | public function iShouldBeNotifiedThatThereAreNoReviews() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @Then I should see :rating as its average rating |
||
| 502 | */ |
||
| 503 | public function iShouldSeeAsItsAverageRating($rating) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @Then /^I should(?:| also) see the product association "([^"]+)" with (products "[^"]+" and "[^"]+")$/ |
||
| 510 | */ |
||
| 511 | public function iShouldSeeTheProductAssociationWithProducts($productAssociationName, array $products) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @Then /^average rating of (product "[^"]+") should be (\d+)$/ |
||
| 525 | */ |
||
| 526 | public function thisProductAverageRatingShouldBe(ProductInterface $product, $averageRating) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @Then they should have order like :firstProductName, :secondProductName and :thirdProductName |
||
| 534 | */ |
||
| 535 | public function theyShouldHaveOrderLikeAnd(...$productNames) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @Then I should be informed that the product does not exist |
||
| 542 | */ |
||
| 543 | public function iShouldBeInformedThatTheProductDoesNotExist() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @Then /^I should be able to select between (\d+) variants$/ |
||
| 550 | */ |
||
| 551 | public function iShouldBeAbleToSelectBetweenVariants(int $count) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @Then /^I should not be able to select the ("([^"]*)" variant)$/ |
||
| 558 | */ |
||
| 559 | public function iShouldNotBeAbleToSelectTheVariant(ProductVariantInterface $productVariant) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @Then /^I should not be able to select the "([^"]*)" ([^\s]+) option value$/ |
||
| 566 | */ |
||
| 567 | public function iShouldNotBeAbleToSelectTheOptionValue(string $optionValue, string $optionName) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @Then /^I should be able to select the "([^"]*)" and "([^"]*)" ([^\s]+) option values$/ |
||
| 574 | */ |
||
| 575 | public function iShouldBeAbleToSelectTheAndColorOptionValues( |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @param string $productName |
||
| 586 | * @param string $productAssociationName |
||
| 587 | * |
||
| 588 | * @throws \InvalidArgumentException |
||
| 589 | */ |
||
| 590 | private function assertProductIsInAssociation($productName, $productAssociationName) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @return NodeElement[] |
||
| 604 | * |
||
| 605 | * @throws \InvalidArgumentException |
||
| 606 | */ |
||
| 607 | private function getProductAttributes() |
||
| 614 | } |
||
| 615 |