Complex classes like ManagingOrdersContext 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 ManagingOrdersContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | final class ManagingOrdersContext implements Context |
||
34 | { |
||
35 | /** |
||
36 | * @var SharedStorageInterface |
||
37 | */ |
||
38 | private $sharedStorage; |
||
39 | |||
40 | /** |
||
41 | * @var IndexPageInterface |
||
42 | */ |
||
43 | private $indexPage; |
||
44 | |||
45 | /** |
||
46 | * @var ShowPageInterface |
||
47 | */ |
||
48 | private $showPage; |
||
49 | |||
50 | /** |
||
51 | * @var UpdatePageInterface |
||
52 | */ |
||
53 | private $updatePage; |
||
54 | |||
55 | /** |
||
56 | * @var NotificationCheckerInterface |
||
57 | */ |
||
58 | private $notificationChecker; |
||
59 | |||
60 | /** |
||
61 | * @var SharedSecurityServiceInterface |
||
62 | */ |
||
63 | private $sharedSecurityService; |
||
64 | |||
65 | /** |
||
66 | * @param SharedStorageInterface $sharedStorage |
||
67 | * @param IndexPageInterface $indexPage |
||
68 | * @param ShowPageInterface $showPage |
||
69 | * @param UpdatePageInterface $updatePage |
||
70 | * @param NotificationCheckerInterface $notificationChecker |
||
71 | * @param SharedSecurityServiceInterface $sharedSecurityService |
||
72 | */ |
||
73 | public function __construct( |
||
88 | |||
89 | /** |
||
90 | * @Given I am browsing orders |
||
91 | * @When I browse orders |
||
92 | */ |
||
93 | public function iBrowseOrders() |
||
97 | |||
98 | /** |
||
99 | * @Given /^I am viewing the summary of (this order)$/ |
||
100 | * @When I view the summary of the order :order |
||
101 | * @When /^I view the summary of (this order made by "[^"]+")$/ |
||
102 | */ |
||
103 | public function iSeeTheOrder(OrderInterface $order) |
||
107 | |||
108 | /** |
||
109 | * @When /^I mark (this order) as paid$/ |
||
110 | */ |
||
111 | public function iMarkThisOrderAsAPaid(OrderInterface $order) |
||
115 | |||
116 | /** |
||
117 | * @When /^I mark (this order)'s payment as refunded$/ |
||
118 | */ |
||
119 | public function iMarkThisOrderSPaymentAsRefunded(OrderInterface $order) |
||
123 | |||
124 | /** |
||
125 | * @When specify its tracking code as :trackingCode |
||
126 | */ |
||
127 | public function specifyItsTrackingCodeAs($trackingCode) |
||
132 | |||
133 | /** |
||
134 | * @When /^I ship (this order)$/ |
||
135 | */ |
||
136 | public function iShipThisOrder(OrderInterface $order) |
||
140 | |||
141 | /** |
||
142 | * @When I switch the way orders are sorted by :fieldName |
||
143 | */ |
||
144 | public function iSwitchSortingBy($fieldName) |
||
148 | |||
149 | /** |
||
150 | * @Then I should see a single order from customer :customer |
||
151 | */ |
||
152 | public function iShouldSeeASingleOrderFromCustomer(CustomerInterface $customer) |
||
159 | |||
160 | /** |
||
161 | * @Then it should have been placed by the customer :customerEmail |
||
162 | */ |
||
163 | public function itShouldBePlacedByCustomer($customerEmail) |
||
170 | |||
171 | /** |
||
172 | * @Then it should be shipped to :customerName, :street, :postcode, :city, :countryName |
||
173 | * @Then /^(this order) should (?:|still )be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/ |
||
174 | */ |
||
175 | public function itShouldBeShippedTo( |
||
192 | |||
193 | /** |
||
194 | * @Then it should be billed to :customerName, :street, :postcode, :city, :countryName |
||
195 | * @Then /^(this order) bill should (?:|still )be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/ |
||
196 | */ |
||
197 | public function itShouldBeBilledTo( |
||
214 | |||
215 | /** |
||
216 | * @Then it should be shipped via the :shippingMethodName shipping method |
||
217 | */ |
||
218 | public function itShouldBeShippedViaShippingMethod($shippingMethodName) |
||
225 | |||
226 | /** |
||
227 | * @Then it should be paid with :paymentMethodName |
||
228 | */ |
||
229 | public function itShouldBePaidWith($paymentMethodName) |
||
236 | |||
237 | /** |
||
238 | * @Then /^it should have (\d+) items$/ |
||
239 | */ |
||
240 | public function itShouldHaveAmountOfItems($amount) |
||
250 | |||
251 | /** |
||
252 | * @Then the product named :productName should be in the items list |
||
253 | */ |
||
254 | public function theProductShouldBeInTheItemsList($productName) |
||
261 | |||
262 | /** |
||
263 | * @Then the order's items total should be :itemsTotal |
||
264 | */ |
||
265 | public function theOrdersItemsTotalShouldBe($itemsTotal) |
||
275 | |||
276 | /** |
||
277 | * @Then /^the order's total should(?:| still) be "([^"]+)"$/ |
||
278 | */ |
||
279 | public function theOrdersTotalShouldBe($total) |
||
289 | |||
290 | /** |
||
291 | * @Then there should be a shipping charge :shippingCharge |
||
292 | */ |
||
293 | public function theOrdersShippingChargesShouldBe($shippingCharge) |
||
300 | |||
301 | /** |
||
302 | * @Then the order's shipping total should be :shippingTotal |
||
303 | */ |
||
304 | public function theOrdersShippingTotalShouldBe($shippingTotal) |
||
314 | |||
315 | /** |
||
316 | * @Then the order's payment should (also) be :paymentAmount |
||
317 | */ |
||
318 | public function theOrdersPaymentShouldBe($paymentAmount) |
||
324 | |||
325 | /** |
||
326 | * @Then the order should have tax :tax |
||
327 | */ |
||
328 | public function theOrderShouldHaveTax($tax) |
||
335 | |||
336 | /** |
||
337 | * @Then /^the order's tax total should(?:| still) be "([^"]+)"$/ |
||
338 | */ |
||
339 | public function theOrdersTaxTotalShouldBe($taxTotal) |
||
349 | |||
350 | /** |
||
351 | * @Then the order's promotion discount should be :promotionDiscount |
||
352 | */ |
||
353 | public function theOrdersPromotionDiscountShouldBe($promotionDiscount) |
||
360 | |||
361 | /** |
||
362 | * @Then /^the order's promotion total should(?:| still) be "([^"]+)"$/ |
||
363 | */ |
||
364 | public function theOrdersPromotionTotalShouldBe($promotionTotal) |
||
374 | |||
375 | /** |
||
376 | * @When I check :itemName data |
||
377 | */ |
||
378 | public function iCheckData($itemName) |
||
382 | |||
383 | /** |
||
384 | * @Then /^(its) code should be "([^"]+)"$/ |
||
385 | */ |
||
386 | public function itemCodeShouldBe($itemName, $code) |
||
396 | |||
397 | /** |
||
398 | * @Then /^(its) unit price should be ([^"]+)$/ |
||
399 | */ |
||
400 | public function itemUnitPriceShouldBe($itemName, $unitPrice) |
||
410 | |||
411 | /** |
||
412 | * @Then /^(its) discounted unit price should be ([^"]+)$/ |
||
413 | */ |
||
414 | public function itemDiscountedUnitPriceShouldBe($itemName, $discountedUnitPrice) |
||
424 | |||
425 | /** |
||
426 | * @Then /^(its) quantity should be ([^"]+)$/ |
||
427 | */ |
||
428 | public function itemQuantityShouldBe($itemName, $quantity) |
||
438 | |||
439 | /** |
||
440 | * @Then /^(its) subtotal should be ([^"]+)$/ |
||
441 | */ |
||
442 | public function itemSubtotalShouldBe($itemName, $subtotal) |
||
452 | |||
453 | /** |
||
454 | * @Then /^(its) discount should be ([^"]+)$/ |
||
455 | * @Then the :itemName should have :discount discount |
||
456 | */ |
||
457 | public function theItemShouldHaveDiscount($itemName, $discount) |
||
467 | |||
468 | /** |
||
469 | * @Then /^(its) tax should be ([^"]+)$/ |
||
470 | */ |
||
471 | public function itemTaxShouldBe($itemName, $tax) |
||
481 | |||
482 | /** |
||
483 | * @Then /^(its) total should be ([^"]+)$/ |
||
484 | */ |
||
485 | public function itemTotalShouldBe($itemName, $total) |
||
495 | |||
496 | /** |
||
497 | * @Then I should be notified that the order's payment has been successfully completed |
||
498 | */ |
||
499 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyCompleted() |
||
505 | |||
506 | /** |
||
507 | * @Then I should be notified that the order's payment has been successfully refunded |
||
508 | */ |
||
509 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyRefunded() |
||
515 | |||
516 | /** |
||
517 | * @Then it should have payment state :paymentState |
||
518 | * @Then it should have payment with state :paymentState |
||
519 | */ |
||
520 | public function itShouldHavePaymentState($paymentState) |
||
527 | |||
528 | /** |
||
529 | * @Then it's payment state should be refunded |
||
530 | */ |
||
531 | public function orderPaymentStateShouldBeRefunded() |
||
539 | |||
540 | /** |
||
541 | * @Then /^I should not be able to mark (this order) as paid again$/ |
||
542 | */ |
||
543 | public function iShouldNotBeAbleToFinalizeItsPayment(OrderInterface $order) |
||
550 | |||
551 | /** |
||
552 | * @Then I should be notified that the order has been successfully shipped |
||
553 | */ |
||
554 | public function iShouldBeNotifiedThatTheOrderHasBeenSuccessfullyShipped() |
||
561 | |||
562 | /** |
||
563 | * @Then /^I should not be able to ship (this order)$/ |
||
564 | */ |
||
565 | public function iShouldNotBeAbleToShipThisOrder(OrderInterface $order) |
||
572 | |||
573 | /** |
||
574 | * @When I cancel this order |
||
575 | */ |
||
576 | public function iCancelThisOrder() |
||
580 | |||
581 | /** |
||
582 | * @Then I should be notified that it has been successfully updated |
||
583 | */ |
||
584 | public function iShouldBeNotifiedAboutItHasBeenSuccessfullyCanceled() |
||
591 | |||
592 | /** |
||
593 | * @Then I should not be able to cancel this order |
||
594 | */ |
||
595 | public function iShouldNotBeAbleToCancelThisOrder() |
||
602 | |||
603 | /** |
||
604 | * @Then this order should have state :state |
||
605 | * @Then its state should be :state |
||
606 | */ |
||
607 | public function itsStateShouldBe($state) |
||
615 | |||
616 | /** |
||
617 | * @Then it should( still) have a :state state |
||
618 | */ |
||
619 | public function itShouldHaveState($state) |
||
626 | |||
627 | /** |
||
628 | * @Then /^(the administrator) should know about (this additional note) for (this order made by "[^"]+")$/ |
||
629 | */ |
||
630 | public function theCustomerServiceShouldKnowAboutThisAdditionalNotes( |
||
643 | |||
644 | /** |
||
645 | * @Then I should see an order with :orderNumber number |
||
646 | */ |
||
647 | public function iShouldSeeOrderWithNumber($orderNumber) |
||
654 | |||
655 | /** |
||
656 | * @Then the first order should have number :number |
||
657 | */ |
||
658 | public function theFirstOrderShouldHaveNumber($number) |
||
668 | |||
669 | /** |
||
670 | * @Then it should have shipment in state :shipmentState |
||
671 | */ |
||
672 | public function itShouldHaveShipmentState($shipmentState) |
||
679 | |||
680 | /** |
||
681 | * @Then order :orderNumber should have shipment state :shippingState |
||
682 | */ |
||
683 | public function thisOrderShipmentStateShouldBe($shippingState) |
||
690 | |||
691 | /** |
||
692 | * @Then the order :order should have order payment state :orderPaymentState |
||
693 | * @Then /^(this order) should have order payment state "([^"]+)"$/ |
||
694 | * @Then /^(its) payment state should be "([^"]+)"$/ |
||
695 | */ |
||
696 | public function theOrderShouldHavePaymentState(OrderInterface $order, $orderPaymentState) |
||
703 | |||
704 | /** |
||
705 | * @Then the order :order should have order shipping state :orderShipmentState |
||
706 | * @Then /^(this order) should have order shipping state "([^"]+)"$/ |
||
707 | * @Then /^(its) shipping state should be "([^"]+)"$/ |
||
708 | */ |
||
709 | public function theOrderShouldHaveShipmentState(OrderInterface $order, $orderShipmentState) |
||
716 | |||
717 | /** |
||
718 | * @Then /^there should be(?:| only) (\d+) payments?$/ |
||
719 | */ |
||
720 | public function theOrderShouldHaveNumberOfPayments($number) |
||
726 | |||
727 | /** |
||
728 | * @Then I should see the order :orderNumber with total :total |
||
729 | */ |
||
730 | public function iShouldSeeTheOrderWithTotal($orderNumber, $total) |
||
737 | |||
738 | /** |
||
739 | * @When /^I want to modify a customer's (?:billing|shipping) address of (this order)$/ |
||
740 | */ |
||
741 | public function iWantToModifyACustomerSShippingAddress(OrderInterface $order) |
||
745 | |||
746 | /** |
||
747 | * @When I save my changes |
||
748 | * @When I try to save my changes |
||
749 | */ |
||
750 | public function iSaveMyChanges() |
||
754 | |||
755 | /** |
||
756 | * @When /^I specify their (?:|new )shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
757 | */ |
||
758 | public function iSpecifyTheirShippingAddressAsFor(AddressInterface $address) |
||
762 | |||
763 | /** |
||
764 | * @When /^I specify their (?:|new )billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
765 | */ |
||
766 | public function iSpecifyTheirBillingAddressAsFor(AddressInterface $address) |
||
770 | |||
771 | /** |
||
772 | * @Then /^I should be notified that the "([^"]+)", the "([^"]+)", the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
773 | */ |
||
774 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $thirdElement, $fourthElement, $type) |
||
782 | |||
783 | /** |
||
784 | * @Then I should see :provinceName as province in the shipping address |
||
785 | */ |
||
786 | public function iShouldSeeAsProvinceInTheShippingAddress($provinceName) |
||
793 | |||
794 | /** |
||
795 | * @Then I should see :provinceName ad province in the billing address |
||
796 | */ |
||
797 | public function iShouldSeeAdProvinceInTheBillingAddress($provinceName) |
||
804 | |||
805 | /** |
||
806 | * @Then /^(the administrator) should know about IP address of (this order made by "[^"]+")$/ |
||
807 | */ |
||
808 | public function theAdministratorShouldKnowAboutIPAddressOfThisOrderMadeBy( |
||
825 | |||
826 | /** |
||
827 | * @When /^I (clear old billing address) information$/ |
||
828 | */ |
||
829 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
833 | |||
834 | /** |
||
835 | * @When /^I (clear old shipping address) information$/ |
||
836 | */ |
||
837 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
841 | |||
842 | /** |
||
843 | * @When /^I do not specify new information$/ |
||
844 | */ |
||
845 | public function iDoNotSpecifyNewInformation() |
||
849 | |||
850 | /** |
||
851 | * @param string $type |
||
852 | * @param string $element |
||
853 | * @param string $expectedMessage |
||
854 | * |
||
855 | * @throws \InvalidArgumentException |
||
856 | */ |
||
857 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
865 | } |
||
866 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.