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 |
||
| 26 | final class ProductContext implements Context |
||
| 27 | { |
||
| 28 | /** @var ShowPageInterface */ |
||
| 29 | private $showPage; |
||
| 30 | |||
| 31 | /** @var IndexPageInterface */ |
||
| 32 | private $indexPage; |
||
| 33 | |||
| 34 | /** @var ProductReviewIndexPageInterface */ |
||
| 35 | private $productReviewsIndexPage; |
||
| 36 | |||
| 37 | /** @var ErrorPageInterface */ |
||
| 38 | private $errorPage; |
||
| 39 | |||
| 40 | public function __construct( |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @Then I should be able to access product :product |
||
| 54 | */ |
||
| 55 | public function iShouldBeAbleToAccessProduct(ProductInterface $product) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @Then I should not be able to access product :product |
||
| 64 | */ |
||
| 65 | public function iShouldNotBeAbleToAccessProduct(ProductInterface $product) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @When /^I check (this product)'s details$/ |
||
| 74 | * @When /^I check (this product)'s details in the ("([^"]+)" locale)$/ |
||
| 75 | * @When I view product :product |
||
| 76 | * @When I view product :product in the :localeCode locale |
||
| 77 | */ |
||
| 78 | public function iOpenProductPage(ProductInterface $product, $localeCode = 'en_US') |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @When /^I try to check (this product)'s details in the ("([^"]+)" locale)$/ |
||
| 85 | */ |
||
| 86 | public function iTryToOpenProductPage(ProductInterface $product, $localeCode = 'en_US') |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @When I try to reach unexistent product |
||
| 96 | */ |
||
| 97 | public function iTryToReachUnexistentProductPage($localeCode = 'en_US') |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @Then /^I should not be able to view (this product) in the ("([^"]+)" locale)$/ |
||
| 107 | */ |
||
| 108 | public function iShouldNotBeAbleToViewThisProductInLocale(ProductInterface $product, $localeCode = 'en_US') |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @Then I should see the product name :name |
||
| 120 | */ |
||
| 121 | public function iShouldSeeProductName($name) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @When I open page :url |
||
| 128 | */ |
||
| 129 | public function iOpenPage($url) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @Then I should be on :product product detailed page |
||
| 136 | * @Then I should still be on product :product page |
||
| 137 | */ |
||
| 138 | public function iShouldBeOnProductDetailedPage(ProductInterface $product) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @Then I should (also) see the product attribute :attributeName with value :expectedAttribute |
||
| 145 | */ |
||
| 146 | public function iShouldSeeTheProductAttributeWithValue($attributeName, $expectedAttribute) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @Then I should not see the product attribute :attributeName |
||
| 153 | */ |
||
| 154 | public function iShouldNotSeeTheProductAttribute(string $attributeName): void |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @Then I should (also) see the product attribute :attributeName with date :expectedAttribute |
||
| 161 | */ |
||
| 162 | public function iShouldSeeTheProductAttributeWithDate($attributeName, $expectedAttribute) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @Then I should see :count attributes |
||
| 172 | */ |
||
| 173 | public function iShouldSeeAttributes($count) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @Then the first attribute should be :name |
||
| 180 | */ |
||
| 181 | public function theFirstAttributeShouldBe($name) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @Then the last attribute should be :name |
||
| 190 | */ |
||
| 191 | public function theLastAttributeShouldBe($name) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @When /^I browse products from (taxon "([^"]+)")$/ |
||
| 200 | */ |
||
| 201 | public function iCheckListOfProductsForTaxon(TaxonInterface $taxon) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @When I search for products with name :name |
||
| 208 | */ |
||
| 209 | public function iSearchForProductsWithName($name) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @When I sort products by the lowest price first |
||
| 216 | */ |
||
| 217 | public function iSortProductsByTheLowestPriceFirst() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @When I sort products by the highest price first |
||
| 224 | */ |
||
| 225 | public function iSortProductsByTheHighestPriceFisrt() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @When I sort products alphabetically from a to z |
||
| 232 | */ |
||
| 233 | public function iSortProductsAlphabeticallyFromAToZ() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @When I sort products alphabetically from z to a |
||
| 240 | */ |
||
| 241 | public function iSortProductsAlphabeticallyFromZToA() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @When I clear filter |
||
| 248 | */ |
||
| 249 | public function iClearFilter() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @Then I should see the product :productName |
||
| 256 | */ |
||
| 257 | public function iShouldSeeProduct($productName) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @Then I should not see the product :productName |
||
| 264 | */ |
||
| 265 | public function iShouldNotSeeProduct($productName) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @Then I should see empty list of products |
||
| 272 | */ |
||
| 273 | public function iShouldSeeEmptyListOfProducts() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @Then I should see that it is out of stock |
||
| 280 | */ |
||
| 281 | public function iShouldSeeItIsOutOfStock() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @Then I should be unable to add it to the cart |
||
| 288 | */ |
||
| 289 | public function iShouldBeUnableToAddItToTheCart() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @Then the product price should be :price |
||
| 296 | * @Then I should see the product price :price |
||
| 297 | */ |
||
| 298 | public function iShouldSeeTheProductPrice($price) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @Then the product original price should be :price |
||
| 305 | * @Then I should see the product original price :price |
||
| 306 | */ |
||
| 307 | public function iShouldSeeTheProductOriginalPrice($price) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @When I set its :optionName to :optionValue |
||
| 314 | */ |
||
| 315 | public function iSetItsOptionTo($optionName, $optionValue) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @When I select :variantName variant |
||
| 322 | */ |
||
| 323 | public function iSelectVariant($variantName) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @Then its current variant should be named :name |
||
| 330 | */ |
||
| 331 | public function itsCurrentVariantShouldBeNamed($name) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @Then I should see the product :productName with price :productPrice |
||
| 338 | */ |
||
| 339 | public function iShouldSeeTheProductWithPrice($productName, $productPrice) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @Then /^I should be notified that (this product) does not have sufficient stock$/ |
||
| 346 | */ |
||
| 347 | public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @Then /^I should not be notified that (this product) does not have sufficient stock$/ |
||
| 354 | */ |
||
| 355 | public function iShouldNotBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @Then I should see a main image |
||
| 362 | */ |
||
| 363 | public function iShouldSeeAMainImage() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @When /^I view (oldest|newest) products from (taxon "([^"]+)")$/ |
||
| 370 | */ |
||
| 371 | public function iViewSortedProductsFromTaxon($sortDirection, TaxonInterface $taxon) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @Then I should see :numberOfProducts products in the list |
||
| 380 | */ |
||
| 381 | public function iShouldSeeProductsInTheList($numberOfProducts) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @Then I should see a product with name :name |
||
| 388 | */ |
||
| 389 | public function iShouldSeeProductWithName($name) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Then the first product on the list should have name :name |
||
| 396 | */ |
||
| 397 | public function theFirstProductOnTheListShouldHaveName($name) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @Then the first product on the list should have name :name and price :price |
||
| 404 | */ |
||
| 405 | public function theFirstProductOnTheListShouldHaveNameAndPrice($name, $price) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @Then the last product on the list should have name :name |
||
| 413 | */ |
||
| 414 | public function theLastProductOnTheListShouldHaveName($name) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @Then the last product on the list should have name :name and price :price |
||
| 421 | */ |
||
| 422 | public function theLastProductOnTheListShouldHaveNameAndPrice($name, $price) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @Then I should see :count product reviews |
||
| 430 | */ |
||
| 431 | public function iShouldSeeProductReviews($count) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @Then I should see reviews titled :firstReview, :secondReview and :thirdReview |
||
| 438 | */ |
||
| 439 | public function iShouldSeeReviewsTitled(...$reviews) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @Then I should not see review titled :title |
||
| 451 | */ |
||
| 452 | public function iShouldNotSeeReviewTitled($title) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @When /^I check (this product)'s reviews$/ |
||
| 459 | */ |
||
| 460 | public function iCheckThisProductSReviews(ProductInterface $product) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @Then /^I should see (\d+) product reviews in the list$/ |
||
| 467 | */ |
||
| 468 | public function iShouldSeeNumberOfProductReviewsInTheList($count) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @Then I should not see review titled :title in the list |
||
| 475 | */ |
||
| 476 | public function iShouldNotSeeReviewTitledInTheList($title) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @Then /^I should be notified that there are no reviews$/ |
||
| 483 | */ |
||
| 484 | public function iShouldBeNotifiedThatThereAreNoReviews() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @Then I should see :rating as its average rating |
||
| 491 | */ |
||
| 492 | public function iShouldSeeAsItsAverageRating($rating) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @Then /^I should(?:| also) see the product association "([^"]+)" with (products "[^"]+" and "[^"]+")$/ |
||
| 499 | */ |
||
| 500 | public function iShouldSeeTheProductAssociationWithProducts($productAssociationName, array $products) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @Then /^average rating of (product "[^"]+") should be (\d+)$/ |
||
| 514 | */ |
||
| 515 | public function thisProductAverageRatingShouldBe(ProductInterface $product, $averageRating) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @Then they should have order like :firstProductName, :secondProductName and :thirdProductName |
||
| 523 | */ |
||
| 524 | public function theyShouldHaveOrderLikeAnd(...$productNames) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @Then I should be informed that the product does not exist |
||
| 531 | */ |
||
| 532 | public function iShouldBeInformedThatTheProductDoesNotExist() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param string $productName |
||
| 539 | * @param string $productAssociationName |
||
| 540 | * |
||
| 541 | * @throws \InvalidArgumentException |
||
| 542 | */ |
||
| 543 | private function assertProductIsInAssociation($productName, $productAssociationName) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @return NodeElement[] |
||
| 557 | * |
||
| 558 | * @throws \InvalidArgumentException |
||
| 559 | */ |
||
| 560 | private function getProductAttributes() |
||
| 567 | } |
||
| 568 |