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 choose :currencyName as the filter currency |
||
| 174 | */ |
||
| 175 | public function iChooseCurrencyAsTheFilterCurrency($currencyName) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @When I specify filter total being greater than :total |
||
| 182 | */ |
||
| 183 | public function iSpecifyFilterTotalBeingGreaterThan($total) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @When I specify filter total being less than :total |
||
| 190 | */ |
||
| 191 | public function iSpecifyFilterTotalBeingLessThan($total) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @When I filter |
||
| 198 | */ |
||
| 199 | public function iFilter() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @Then I should see a single order from customer :customer |
||
| 206 | */ |
||
| 207 | public function iShouldSeeASingleOrderFromCustomer(CustomerInterface $customer) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @Then it should have been placed by the customer :customerEmail |
||
| 217 | */ |
||
| 218 | public function itShouldBePlacedByCustomer($customerEmail) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @Then it should be shipped to :customerName, :street, :postcode, :city, :countryName |
||
| 228 | * @Then /^(this order) should (?:|still )be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/ |
||
| 229 | */ |
||
| 230 | public function itShouldBeShippedTo( |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @Then it should be billed to :customerName, :street, :postcode, :city, :countryName |
||
| 250 | * @Then /^(this order) bill should (?:|still )be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/ |
||
| 251 | */ |
||
| 252 | public function itShouldBeBilledTo( |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @Then it should be shipped via the :shippingMethodName shipping method |
||
| 272 | */ |
||
| 273 | public function itShouldBeShippedViaShippingMethod($shippingMethodName) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @Then it should be paid with :paymentMethodName |
||
| 283 | */ |
||
| 284 | public function itShouldBePaidWith($paymentMethodName) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @Then /^it should have (\d+) items$/ |
||
| 294 | * @Then I should see :amount orders in the list |
||
| 295 | * @Then I should see a single order in the list |
||
| 296 | */ |
||
| 297 | public function itShouldHaveAmountOfItems($amount = 1) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @Then the product named :productName should be in the items list |
||
| 310 | */ |
||
| 311 | public function theProductShouldBeInTheItemsList($productName) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @Then the order's items total should be :itemsTotal |
||
| 321 | */ |
||
| 322 | public function theOrdersItemsTotalShouldBe($itemsTotal) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @Then /^the order's total should(?:| still) be "([^"]+)"$/ |
||
| 335 | */ |
||
| 336 | public function theOrdersTotalShouldBe($total) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @Then there should be a shipping charge :shippingCharge |
||
| 349 | */ |
||
| 350 | public function theOrdersShippingChargesShouldBe($shippingCharge) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @Then the order's shipping total should be :shippingTotal |
||
| 360 | */ |
||
| 361 | public function theOrdersShippingTotalShouldBe($shippingTotal) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @Then the order's payment should (also) be :paymentAmount |
||
| 374 | */ |
||
| 375 | public function theOrdersPaymentShouldBe($paymentAmount) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @Then the order should have tax :tax |
||
| 384 | */ |
||
| 385 | public function theOrderShouldHaveTax($tax) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @Then /^the order's tax total should(?:| still) be "([^"]+)"$/ |
||
| 395 | */ |
||
| 396 | public function theOrdersTaxTotalShouldBe($taxTotal) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @Then the order's promotion discount should be :promotionDiscount |
||
| 409 | */ |
||
| 410 | public function theOrdersPromotionDiscountShouldBe($promotionDiscount) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @Then /^the order's promotion total should(?:| still) be "([^"]+)"$/ |
||
| 420 | */ |
||
| 421 | public function theOrdersPromotionTotalShouldBe($promotionTotal) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @When I check :itemName data |
||
| 434 | */ |
||
| 435 | public function iCheckData($itemName) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @Then /^(its) code should be "([^"]+)"$/ |
||
| 442 | */ |
||
| 443 | public function itemCodeShouldBe($itemName, $code) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @Then /^(its) unit price should be ([^"]+)$/ |
||
| 456 | */ |
||
| 457 | public function itemUnitPriceShouldBe($itemName, $unitPrice) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @Then /^(its) discounted unit price should be ([^"]+)$/ |
||
| 470 | */ |
||
| 471 | public function itemDiscountedUnitPriceShouldBe($itemName, $discountedUnitPrice) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @Then /^(its) quantity should be ([^"]+)$/ |
||
| 484 | */ |
||
| 485 | public function itemQuantityShouldBe($itemName, $quantity) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @Then /^(its) subtotal should be ([^"]+)$/ |
||
| 498 | */ |
||
| 499 | public function itemSubtotalShouldBe($itemName, $subtotal) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @Then /^(its) discount should be ([^"]+)$/ |
||
| 512 | * @Then the :itemName should have :discount discount |
||
| 513 | */ |
||
| 514 | public function theItemShouldHaveDiscount($itemName, $discount) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @Then /^(its) tax should be ([^"]+)$/ |
||
| 527 | */ |
||
| 528 | public function itemTaxShouldBe($itemName, $tax) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @Then /^(its) total should be ([^"]+)$/ |
||
| 541 | */ |
||
| 542 | public function itemTotalShouldBe($itemName, $total) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @Then I should be notified that the order's payment has been successfully completed |
||
| 555 | */ |
||
| 556 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyCompleted() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @Then I should be notified that the order's payment has been successfully refunded |
||
| 565 | */ |
||
| 566 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyRefunded() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @Then it should have payment state :paymentState |
||
| 575 | * @Then it should have payment with state :paymentState |
||
| 576 | */ |
||
| 577 | public function itShouldHavePaymentState($paymentState) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @Then it's payment state should be refunded |
||
| 587 | */ |
||
| 588 | public function orderPaymentStateShouldBeRefunded() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @Then /^I should not be able to mark (this order) as paid again$/ |
||
| 599 | */ |
||
| 600 | public function iShouldNotBeAbleToFinalizeItsPayment(OrderInterface $order) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @Then I should be notified that the order has been successfully shipped |
||
| 610 | */ |
||
| 611 | public function iShouldBeNotifiedThatTheOrderHasBeenSuccessfullyShipped() |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @Then /^I should not be able to ship (this order)$/ |
||
| 621 | */ |
||
| 622 | public function iShouldNotBeAbleToShipThisOrder(OrderInterface $order) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @When I cancel this order |
||
| 632 | */ |
||
| 633 | public function iCancelThisOrder() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @Then I should be notified that it has been successfully updated |
||
| 640 | */ |
||
| 641 | public function iShouldBeNotifiedAboutItHasBeenSuccessfullyCanceled() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @Then I should not be able to cancel this order |
||
| 651 | */ |
||
| 652 | public function iShouldNotBeAbleToCancelThisOrder() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @Then this order should have state :state |
||
| 662 | * @Then its state should be :state |
||
| 663 | */ |
||
| 664 | public function itsStateShouldBe($state) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @Then it should( still) have a :state state |
||
| 675 | */ |
||
| 676 | public function itShouldHaveState($state) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @Then /^(the administrator) should know about (this additional note) for (this order made by "[^"]+")$/ |
||
| 686 | */ |
||
| 687 | public function theCustomerServiceShouldKnowAboutThisAdditionalNotes( |
||
| 700 | |||
| 701 | /** |
||
| 702 | * @Then I should see an order with :orderNumber number |
||
| 703 | */ |
||
| 704 | public function iShouldSeeOrderWithNumber($orderNumber) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * @Then I should not see an order with :orderNumber number |
||
| 714 | */ |
||
| 715 | public function iShouldNotSeeOrderWithNumber($orderNumber) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * @Then I should not see any orders with currency :currencyCode |
||
| 725 | */ |
||
| 726 | public function iShouldNotSeeAnyOrderWithCurrency($currencyCode) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @Then the first order should have number :number |
||
| 736 | */ |
||
| 737 | public function theFirstOrderShouldHaveNumber($number) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * @Then it should have shipment in state :shipmentState |
||
| 750 | */ |
||
| 751 | public function itShouldHaveShipmentState($shipmentState) |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @Then order :orderNumber should have shipment state :shippingState |
||
| 761 | */ |
||
| 762 | public function thisOrderShipmentStateShouldBe($shippingState) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * @Then the order :order should have order payment state :orderPaymentState |
||
| 772 | * @Then /^(this order) should have order payment state "([^"]+)"$/ |
||
| 773 | * @Then /^(its) payment state should be "([^"]+)"$/ |
||
| 774 | */ |
||
| 775 | public function theOrderShouldHavePaymentState(OrderInterface $order, $orderPaymentState) |
||
| 782 | |||
| 783 | /** |
||
| 784 | * @Then the order :order should have order shipping state :orderShipmentState |
||
| 785 | * @Then /^(this order) should have order shipping state "([^"]+)"$/ |
||
| 786 | * @Then /^(its) shipping state should be "([^"]+)"$/ |
||
| 787 | */ |
||
| 788 | public function theOrderShouldHaveShipmentState(OrderInterface $order, $orderShipmentState) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * @Then /^there should be(?:| only) (\d+) payments?$/ |
||
| 798 | */ |
||
| 799 | public function theOrderShouldHaveNumberOfPayments($number) |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @Then I should see the order :orderNumber with total :total |
||
| 808 | */ |
||
| 809 | public function iShouldSeeTheOrderWithTotal($orderNumber, $total) |
||
| 816 | |||
| 817 | /** |
||
| 818 | * @When /^I want to modify a customer's (?:billing|shipping) address of (this order)$/ |
||
| 819 | */ |
||
| 820 | public function iWantToModifyACustomerSShippingAddress(OrderInterface $order) |
||
| 824 | |||
| 825 | /** |
||
| 826 | * @When I save my changes |
||
| 827 | * @When I try to save my changes |
||
| 828 | */ |
||
| 829 | public function iSaveMyChanges() |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @When /^I specify their (?:|new )shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 836 | */ |
||
| 837 | public function iSpecifyTheirShippingAddressAsFor(AddressInterface $address) |
||
| 841 | |||
| 842 | /** |
||
| 843 | * @When /^I specify their (?:|new )billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 844 | */ |
||
| 845 | public function iSpecifyTheirBillingAddressAsFor(AddressInterface $address) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * @Then /^I should be notified that the "([^"]+)", the "([^"]+)", the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
| 852 | */ |
||
| 853 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $thirdElement, $fourthElement, $type) |
||
| 860 | |||
| 861 | /** |
||
| 862 | * @Then I should see :provinceName as province in the shipping address |
||
| 863 | */ |
||
| 864 | public function iShouldSeeAsProvinceInTheShippingAddress($provinceName) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * @Then I should see :provinceName ad province in the billing address |
||
| 874 | */ |
||
| 875 | public function iShouldSeeAdProvinceInTheBillingAddress($provinceName) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * @Then /^(the administrator) should know about IP address of (this order made by "[^"]+")$/ |
||
| 885 | */ |
||
| 886 | public function theAdministratorShouldKnowAboutIPAddressOfThisOrderMadeBy( |
||
| 903 | |||
| 904 | /** |
||
| 905 | * @When /^I (clear old billing address) information$/ |
||
| 906 | */ |
||
| 907 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
| 911 | |||
| 912 | /** |
||
| 913 | * @When /^I (clear old shipping address) information$/ |
||
| 914 | */ |
||
| 915 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
| 919 | |||
| 920 | /** |
||
| 921 | * @When /^I do not specify new information$/ |
||
| 922 | */ |
||
| 923 | public function iDoNotSpecifyNewInformation() |
||
| 927 | |||
| 928 | /** |
||
| 929 | * @Then /^(the administrator) should see that (order placed by "[^"]+") has "([^"]+)" currency$/ |
||
| 930 | */ |
||
| 931 | public function theAdministratorShouldSeeThatThisOrderHasBeenPlacedIn(AdminUserInterface $user, OrderInterface $order, $currency) |
||
| 942 | |||
| 943 | /** |
||
| 944 | * @Then /^(the administrator) should see the order with total "([^"]+)" in order list$/ |
||
| 945 | */ |
||
| 946 | public function theAdministratorShouldSeeTheOrderWithTotalInOrderList(AdminUserInterface $user, $total) |
||
| 957 | |||
| 958 | /** |
||
| 959 | * @param string $type |
||
| 960 | * @param string $element |
||
| 961 | * @param string $expectedMessage |
||
| 962 | * |
||
| 963 | * @throws \InvalidArgumentException |
||
| 964 | */ |
||
| 965 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
| 973 | } |
||
| 974 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.