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 HistoryPageInterface | ||
| 57 | */ | ||
| 58 | private $historyPage; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * @var NotificationCheckerInterface | ||
| 62 | */ | ||
| 63 | private $notificationChecker; | ||
| 64 | |||
| 65 | /** | ||
| 66 | * @var SharedSecurityServiceInterface | ||
| 67 | */ | ||
| 68 | private $sharedSecurityService; | ||
| 69 | |||
| 70 | /** | ||
| 71 | * @param SharedStorageInterface $sharedStorage | ||
| 72 | * @param IndexPageInterface $indexPage | ||
| 73 | * @param ShowPageInterface $showPage | ||
| 74 | * @param UpdatePageInterface $updatePage | ||
| 75 | * @param HistoryPageInterface $historyPage | ||
| 76 | * @param NotificationCheckerInterface $notificationChecker | ||
| 77 | * @param SharedSecurityServiceInterface $sharedSecurityService | ||
| 78 | */ | ||
| 79 | public function __construct( | ||
| 96 | |||
| 97 | /** | ||
| 98 | * @Given I am browsing orders | ||
| 99 | * @When I browse orders | ||
| 100 | */ | ||
| 101 | public function iBrowseOrders() | ||
| 105 | |||
| 106 | /** | ||
| 107 | * @When I browse order's :order history | ||
| 108 | */ | ||
| 109 | public function iBrowseOrderHistory(OrderInterface $order) | ||
| 113 | |||
| 114 | /** | ||
| 115 | * @Given /^I am viewing the summary of (this order)$/ | ||
| 116 | * @When I view the summary of the order :order | ||
| 117 | */ | ||
| 118 | public function iSeeTheOrder(OrderInterface $order) | ||
| 122 | |||
| 123 | /** | ||
| 124 | * @When /^I mark (this order) as paid$/ | ||
| 125 | */ | ||
| 126 | public function iMarkThisOrderAsAPaid(OrderInterface $order) | ||
| 130 | |||
| 131 | /** | ||
| 132 | * @When /^I mark (this order)'s payment as refunded$/ | ||
| 133 | */ | ||
| 134 | public function iMarkThisOrderSPaymentAsRefunded(OrderInterface $order) | ||
| 138 | |||
| 139 | /** | ||
| 140 | * @When specify its tracking code as :trackingCode | ||
| 141 | */ | ||
| 142 | public function specifyItsTrackingCodeAs($trackingCode) | ||
| 147 | |||
| 148 | /** | ||
| 149 | * @When /^I ship (this order)$/ | ||
| 150 | */ | ||
| 151 | public function iShipThisOrder(OrderInterface $order) | ||
| 155 | |||
| 156 | /** | ||
| 157 | * @When I switch the way orders are sorted by :fieldName | ||
| 158 | */ | ||
| 159 | public function iSwitchSortingBy($fieldName) | ||
| 163 | |||
| 164 | /** | ||
| 165 | * @When I specify filter date from as :dateTime | ||
| 166 | */ | ||
| 167 | public function iSpecifyFilterDateFromAs($dateTime) | ||
| 171 | |||
| 172 | /** | ||
| 173 | * @When I specify filter date to as :dateTime | ||
| 174 | */ | ||
| 175 | public function iSpecifyFilterDateToAs($dateTime) | ||
| 179 | |||
| 180 | /** | ||
| 181 | * @When I choose :channelName as a channel filter | ||
| 182 | */ | ||
| 183 | public function iChooseChannelAsAChannelFilter($channelName) | ||
| 187 | |||
| 188 | /** | ||
| 189 | * @When I choose :currencyName as the filter currency | ||
| 190 | */ | ||
| 191 | public function iChooseCurrencyAsTheFilterCurrency($currencyName) | ||
| 195 | |||
| 196 | /** | ||
| 197 | * @When I specify filter total being greater than :total | ||
| 198 | */ | ||
| 199 | public function iSpecifyFilterTotalBeingGreaterThan($total) | ||
| 203 | |||
| 204 | /** | ||
| 205 | * @When I specify filter total being less than :total | ||
| 206 | */ | ||
| 207 | public function iSpecifyFilterTotalBeingLessThan($total) | ||
| 211 | |||
| 212 | /** | ||
| 213 | * @When I filter | ||
| 214 | */ | ||
| 215 | public function iFilter() | ||
| 219 | |||
| 220 | /** | ||
| 221 | * @Then I should see a single order from customer :customer | ||
| 222 | */ | ||
| 223 | public function iShouldSeeASingleOrderFromCustomer(CustomerInterface $customer) | ||
| 230 | |||
| 231 | /** | ||
| 232 | * @Then it should have been placed by the customer :customerEmail | ||
| 233 | */ | ||
| 234 | public function itShouldBePlacedByCustomer($customerEmail) | ||
| 241 | |||
| 242 | /** | ||
| 243 | * @Then it should be shipped to :customerName, :street, :postcode, :city, :countryName | ||
| 244 | * @Then /^(this order) should (?:|still )be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/ | ||
| 245 | */ | ||
| 246 | public function itShouldBeShippedTo( | ||
| 263 | |||
| 264 | /** | ||
| 265 | * @Then it should be billed to :customerName, :street, :postcode, :city, :countryName | ||
| 266 | * @Then the order should be billed to :customerName, :street, :postcode, :city, :countryName | ||
| 267 | * @Then /^(this order) bill should (?:|still )be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/ | ||
| 268 | */ | ||
| 269 | public function itShouldBeBilledTo( | ||
| 286 | |||
| 287 | /** | ||
| 288 | * @Then it should be shipped via the :shippingMethodName shipping method | ||
| 289 | */ | ||
| 290 | public function itShouldBeShippedViaShippingMethod($shippingMethodName) | ||
| 297 | |||
| 298 | /** | ||
| 299 | * @Then it should be paid with :paymentMethodName | ||
| 300 | */ | ||
| 301 | public function itShouldBePaidWith($paymentMethodName) | ||
| 308 | |||
| 309 | /** | ||
| 310 | * @Then /^it should have (\d+) items$/ | ||
| 311 | * @Then I should see :amount orders in the list | ||
| 312 | * @Then I should see a single order in the list | ||
| 313 | */ | ||
| 314 | public function itShouldHaveAmountOfItems($amount = 1) | ||
| 324 | |||
| 325 | /** | ||
| 326 | * @Then the product named :productName should be in the items list | ||
| 327 | */ | ||
| 328 | public function theProductShouldBeInTheItemsList($productName) | ||
| 335 | |||
| 336 | /** | ||
| 337 | * @Then the order's items total should be :itemsTotal | ||
| 338 | */ | ||
| 339 | public function theOrdersItemsTotalShouldBe($itemsTotal) | ||
| 349 | |||
| 350 | /** | ||
| 351 | * @Then /^the order's total should(?:| still) be "([^"]+)"$/ | ||
| 352 | */ | ||
| 353 | public function theOrdersTotalShouldBe($total) | ||
| 363 | |||
| 364 | /** | ||
| 365 | * @Then there should be a shipping charge :shippingCharge | ||
| 366 | */ | ||
| 367 | public function theOrdersShippingChargesShouldBe($shippingCharge) | ||
| 374 | |||
| 375 | /** | ||
| 376 | * @Then the order's shipping total should be :shippingTotal | ||
| 377 | */ | ||
| 378 | public function theOrdersShippingTotalShouldBe($shippingTotal) | ||
| 388 | |||
| 389 | /** | ||
| 390 | * @Then the order's payment should (also) be :paymentAmount | ||
| 391 | */ | ||
| 392 | public function theOrdersPaymentShouldBe($paymentAmount) | ||
| 398 | |||
| 399 | /** | ||
| 400 | * @Then the order should have tax :tax | ||
| 401 | */ | ||
| 402 | public function theOrderShouldHaveTax($tax) | ||
| 409 | |||
| 410 | /** | ||
| 411 | * @Then /^the order's tax total should(?:| still) be "([^"]+)"$/ | ||
| 412 | */ | ||
| 413 | public function theOrdersTaxTotalShouldBe($taxTotal) | ||
| 423 | |||
| 424 | /** | ||
| 425 | * @Then the order's promotion discount should be :promotionDiscount | ||
| 426 | */ | ||
| 427 | public function theOrdersPromotionDiscountShouldBe($promotionDiscount) | ||
| 434 | |||
| 435 | /** | ||
| 436 | * @Then /^the order's promotion total should(?:| still) be "([^"]+)"$/ | ||
| 437 | */ | ||
| 438 | public function theOrdersPromotionTotalShouldBe($promotionTotal) | ||
| 448 | |||
| 449 | /** | ||
| 450 | * @When I check :itemName data | ||
| 451 | */ | ||
| 452 | public function iCheckData($itemName) | ||
| 456 | |||
| 457 | /** | ||
| 458 | * @Then /^(its) code should be "([^"]+)"$/ | ||
| 459 | */ | ||
| 460 | public function itemCodeShouldBe($itemName, $code) | ||
| 470 | |||
| 471 | /** | ||
| 472 | * @Then /^(its) unit price should be ([^"]+)$/ | ||
| 473 | */ | ||
| 474 | public function itemUnitPriceShouldBe($itemName, $unitPrice) | ||
| 484 | |||
| 485 | /** | ||
| 486 | * @Then /^(its) discounted unit price should be ([^"]+)$/ | ||
| 487 | */ | ||
| 488 | public function itemDiscountedUnitPriceShouldBe($itemName, $discountedUnitPrice) | ||
| 498 | |||
| 499 | /** | ||
| 500 | * @Then /^(its) quantity should be ([^"]+)$/ | ||
| 501 | */ | ||
| 502 | public function itemQuantityShouldBe($itemName, $quantity) | ||
| 512 | |||
| 513 | /** | ||
| 514 | * @Then /^(its) subtotal should be ([^"]+)$/ | ||
| 515 | */ | ||
| 516 | public function itemSubtotalShouldBe($itemName, $subtotal) | ||
| 526 | |||
| 527 | /** | ||
| 528 | * @Then /^(its) discount should be ([^"]+)$/ | ||
| 529 | */ | ||
| 530 | public function theItemShouldHaveDiscount($itemName, $discount) | ||
| 540 | |||
| 541 | /** | ||
| 542 | * @Then /^(its) tax should be ([^"]+)$/ | ||
| 543 | */ | ||
| 544 | public function itemTaxShouldBe($itemName, $tax) | ||
| 554 | |||
| 555 | /** | ||
| 556 | * @Then /^(its) total should be ([^"]+)$/ | ||
| 557 | */ | ||
| 558 | public function itemTotalShouldBe($itemName, $total) | ||
| 568 | |||
| 569 | /** | ||
| 570 | * @Then I should be notified that the order's payment has been successfully completed | ||
| 571 | */ | ||
| 572 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyCompleted() | ||
| 578 | |||
| 579 | /** | ||
| 580 | * @Then I should be notified that the order's payment has been successfully refunded | ||
| 581 | */ | ||
| 582 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyRefunded() | ||
| 588 | |||
| 589 | /** | ||
| 590 | * @Then it should have payment state :paymentState | ||
| 591 | * @Then it should have payment with state :paymentState | ||
| 592 | */ | ||
| 593 | public function itShouldHavePaymentState($paymentState) | ||
| 600 | |||
| 601 | /** | ||
| 602 | * @Then it's payment state should be refunded | ||
| 603 | */ | ||
| 604 | public function orderPaymentStateShouldBeRefunded() | ||
| 612 | |||
| 613 | /** | ||
| 614 | * @Then /^I should not be able to mark (this order) as paid again$/ | ||
| 615 | */ | ||
| 616 | public function iShouldNotBeAbleToFinalizeItsPayment(OrderInterface $order) | ||
| 623 | |||
| 624 | /** | ||
| 625 | * @Then I should be notified that the order has been successfully shipped | ||
| 626 | */ | ||
| 627 | public function iShouldBeNotifiedThatTheOrderHasBeenSuccessfullyShipped() | ||
| 634 | |||
| 635 | /** | ||
| 636 | * @Then /^I should not be able to ship (this order)$/ | ||
| 637 | */ | ||
| 638 | public function iShouldNotBeAbleToShipThisOrder(OrderInterface $order) | ||
| 645 | |||
| 646 | /** | ||
| 647 | * @When I cancel this order | ||
| 648 | */ | ||
| 649 | public function iCancelThisOrder() | ||
| 653 | |||
| 654 | /** | ||
| 655 | * @Then I should be notified that it has been successfully updated | ||
| 656 | */ | ||
| 657 | public function iShouldBeNotifiedAboutItHasBeenSuccessfullyCanceled() | ||
| 664 | |||
| 665 | /** | ||
| 666 | * @Then I should not be able to cancel this order | ||
| 667 | */ | ||
| 668 | public function iShouldNotBeAbleToCancelThisOrder() | ||
| 675 | |||
| 676 | /** | ||
| 677 | * @Then this order should have state :state | ||
| 678 | * @Then its state should be :state | ||
| 679 | */ | ||
| 680 | public function itsStateShouldBe($state) | ||
| 688 | |||
| 689 | /** | ||
| 690 | * @Then it should( still) have a :state state | ||
| 691 | */ | ||
| 692 | public function itShouldHaveState($state) | ||
| 699 | |||
| 700 | /** | ||
| 701 | * @Then /^(the administrator) should know about (this additional note) for (this order made by "[^"]+")$/ | ||
| 702 | */ | ||
| 703 | public function theCustomerServiceShouldKnowAboutThisAdditionalNotes( | ||
| 716 | |||
| 717 | /** | ||
| 718 | * @Then I should see an order with :orderNumber number | ||
| 719 | */ | ||
| 720 | public function iShouldSeeOrderWithNumber($orderNumber) | ||
| 727 | |||
| 728 | /** | ||
| 729 | * @Then I should not see an order with :orderNumber number | ||
| 730 | */ | ||
| 731 | public function iShouldNotSeeOrderWithNumber($orderNumber) | ||
| 738 | |||
| 739 | /** | ||
| 740 | * @Then I should not see any orders with currency :currencyCode | ||
| 741 | */ | ||
| 742 | public function iShouldNotSeeAnyOrderWithCurrency($currencyCode) | ||
| 749 | |||
| 750 | /** | ||
| 751 | * @Then the first order should have number :number | ||
| 752 | */ | ||
| 753 | public function theFirstOrderShouldHaveNumber($number) | ||
| 763 | |||
| 764 | /** | ||
| 765 | * @Then it should have shipment in state :shipmentState | ||
| 766 | */ | ||
| 767 | public function itShouldHaveShipmentState($shipmentState) | ||
| 774 | |||
| 775 | /** | ||
| 776 | * @Then order :orderNumber should have shipment state :shippingState | ||
| 777 | */ | ||
| 778 | public function thisOrderShipmentStateShouldBe($shippingState) | ||
| 785 | |||
| 786 | /** | ||
| 787 | * @Then the order :order should have order payment state :orderPaymentState | ||
| 788 | * @Then /^(this order) should have order payment state "([^"]+)"$/ | ||
| 789 | */ | ||
| 790 | public function theOrderShouldHavePaymentState(OrderInterface $order, $orderPaymentState) | ||
| 797 | |||
| 798 | /** | ||
| 799 | * @Then /^(this order) should have order shipping state "([^"]+)"$/ | ||
| 800 | */ | ||
| 801 | public function theOrderShouldHaveShipmentState(OrderInterface $order, $orderShipmentState) | ||
| 808 | |||
| 809 | /** | ||
| 810 | * @Then /^there should be(?:| only) (\d+) payments?$/ | ||
| 811 | */ | ||
| 812 | public function theOrderShouldHaveNumberOfPayments($number) | ||
| 818 | |||
| 819 | /** | ||
| 820 | * @Then I should see the order :orderNumber with total :total | ||
| 821 | */ | ||
| 822 | public function iShouldSeeTheOrderWithTotal($orderNumber, $total) | ||
| 829 | |||
| 830 | /** | ||
| 831 | * @When /^I want to modify a customer's (?:billing|shipping) address of (this order)$/ | ||
| 832 | */ | ||
| 833 | public function iWantToModifyACustomerSShippingAddress(OrderInterface $order) | ||
| 837 | |||
| 838 | /** | ||
| 839 | * @When I save my changes | ||
| 840 | * @When I try to save my changes | ||
| 841 | */ | ||
| 842 | public function iSaveMyChanges() | ||
| 846 | |||
| 847 | /** | ||
| 848 | * @When /^I specify their (?:|new )shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ | ||
| 849 | */ | ||
| 850 | public function iSpecifyTheirShippingAddressAsFor(AddressInterface $address) | ||
| 854 | |||
| 855 | /** | ||
| 856 | * @When /^I specify their (?:|new )billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ | ||
| 857 | */ | ||
| 858 | public function iSpecifyTheirBillingAddressAsFor(AddressInterface $address) | ||
| 862 | |||
| 863 | /** | ||
| 864 | * @Then /^I should be notified that the "([^"]+)", the "([^"]+)", the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ | ||
| 865 | */ | ||
| 866 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $thirdElement, $fourthElement, $type) | ||
| 873 | |||
| 874 | /** | ||
| 875 | * @Then I should see :provinceName as province in the shipping address | ||
| 876 | */ | ||
| 877 | public function iShouldSeeAsProvinceInTheShippingAddress($provinceName) | ||
| 884 | |||
| 885 | /** | ||
| 886 | * @Then I should see :provinceName ad province in the billing address | ||
| 887 | */ | ||
| 888 | public function iShouldSeeAdProvinceInTheBillingAddress($provinceName) | ||
| 895 | |||
| 896 | /** | ||
| 897 | * @Then /^(the administrator) should know about IP address of (this order made by "[^"]+")$/ | ||
| 898 | */ | ||
| 899 | public function theAdministratorShouldKnowAboutIPAddressOfThisOrderMadeBy( | ||
| 916 | |||
| 917 | /** | ||
| 918 | * @When /^I (clear old billing address) information$/ | ||
| 919 | */ | ||
| 920 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) | ||
| 924 | |||
| 925 | /** | ||
| 926 | * @When /^I (clear old shipping address) information$/ | ||
| 927 | */ | ||
| 928 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) | ||
| 932 | |||
| 933 | /** | ||
| 934 | * @When /^I do not specify new information$/ | ||
| 935 | */ | ||
| 936 | public function iDoNotSpecifyNewInformation() | ||
| 940 | |||
| 941 | /** | ||
| 942 | * @Then /^(the administrator) should see that (order placed by "[^"]+") has "([^"]+)" currency$/ | ||
| 943 | */ | ||
| 944 | public function theAdministratorShouldSeeThatThisOrderHasBeenPlacedIn(AdminUserInterface $user, OrderInterface $order, $currency) | ||
| 955 | |||
| 956 | /** | ||
| 957 | * @Then /^(the administrator) should see the order with total "([^"]+)" in order list$/ | ||
| 958 | */ | ||
| 959 | public function theAdministratorShouldSeeTheOrderWithTotalInOrderList(AdminUserInterface $user, $total) | ||
| 970 | |||
| 971 | /** | ||
| 972 | * @Then there should be :count changes in the registry | ||
| 973 | */ | ||
| 974 | public function thereShouldBeCountChangesInTheRegistry($count) | ||
| 981 | |||
| 982 | /** | ||
| 983 | * @param string $type | ||
| 984 | * @param string $element | ||
| 985 | * @param string $expectedMessage | ||
| 986 | * | ||
| 987 | * @throws \InvalidArgumentException | ||
| 988 | */ | ||
| 989 | private function assertElementValidationMessage($type, $element, $expectedMessage) | ||
| 997 | } | ||
| 998 | 
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.