Complex classes like CartContext 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 CartContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | final class CartContext implements Context |
||
28 | { |
||
29 | /** @var SharedStorageInterface */ |
||
30 | private $sharedStorage; |
||
31 | |||
32 | /** @var SummaryPageInterface */ |
||
33 | private $summaryPage; |
||
34 | |||
35 | /** @var ShowPageInterface */ |
||
36 | private $productShowPage; |
||
37 | |||
38 | /** @var NotificationCheckerInterface */ |
||
39 | private $notificationChecker; |
||
40 | |||
41 | public function __construct( |
||
52 | |||
53 | /** |
||
54 | * @When I see the summary of my cart |
||
55 | */ |
||
56 | public function iOpenCartSummaryPage() |
||
60 | |||
61 | /** |
||
62 | * @When I update my cart |
||
63 | */ |
||
64 | public function iUpdateMyCart() |
||
68 | |||
69 | /** |
||
70 | * @Then my cart should be empty |
||
71 | * @Then cart should be empty with no value |
||
72 | */ |
||
73 | public function iShouldBeNotifiedThatMyCartIsEmpty() |
||
79 | |||
80 | /** |
||
81 | * @Given I removed product :productName from the cart |
||
82 | * @When I remove product :productName from the cart |
||
83 | */ |
||
84 | public function iRemoveProductFromTheCart(string $productName): void |
||
89 | |||
90 | /** |
||
91 | * @Given I change :productName quantity to :quantity |
||
92 | */ |
||
93 | public function iChangeQuantityTo($productName, $quantity) |
||
98 | |||
99 | /** |
||
100 | * @Then the grand total value should be :total |
||
101 | * @Then my cart total should be :total |
||
102 | */ |
||
103 | public function myCartTotalShouldBe($total) |
||
109 | |||
110 | /** |
||
111 | * @Then the grand total value in base currency should be :total |
||
112 | */ |
||
113 | public function myBaseCartTotalShouldBe($total) |
||
119 | |||
120 | /** |
||
121 | * @Then my cart taxes should be :taxTotal |
||
122 | */ |
||
123 | public function myCartTaxesShouldBe(string $taxTotal): void |
||
129 | |||
130 | /** |
||
131 | * @Then my included in price taxes should be :taxTotal |
||
132 | */ |
||
133 | public function myIncludedInPriceTaxesShouldBe(string $taxTotal): void |
||
139 | |||
140 | /** |
||
141 | * @Then there should be no taxes charged |
||
142 | */ |
||
143 | public function thereShouldBeNoTaxesCharged(): void |
||
149 | |||
150 | /** |
||
151 | * @Then my cart shipping total should be :shippingTotal |
||
152 | * @Then my cart shipping should be for free |
||
153 | */ |
||
154 | public function myCartShippingFeeShouldBe($shippingTotal = '$0.00') |
||
160 | |||
161 | /** |
||
162 | * @Then I should not see shipping total for my cart |
||
163 | */ |
||
164 | public function iShouldNotSeeShippingTotalForMyCart(): void |
||
170 | |||
171 | /** |
||
172 | * @Then my discount should be :promotionsTotal |
||
173 | */ |
||
174 | public function myDiscountShouldBe($promotionsTotal) |
||
180 | |||
181 | /** |
||
182 | * @Given /^there should be no shipping fee$/ |
||
183 | */ |
||
184 | public function thereShouldBeNoShippingFee() |
||
196 | |||
197 | /** |
||
198 | * @Given /^there should be no discount$/ |
||
199 | */ |
||
200 | public function thereShouldBeNoDiscount() |
||
212 | |||
213 | /** |
||
214 | * @Then /^(its|theirs) price should be decreased by ("[^"]+")$/ |
||
215 | * @Then /^(product "[^"]+") price should be decreased by ("[^"]+")$/ |
||
216 | */ |
||
217 | public function itsPriceShouldBeDecreasedBy(ProductInterface $product, $amount) |
||
227 | |||
228 | /** |
||
229 | * @Then /^(product "[^"]+") price should not be decreased$/ |
||
230 | */ |
||
231 | public function productPriceShouldNotBeDecreased(ProductInterface $product) |
||
237 | |||
238 | /** |
||
239 | * @Given /^I (?:add|added) (this product) to the cart$/ |
||
240 | * @Given I added product :product to the cart |
||
241 | * @Given /^I (?:have|had) (product "[^"]+") in the cart$/ |
||
242 | * @Given the customer added :product product to the cart |
||
243 | * @When I add product :product to the cart |
||
244 | */ |
||
245 | public function iAddProductToTheCart(ProductInterface $product): void |
||
252 | |||
253 | /** |
||
254 | * @When /^I add (products "([^"]+)" and "([^"]+)") to the cart$/ |
||
255 | * @When /^I add (products "([^"]+)", "([^"]+)" and "([^"]+)") to the cart$/ |
||
256 | */ |
||
257 | public function iAddMultipleProductsToTheCart(array $products) |
||
263 | |||
264 | /** |
||
265 | * @When I add :variantName variant of product :product to the cart |
||
266 | * @When /^I add "([^"]+)" variant of (this product) to the cart$/ |
||
267 | * @Given I have :variantName variant of product :product in the cart |
||
268 | */ |
||
269 | public function iAddProductToTheCartSelectingVariant($variantName, ProductInterface $product) |
||
276 | |||
277 | /** |
||
278 | * @When /^I add (\d+) of (them) to (?:the|my) cart$/ |
||
279 | */ |
||
280 | public function iAddQuantityOfProductsToTheCart($quantity, ProductInterface $product) |
||
285 | |||
286 | /** |
||
287 | * @Given /^I have(?:| added) (\d+) (products "([^"]+)") (?:to|in) the cart$/ |
||
288 | * @When /^I add(?:|ed)(?:| again) (\d+) (products "([^"]+)") to the cart$/ |
||
289 | */ |
||
290 | public function iAddProductsToTheCart($quantity, ProductInterface $product) |
||
297 | |||
298 | /** |
||
299 | * @Then /^I should be(?: on| redirected to) my cart summary page$/ |
||
300 | */ |
||
301 | public function shouldBeOnMyCartSummaryPage() |
||
307 | |||
308 | /** |
||
309 | * @Then I should be notified that the product has been successfully added |
||
310 | */ |
||
311 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyAdded() |
||
315 | |||
316 | /** |
||
317 | * @Then there should be one item in my cart |
||
318 | */ |
||
319 | public function thereShouldBeOneItemInMyCart() |
||
323 | |||
324 | /** |
||
325 | * @Then this item should have name :itemName |
||
326 | */ |
||
327 | public function thisProductShouldHaveName($itemName) |
||
331 | |||
332 | /** |
||
333 | * @Then this item should have variant :variantName |
||
334 | */ |
||
335 | public function thisItemShouldHaveVariant($variantName) |
||
339 | |||
340 | /** |
||
341 | * @Then this item should have code :variantCode |
||
342 | */ |
||
343 | public function thisItemShouldHaveCode($variantCode) |
||
347 | |||
348 | /** |
||
349 | * @Given I have :product with :productOption :productOptionValue in the cart |
||
350 | * @When I add :product with :productOption :productOptionValue to the cart |
||
351 | */ |
||
352 | public function iAddThisProductWithToTheCart(ProductInterface $product, ProductOptionInterface $productOption, $productOptionValue) |
||
358 | |||
359 | /** |
||
360 | * @Given /^(this product) should have ([^"]+) "([^"]+)"$/ |
||
361 | */ |
||
362 | public function thisItemShouldHaveOptionValue(ProductInterface $product, $optionName, $optionValue) |
||
366 | |||
367 | /** |
||
368 | * @When I clear my cart |
||
369 | */ |
||
370 | public function iClearMyCart() |
||
374 | |||
375 | /** |
||
376 | * @Then /^I should see "([^"]+)" with quantity (\d+) in my cart$/ |
||
377 | */ |
||
378 | public function iShouldSeeWithQuantityInMyCart($productName, $quantity) |
||
382 | |||
383 | /** |
||
384 | * @Then /^I should see "([^"]+)" with unit price ("[^"]+") in my cart$/ |
||
385 | */ |
||
386 | public function iShouldSeeProductWithUnitPriceInMyCart($productName, $unitPrice) |
||
390 | |||
391 | /** |
||
392 | * @Given I use coupon with code :couponCode |
||
393 | */ |
||
394 | public function iUseCouponWithCode($couponCode) |
||
398 | |||
399 | /** |
||
400 | * @Then I should be notified that the coupon is invalid |
||
401 | */ |
||
402 | public function iShouldBeNotifiedThatCouponIsInvalid() |
||
406 | |||
407 | /** |
||
408 | * @Then total price of :productName item should be :productPrice |
||
409 | */ |
||
410 | public function thisItemPriceShouldBe($productName, $productPrice) |
||
416 | |||
417 | /** |
||
418 | * @Then /^I should be notified that (this product) cannot be updated$/ |
||
419 | */ |
||
420 | public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product) |
||
424 | |||
425 | /** |
||
426 | * @Then /^I should not be notified that (this product) cannot be updated$/ |
||
427 | */ |
||
428 | public function iShouldNotBeNotifiedThatThisProductCannotBeUpdated(ProductInterface $product) |
||
432 | |||
433 | /** |
||
434 | * @Then my cart's total should be :total |
||
435 | */ |
||
436 | public function myCartSTotalShouldBe($total) |
||
442 | |||
443 | /** |
||
444 | * @Then /^(\d)(st|nd|rd|th) item in my cart should have "([^"]+)" image displayed$/ |
||
445 | */ |
||
446 | public function itemShouldHaveImageDisplayed(int $itemNumber, string $image): void |
||
450 | |||
451 | private function getPriceFromString(string $price): int |
||
455 | } |
||
456 |
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.