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 specify its tracking code as :trackingCode |
||
| 117 | */ |
||
| 118 | public function specifyItsTrackingCodeAs($trackingCode) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @When /^I ship (this order)$/ |
||
| 126 | */ |
||
| 127 | public function iShipThisOrder(OrderInterface $order) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @When I switch the way orders are sorted by :fieldName |
||
| 134 | */ |
||
| 135 | public function iSwitchSortingBy($fieldName) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @Then I should see a single order from customer :customer |
||
| 142 | */ |
||
| 143 | public function iShouldSeeASingleOrderFromCustomer(CustomerInterface $customer) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @Then it should have been placed by the customer :customerEmail |
||
| 153 | */ |
||
| 154 | public function itShouldBePlacedByCustomer($customerEmail) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @Then it should be shipped to :customerName, :street, :postcode, :city, :countryName |
||
| 164 | * @Then this order should be shipped to :customerName, :street, :postcode, :city, :countryName |
||
| 165 | * @Then /^(this order) should still be shipped to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/ |
||
| 166 | */ |
||
| 167 | public function itShouldBeShippedTo(OrderInterface $order = null, $customerName, $street, $postcode, $city, $countryName) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @Then it should be billed to :customerName, :street, :postcode, :city, :countryName |
||
| 181 | */ |
||
| 182 | public function itShouldBeBilledTo($customerName, $street, $postcode, $city, $countryName) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @Then it should be shipped via the :shippingMethodName shipping method |
||
| 192 | */ |
||
| 193 | public function itShouldBeShippedViaShippingMethod($shippingMethodName) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @Then it should be paid with :paymentMethodName |
||
| 203 | */ |
||
| 204 | public function itShouldBePaidWith($paymentMethodName) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @Then /^it should have (\d+) items$/ |
||
| 214 | */ |
||
| 215 | public function itShouldHaveAmountOfItems($amount) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @Then the product named :productName should be in the items list |
||
| 228 | */ |
||
| 229 | public function theProductShouldBeInTheItemsList($productName) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @Then the order's items total should be :itemsTotal |
||
| 239 | */ |
||
| 240 | public function theOrdersItemsTotalShouldBe($itemsTotal) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @Then /^the order's total should(?:| still) be "([^"]+)"$/ |
||
| 253 | */ |
||
| 254 | public function theOrdersTotalShouldBe($total) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @Then there should be a shipping charge :shippingCharge |
||
| 267 | */ |
||
| 268 | public function theOrdersShippingChargesShouldBe($shippingCharge) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @Then the order's shipping total should be :shippingTotal |
||
| 278 | */ |
||
| 279 | public function theOrdersShippingTotalShouldBe($shippingTotal) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @Then the order's payment should (also) be :paymentAmount |
||
| 292 | */ |
||
| 293 | public function theOrdersPaymentShouldBe($paymentAmount) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @Then the order should have tax :tax |
||
| 302 | */ |
||
| 303 | public function theOrderShouldHaveTax($tax) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @Then /^the order's tax total should(?:| still) be "([^"]+)"$/ |
||
| 313 | */ |
||
| 314 | public function theOrdersTaxTotalShouldBe($taxTotal) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @Then the order's promotion discount should be :promotionDiscount |
||
| 327 | */ |
||
| 328 | public function theOrdersPromotionDiscountShouldBe($promotionDiscount) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @Then /^the order's promotion total should(?:| still) be "([^"]+)"$/ |
||
| 338 | */ |
||
| 339 | public function theOrdersPromotionTotalShouldBe($promotionTotal) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @When I check :itemName data |
||
| 352 | */ |
||
| 353 | public function iCheckData($itemName) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @Then /^(its) code should be "([^"]+)"$/ |
||
| 360 | */ |
||
| 361 | public function itemCodeShouldBe($itemName, $code) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @Then /^(its) unit price should be ([^"]+)$/ |
||
| 374 | */ |
||
| 375 | public function itemUnitPriceShouldBe($itemName, $unitPrice) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @Then /^(its) discounted unit price should be ([^"]+)$/ |
||
| 388 | */ |
||
| 389 | public function itemDiscountedUnitPriceShouldBe($itemName, $discountedUnitPrice) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @Then /^(its) quantity should be ([^"]+)$/ |
||
| 402 | */ |
||
| 403 | public function itemQuantityShouldBe($itemName, $quantity) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @Then /^(its) subtotal should be ([^"]+)$/ |
||
| 416 | */ |
||
| 417 | public function itemSubtotalShouldBe($itemName, $subtotal) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @Then /^(its) discount should be ([^"]+)$/ |
||
| 430 | * @Then the :itemName should have :discount discount |
||
| 431 | */ |
||
| 432 | public function theItemShouldHaveDiscount($itemName, $discount) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @Then /^(its) tax should be ([^"]+)$/ |
||
| 445 | */ |
||
| 446 | public function itemTaxShouldBe($itemName, $tax) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @Then /^(its) total should be ([^"]+)$/ |
||
| 459 | */ |
||
| 460 | public function itemTotalShouldBe($itemName, $total) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @Then I should be notified that the order's payment has been successfully completed |
||
| 473 | */ |
||
| 474 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyCompleted() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @Then it should have payment state :paymentState |
||
| 481 | */ |
||
| 482 | public function itShouldHavePaymentState($paymentState) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @Then /^I should not be able to mark (this order) as paid again$/ |
||
| 492 | */ |
||
| 493 | public function iShouldNotBeAbleToFinalizeItsPayment(OrderInterface $order) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @Then I should be notified that the order has been successfully shipped |
||
| 503 | */ |
||
| 504 | public function iShouldBeNotifiedThatTheOrderHasBeenSuccessfullyShipped() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @Then /^I should not be able to ship (this order)$/ |
||
| 511 | */ |
||
| 512 | public function iShouldNotBeAbleToShipThisOrder(OrderInterface $order) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @When I cancel this order |
||
| 522 | */ |
||
| 523 | public function iCancelThisOrder() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @Then I should be notified that it has been successfully updated |
||
| 530 | */ |
||
| 531 | public function iShouldBeNotifiedAboutItHasBeenSuccessfullyCanceled() |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @Then I should not be able to cancel this order |
||
| 541 | */ |
||
| 542 | public function iShouldNotBeAbleToCancelThisOrder() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @Then this order should have state :state |
||
| 552 | * @Then its state should be :state |
||
| 553 | */ |
||
| 554 | public function itsStateShouldBe($state) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @Then it should( still) have a :state state |
||
| 565 | */ |
||
| 566 | public function itShouldHaveState($state) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @Then /^(the administrator) should know about (this additional note) for (this order made by "[^"]+")$/ |
||
| 576 | */ |
||
| 577 | public function theCustomerServiceShouldKnowAboutThisAdditionalNotes(AdminUserInterface $user, $note, OrderInterface $order) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @Then I should see an order with :orderNumber number |
||
| 587 | */ |
||
| 588 | public function iShouldSeeOrderWithNumber($orderNumber) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @Then the first order should have number :number |
||
| 598 | */ |
||
| 599 | public function theFirstOrderShouldHaveNumber($number) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @Then it should have shipment in state :shipmentState |
||
| 612 | */ |
||
| 613 | public function itShouldHaveShipmentState($shipmentState) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @Then order :orderNumber should have shipment state :shippingState |
||
| 623 | */ |
||
| 624 | public function thisOrderShipmentStateShouldBe($shippingState) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @Then the order :order should have order payment state :orderPaymentState |
||
| 634 | * @Then /^(this order) should have order payment state "([^"]+)"$/ |
||
| 635 | * @Then /^(its) payment state should be "([^"]+)"$/ |
||
| 636 | */ |
||
| 637 | public function theOrderShouldHavePaymentState(OrderInterface $order, $orderPaymentState) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @Then the order :order should have order shipping state :orderShipmentState |
||
| 647 | * @Then /^(this order) should have order shipping state "([^"]+)"$/ |
||
| 648 | * @Then /^(its) shipping state should be "([^"]+)"$/ |
||
| 649 | */ |
||
| 650 | public function theOrderShouldHaveShipmentState(OrderInterface $order, $orderShipmentState) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @Then /^there should be(?:| only) (\d+) payments?$/ |
||
| 660 | */ |
||
| 661 | public function theOrderShouldHaveNumberOfPayments($number) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * @Then I should see the order :orderNumber with total :total |
||
| 670 | */ |
||
| 671 | public function iShouldSeeTheOrderWithTotal($orderNumber, $total) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * @When /^I want to modify a customer's shipping address of (this order)$/ |
||
| 681 | */ |
||
| 682 | public function iWantToModifyACustomerSShippingAddress(OrderInterface $order) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @When I specify the first name as :firstName |
||
| 689 | * @When I do not specify the first name |
||
| 690 | */ |
||
| 691 | public function iSpecifyTheFirstNameAs($firstName = null) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @When I specify the last name as :lastName |
||
| 698 | * @When I do not specify the last name |
||
| 699 | */ |
||
| 700 | public function iSpecifyTheLastNameAs($lastName = null) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @When I specify the street as :street |
||
| 707 | * @When I do not specify the street |
||
| 708 | */ |
||
| 709 | public function iSpecifyTheStreetAs($street = null) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @When I specify the city as :city |
||
| 716 | * @When I do not specify the city |
||
| 717 | */ |
||
| 718 | public function iSpecifyTheCityAs($city = null) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * @When I specify the postcode as :postcode |
||
| 725 | */ |
||
| 726 | public function iSpecifyThePostcodeAs($postcode) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @When I choose :country as the country |
||
| 733 | */ |
||
| 734 | public function iChooseCountryAs($country) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * @When I save my changes |
||
| 741 | * @When I try to save my changes |
||
| 742 | */ |
||
| 743 | public function iSaveMyChanges() |
||
| 747 | |||
| 748 | /** |
||
| 749 | * @When I specify their shipping address as :city, :street, :postcode, :country for :firstAndLastName |
||
| 750 | */ |
||
| 751 | public function iSpecifyTheirShippingAddressAsFor($city, $street, $postcode, $country, $firstAndLastName) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * @Then /^I should be notified that the (last name|first name|city|street) is required$/ |
||
| 758 | */ |
||
| 759 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * @Then I should see :provinceName as province in the shipping address |
||
| 769 | */ |
||
| 770 | public function iShouldSeeAsProvinceInTheShippingAddress($provinceName) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @Then I should see :provinceName ad province in the billing address |
||
| 780 | */ |
||
| 781 | public function iShouldSeeAdProvinceInTheBillingAddress($provinceName) |
||
| 788 | } |
||
| 789 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.