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 |
||
32 | final class ManagingOrdersContext implements Context |
||
33 | { |
||
34 | /** |
||
35 | * @var SharedStorageInterface |
||
36 | */ |
||
37 | private $sharedStorage; |
||
38 | |||
39 | /** |
||
40 | * @var IndexPageInterface |
||
41 | */ |
||
42 | private $indexPage; |
||
43 | |||
44 | /** |
||
45 | * @var ShowPageInterface |
||
46 | */ |
||
47 | private $showPage; |
||
48 | |||
49 | /** |
||
50 | * @var UpdatePageInterface |
||
51 | */ |
||
52 | private $updatePage; |
||
53 | |||
54 | /** |
||
55 | * @var NotificationCheckerInterface |
||
56 | */ |
||
57 | private $notificationChecker; |
||
58 | |||
59 | /** |
||
60 | * @var SharedSecurityServiceInterface |
||
61 | */ |
||
62 | private $sharedSecurityService; |
||
63 | |||
64 | /** |
||
65 | * @param SharedStorageInterface $sharedStorage |
||
66 | * @param IndexPageInterface $indexPage |
||
67 | * @param ShowPageInterface $showPage |
||
68 | * @param UpdatePageInterface $updatePage |
||
69 | * @param NotificationCheckerInterface $notificationChecker |
||
70 | * @param SharedSecurityServiceInterface $sharedSecurityService |
||
71 | */ |
||
72 | public function __construct( |
||
87 | |||
88 | /** |
||
89 | * @Given I am browsing orders |
||
90 | * @When I browse orders |
||
91 | */ |
||
92 | public function iBrowseOrders() |
||
96 | |||
97 | /** |
||
98 | * @Given /^I am viewing the summary of (this order)$/ |
||
99 | * @When I view the summary of the order :order |
||
100 | * @When /^I view the summary of (this order made by "[^"]+")$/ |
||
101 | */ |
||
102 | public function iSeeTheOrder(OrderInterface $order) |
||
106 | |||
107 | /** |
||
108 | * @When /^I mark (this order) as paid$/ |
||
109 | */ |
||
110 | public function iMarkThisOrderAsAPaid(OrderInterface $order) |
||
114 | |||
115 | /** |
||
116 | * @When /^I mark (this order)'s payment as refunded$/ |
||
117 | */ |
||
118 | public function iMarkThisOrderSPaymentAsRefunded(OrderInterface $order) |
||
122 | |||
123 | /** |
||
124 | * @When specify its tracking code as :trackingCode |
||
125 | */ |
||
126 | public function specifyItsTrackingCodeAs($trackingCode) |
||
131 | |||
132 | /** |
||
133 | * @When /^I ship (this order)$/ |
||
134 | */ |
||
135 | public function iShipThisOrder(OrderInterface $order) |
||
139 | |||
140 | /** |
||
141 | * @When I switch the way orders are sorted by :fieldName |
||
142 | */ |
||
143 | public function iSwitchSortingBy($fieldName) |
||
147 | |||
148 | /** |
||
149 | * @When I specify filter date from as :dateTime |
||
150 | */ |
||
151 | public function iSpecifyFilterDateFromAs($dateTime) |
||
155 | |||
156 | /** |
||
157 | * @When I specify filter date to as :dateTime |
||
158 | */ |
||
159 | public function iSpecifyFilterDateToAs($dateTime) |
||
163 | |||
164 | /** |
||
165 | * @When I choose :channelName as a channel filter |
||
166 | */ |
||
167 | public function iChooseChannelAsAChannelFilter($channelName) |
||
171 | |||
172 | /** |
||
173 | * @When I filter |
||
174 | */ |
||
175 | public function iFilter() |
||
179 | |||
180 | /** |
||
181 | * @Then I should see a single order from customer :customer |
||
182 | */ |
||
183 | public function iShouldSeeASingleOrderFromCustomer(CustomerInterface $customer) |
||
190 | |||
191 | /** |
||
192 | * @Then it should have been placed by the customer :customerEmail |
||
193 | */ |
||
194 | public function itShouldBePlacedByCustomer($customerEmail) |
||
201 | |||
202 | /** |
||
203 | * @Then it should be shipped to :customerName, :street, :postcode, :city, :countryName |
||
204 | * @Then /^(this order) should (?:|still )be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/ |
||
205 | */ |
||
206 | public function itShouldBeShippedTo( |
||
223 | |||
224 | /** |
||
225 | * @Then it should be billed to :customerName, :street, :postcode, :city, :countryName |
||
226 | * @Then /^(this order) bill should (?:|still )be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/ |
||
227 | */ |
||
228 | public function itShouldBeBilledTo( |
||
245 | |||
246 | /** |
||
247 | * @Then it should be shipped via the :shippingMethodName shipping method |
||
248 | */ |
||
249 | public function itShouldBeShippedViaShippingMethod($shippingMethodName) |
||
256 | |||
257 | /** |
||
258 | * @Then it should be paid with :paymentMethodName |
||
259 | */ |
||
260 | public function itShouldBePaidWith($paymentMethodName) |
||
267 | |||
268 | /** |
||
269 | * @Then /^it should have (\d+) items$/ |
||
270 | * @Then I should see :amount orders in the list |
||
271 | * @Then I should see a single order in the list |
||
272 | */ |
||
273 | public function itShouldHaveAmountOfItems($amount = 1) |
||
283 | |||
284 | /** |
||
285 | * @Then the product named :productName should be in the items list |
||
286 | */ |
||
287 | public function theProductShouldBeInTheItemsList($productName) |
||
294 | |||
295 | /** |
||
296 | * @Then the order's items total should be :itemsTotal |
||
297 | */ |
||
298 | public function theOrdersItemsTotalShouldBe($itemsTotal) |
||
308 | |||
309 | /** |
||
310 | * @Then /^the order's total should(?:| still) be "([^"]+)"$/ |
||
311 | */ |
||
312 | public function theOrdersTotalShouldBe($total) |
||
322 | |||
323 | /** |
||
324 | * @Then there should be a shipping charge :shippingCharge |
||
325 | */ |
||
326 | public function theOrdersShippingChargesShouldBe($shippingCharge) |
||
333 | |||
334 | /** |
||
335 | * @Then the order's shipping total should be :shippingTotal |
||
336 | */ |
||
337 | public function theOrdersShippingTotalShouldBe($shippingTotal) |
||
347 | |||
348 | /** |
||
349 | * @Then the order's payment should (also) be :paymentAmount |
||
350 | */ |
||
351 | public function theOrdersPaymentShouldBe($paymentAmount) |
||
357 | |||
358 | /** |
||
359 | * @Then the order should have tax :tax |
||
360 | */ |
||
361 | public function theOrderShouldHaveTax($tax) |
||
368 | |||
369 | /** |
||
370 | * @Then /^the order's tax total should(?:| still) be "([^"]+)"$/ |
||
371 | */ |
||
372 | public function theOrdersTaxTotalShouldBe($taxTotal) |
||
382 | |||
383 | /** |
||
384 | * @Then the order's promotion discount should be :promotionDiscount |
||
385 | */ |
||
386 | public function theOrdersPromotionDiscountShouldBe($promotionDiscount) |
||
393 | |||
394 | /** |
||
395 | * @Then /^the order's promotion total should(?:| still) be "([^"]+)"$/ |
||
396 | */ |
||
397 | public function theOrdersPromotionTotalShouldBe($promotionTotal) |
||
407 | |||
408 | /** |
||
409 | * @When I check :itemName data |
||
410 | */ |
||
411 | public function iCheckData($itemName) |
||
415 | |||
416 | /** |
||
417 | * @Then /^(its) code should be "([^"]+)"$/ |
||
418 | */ |
||
419 | public function itemCodeShouldBe($itemName, $code) |
||
429 | |||
430 | /** |
||
431 | * @Then /^(its) unit price should be ([^"]+)$/ |
||
432 | */ |
||
433 | public function itemUnitPriceShouldBe($itemName, $unitPrice) |
||
443 | |||
444 | /** |
||
445 | * @Then /^(its) discounted unit price should be ([^"]+)$/ |
||
446 | */ |
||
447 | public function itemDiscountedUnitPriceShouldBe($itemName, $discountedUnitPrice) |
||
457 | |||
458 | /** |
||
459 | * @Then /^(its) quantity should be ([^"]+)$/ |
||
460 | */ |
||
461 | public function itemQuantityShouldBe($itemName, $quantity) |
||
471 | |||
472 | /** |
||
473 | * @Then /^(its) subtotal should be ([^"]+)$/ |
||
474 | */ |
||
475 | public function itemSubtotalShouldBe($itemName, $subtotal) |
||
485 | |||
486 | /** |
||
487 | * @Then /^(its) discount should be ([^"]+)$/ |
||
488 | * @Then the :itemName should have :discount discount |
||
489 | */ |
||
490 | public function theItemShouldHaveDiscount($itemName, $discount) |
||
500 | |||
501 | /** |
||
502 | * @Then /^(its) tax should be ([^"]+)$/ |
||
503 | */ |
||
504 | public function itemTaxShouldBe($itemName, $tax) |
||
514 | |||
515 | /** |
||
516 | * @Then /^(its) total should be ([^"]+)$/ |
||
517 | */ |
||
518 | public function itemTotalShouldBe($itemName, $total) |
||
528 | |||
529 | /** |
||
530 | * @Then I should be notified that the order's payment has been successfully completed |
||
531 | */ |
||
532 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyCompleted() |
||
538 | |||
539 | /** |
||
540 | * @Then I should be notified that the order's payment has been successfully refunded |
||
541 | */ |
||
542 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyRefunded() |
||
548 | |||
549 | /** |
||
550 | * @Then it should have payment state :paymentState |
||
551 | * @Then it should have payment with state :paymentState |
||
552 | */ |
||
553 | public function itShouldHavePaymentState($paymentState) |
||
560 | |||
561 | /** |
||
562 | * @Then it's payment state should be refunded |
||
563 | */ |
||
564 | public function orderPaymentStateShouldBeRefunded() |
||
572 | |||
573 | /** |
||
574 | * @Then /^I should not be able to mark (this order) as paid again$/ |
||
575 | */ |
||
576 | public function iShouldNotBeAbleToFinalizeItsPayment(OrderInterface $order) |
||
583 | |||
584 | /** |
||
585 | * @Then I should be notified that the order has been successfully shipped |
||
586 | */ |
||
587 | public function iShouldBeNotifiedThatTheOrderHasBeenSuccessfullyShipped() |
||
594 | |||
595 | /** |
||
596 | * @Then /^I should not be able to ship (this order)$/ |
||
597 | */ |
||
598 | public function iShouldNotBeAbleToShipThisOrder(OrderInterface $order) |
||
605 | |||
606 | /** |
||
607 | * @When I cancel this order |
||
608 | */ |
||
609 | public function iCancelThisOrder() |
||
613 | |||
614 | /** |
||
615 | * @Then I should be notified that it has been successfully updated |
||
616 | */ |
||
617 | public function iShouldBeNotifiedAboutItHasBeenSuccessfullyCanceled() |
||
624 | |||
625 | /** |
||
626 | * @Then I should not be able to cancel this order |
||
627 | */ |
||
628 | public function iShouldNotBeAbleToCancelThisOrder() |
||
635 | |||
636 | /** |
||
637 | * @Then this order should have state :state |
||
638 | * @Then its state should be :state |
||
639 | */ |
||
640 | public function itsStateShouldBe($state) |
||
648 | |||
649 | /** |
||
650 | * @Then it should( still) have a :state state |
||
651 | */ |
||
652 | public function itShouldHaveState($state) |
||
659 | |||
660 | /** |
||
661 | * @Then /^(the administrator) should know about (this additional note) for (this order made by "[^"]+")$/ |
||
662 | */ |
||
663 | public function theCustomerServiceShouldKnowAboutThisAdditionalNotes( |
||
676 | |||
677 | /** |
||
678 | * @Then I should see an order with :orderNumber number |
||
679 | */ |
||
680 | public function iShouldSeeOrderWithNumber($orderNumber) |
||
687 | |||
688 | /** |
||
689 | * @Then I should not see an order with :orderNumber number |
||
690 | */ |
||
691 | public function iShouldNotSeeOrderWithNumber($orderNumber) |
||
698 | |||
699 | /** |
||
700 | * @Then the first order should have number :number |
||
701 | */ |
||
702 | public function theFirstOrderShouldHaveNumber($number) |
||
712 | |||
713 | /** |
||
714 | * @Then it should have shipment in state :shipmentState |
||
715 | */ |
||
716 | public function itShouldHaveShipmentState($shipmentState) |
||
723 | |||
724 | /** |
||
725 | * @Then order :orderNumber should have shipment state :shippingState |
||
726 | */ |
||
727 | public function thisOrderShipmentStateShouldBe($shippingState) |
||
734 | |||
735 | /** |
||
736 | * @Then the order :order should have order payment state :orderPaymentState |
||
737 | * @Then /^(this order) should have order payment state "([^"]+)"$/ |
||
738 | * @Then /^(its) payment state should be "([^"]+)"$/ |
||
739 | */ |
||
740 | public function theOrderShouldHavePaymentState(OrderInterface $order, $orderPaymentState) |
||
747 | |||
748 | /** |
||
749 | * @Then the order :order should have order shipping state :orderShipmentState |
||
750 | * @Then /^(this order) should have order shipping state "([^"]+)"$/ |
||
751 | * @Then /^(its) shipping state should be "([^"]+)"$/ |
||
752 | */ |
||
753 | public function theOrderShouldHaveShipmentState(OrderInterface $order, $orderShipmentState) |
||
760 | |||
761 | /** |
||
762 | * @Then /^there should be(?:| only) (\d+) payments?$/ |
||
763 | */ |
||
764 | public function theOrderShouldHaveNumberOfPayments($number) |
||
770 | |||
771 | /** |
||
772 | * @Then I should see the order :orderNumber with total :total |
||
773 | */ |
||
774 | public function iShouldSeeTheOrderWithTotal($orderNumber, $total) |
||
781 | |||
782 | /** |
||
783 | * @When /^I want to modify a customer's (?:billing|shipping) address of (this order)$/ |
||
784 | */ |
||
785 | public function iWantToModifyACustomerSShippingAddress(OrderInterface $order) |
||
789 | |||
790 | /** |
||
791 | * @When I save my changes |
||
792 | * @When I try to save my changes |
||
793 | */ |
||
794 | public function iSaveMyChanges() |
||
798 | |||
799 | /** |
||
800 | * @When /^I specify their (?:|new )shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
801 | */ |
||
802 | public function iSpecifyTheirShippingAddressAsFor(AddressInterface $address) |
||
806 | |||
807 | /** |
||
808 | * @When /^I specify their (?:|new )billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
809 | */ |
||
810 | public function iSpecifyTheirBillingAddressAsFor(AddressInterface $address) |
||
814 | |||
815 | /** |
||
816 | * @Then /^I should be notified that the "([^"]+)", the "([^"]+)", the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
817 | */ |
||
818 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $thirdElement, $fourthElement, $type) |
||
825 | |||
826 | /** |
||
827 | * @Then I should see :provinceName as province in the shipping address |
||
828 | */ |
||
829 | public function iShouldSeeAsProvinceInTheShippingAddress($provinceName) |
||
836 | |||
837 | /** |
||
838 | * @Then I should see :provinceName ad province in the billing address |
||
839 | */ |
||
840 | public function iShouldSeeAdProvinceInTheBillingAddress($provinceName) |
||
847 | |||
848 | /** |
||
849 | * @Then /^(the administrator) should know about IP address of (this order made by "[^"]+")$/ |
||
850 | */ |
||
851 | public function theAdministratorShouldKnowAboutIPAddressOfThisOrderMadeBy( |
||
868 | |||
869 | /** |
||
870 | * @When /^I (clear old billing address) information$/ |
||
871 | */ |
||
872 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
876 | |||
877 | /** |
||
878 | * @When /^I (clear old shipping address) information$/ |
||
879 | */ |
||
880 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
884 | |||
885 | /** |
||
886 | * @When /^I do not specify new information$/ |
||
887 | */ |
||
888 | public function iDoNotSpecifyNewInformation() |
||
892 | |||
893 | /** |
||
894 | * @Then /^(the administrator) should see that (order placed by "[^"]+") has "([^"]+)" currency$/ |
||
895 | */ |
||
896 | public function theAdministratorShouldSeeThatThisOrderHasBeenPlacedIn(AdminUserInterface $user, OrderInterface $order, $currency) |
||
907 | |||
908 | /** |
||
909 | * @Then /^(the administrator) should see the order with total "([^"]+)" in order list$/ |
||
910 | */ |
||
911 | public function theAdministratorShouldSeeTheOrderWithTotalInOrderList(AdminUserInterface $user, $total) |
||
922 | |||
923 | /** |
||
924 | * @param string $type |
||
925 | * @param string $element |
||
926 | * @param string $expectedMessage |
||
927 | * |
||
928 | * @throws \InvalidArgumentException |
||
929 | */ |
||
930 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
938 | } |
||
939 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.