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) |
||
119 | { |
||
120 | $this->showPage->open(['id' => $order->getId()]); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @When /^I mark (this order) as paid$/ |
||
125 | */ |
||
126 | public function iMarkThisOrderAsAPaid(OrderInterface $order) |
||
127 | { |
||
128 | $this->showPage->completeOrderLastPayment($order); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @When /^I mark (this order)'s payment as refunded$/ |
||
133 | */ |
||
134 | public function iMarkThisOrderSPaymentAsRefunded(OrderInterface $order) |
||
135 | { |
||
136 | $this->showPage->refundOrderLastPayment($order); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @When specify its tracking code as :trackingCode |
||
141 | */ |
||
142 | public function specifyItsTrackingCodeAs($trackingCode) |
||
143 | { |
||
144 | $this->showPage->specifyTrackingCode($trackingCode); |
||
145 | $this->sharedStorage->set('tracking_code', $trackingCode); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @When /^I ship (this order)$/ |
||
150 | */ |
||
151 | public function iShipThisOrder(OrderInterface $order) |
||
152 | { |
||
153 | $this->showPage->shipOrder($order); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @When I switch the way orders are sorted by :fieldName |
||
158 | */ |
||
159 | public function iSwitchSortingBy($fieldName) |
||
160 | { |
||
161 | $this->indexPage->sortBy($fieldName); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @When I specify filter date from as :dateTime |
||
166 | */ |
||
167 | public function iSpecifyFilterDateFromAs($dateTime) |
||
168 | { |
||
169 | $this->indexPage->specifyFilterDateFrom(new \DateTime($dateTime)); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @When I specify filter date to as :dateTime |
||
174 | */ |
||
175 | public function iSpecifyFilterDateToAs($dateTime) |
||
176 | { |
||
177 | $this->indexPage->specifyFilterDateTo(new \DateTime($dateTime)); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @When I choose :channelName as a channel filter |
||
182 | */ |
||
183 | public function iChooseChannelAsAChannelFilter($channelName) |
||
184 | { |
||
185 | $this->indexPage->chooseChannelFilter($channelName); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @When I choose :currencyName as the filter currency |
||
190 | */ |
||
191 | public function iChooseCurrencyAsTheFilterCurrency($currencyName) |
||
192 | { |
||
193 | $this->indexPage->chooseCurrencyFilter($currencyName); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @When I specify filter total being greater than :total |
||
198 | */ |
||
199 | public function iSpecifyFilterTotalBeingGreaterThan($total) |
||
200 | { |
||
201 | $this->indexPage->specifyFilterTotalGreaterThan($total); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @When I specify filter total being less than :total |
||
206 | */ |
||
207 | public function iSpecifyFilterTotalBeingLessThan($total) |
||
208 | { |
||
209 | $this->indexPage->specifyFilterTotalLessThan($total); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @When I filter |
||
214 | */ |
||
215 | public function iFilter() |
||
216 | { |
||
217 | $this->indexPage->filter(); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @Then I should see a single order from customer :customer |
||
222 | */ |
||
223 | public function iShouldSeeASingleOrderFromCustomer(CustomerInterface $customer) |
||
224 | { |
||
225 | Assert::true( |
||
226 | $this->indexPage->isSingleResourceOnPage(['customer' => $customer->getEmail()]), |
||
227 | sprintf('Cannot find order for customer "%s" in the list.', $customer->getEmail()) |
||
228 | ); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @Then it should have been placed by the customer :customerEmail |
||
233 | */ |
||
234 | public function itShouldBePlacedByCustomer($customerEmail) |
||
235 | { |
||
236 | Assert::true( |
||
237 | $this->showPage->hasCustomer($customerEmail), |
||
238 | sprintf('Cannot find customer "%s".', $customerEmail) |
||
239 | ); |
||
240 | } |
||
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( |
||
247 | OrderInterface $order = null, |
||
248 | $customerName, |
||
249 | $street, |
||
250 | $postcode, |
||
251 | $city, |
||
252 | $countryName |
||
253 | ) { |
||
254 | if (null !== $order) { |
||
255 | $this->iSeeTheOrder($order); |
||
256 | } |
||
257 | |||
258 | Assert::true( |
||
259 | $this->showPage->hasShippingAddress($customerName, $street, $postcode, $city, $countryName), |
||
260 | sprintf('Cannot find shipping address "%s, %s %s, %s".', $street, $postcode, $city, $countryName) |
||
261 | ); |
||
262 | } |
||
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 shipping promotion should be :promotion |
||
437 | */ |
||
438 | public function theOrdersShippingPromotionDiscountShouldBe($promotionData) |
||
439 | { |
||
440 | Assert::same($this->showPage->getShippingPromotionData(), $promotionData); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * @Then /^the order's promotion total should(?:| still) be "([^"]+)"$/ |
||
445 | */ |
||
446 | public function theOrdersPromotionTotalShouldBe($promotionTotal) |
||
447 | { |
||
448 | $promotionTotalOnPage = $this->showPage->getPromotionTotal(); |
||
449 | |||
450 | Assert::eq( |
||
456 | |||
457 | /** |
||
458 | * @When I check :itemName data |
||
459 | */ |
||
460 | public function iCheckData($itemName) |
||
464 | |||
465 | /** |
||
466 | * @Then /^(its) code should be "([^"]+)"$/ |
||
467 | */ |
||
468 | public function itemCodeShouldBe($itemName, $code) |
||
478 | |||
479 | /** |
||
480 | * @Then /^(its) unit price should be ([^"]+)$/ |
||
481 | */ |
||
482 | public function itemUnitPriceShouldBe($itemName, $unitPrice) |
||
492 | |||
493 | /** |
||
494 | * @Then /^(its) discounted unit price should be ([^"]+)$/ |
||
495 | */ |
||
496 | public function itemDiscountedUnitPriceShouldBe($itemName, $discountedUnitPrice) |
||
506 | |||
507 | /** |
||
508 | * @Then /^(its) quantity should be ([^"]+)$/ |
||
509 | */ |
||
510 | public function itemQuantityShouldBe($itemName, $quantity) |
||
520 | |||
521 | /** |
||
522 | * @Then /^(its) subtotal should be ([^"]+)$/ |
||
523 | */ |
||
524 | public function itemSubtotalShouldBe($itemName, $subtotal) |
||
534 | |||
535 | /** |
||
536 | * @Then /^(its) discount should be ([^"]+)$/ |
||
537 | */ |
||
538 | public function theItemShouldHaveDiscount($itemName, $discount) |
||
548 | |||
549 | /** |
||
550 | * @Then /^(its) tax should be ([^"]+)$/ |
||
551 | */ |
||
552 | public function itemTaxShouldBe($itemName, $tax) |
||
562 | |||
563 | /** |
||
564 | * @Then /^(its) total should be ([^"]+)$/ |
||
565 | */ |
||
566 | public function itemTotalShouldBe($itemName, $total) |
||
576 | |||
577 | /** |
||
578 | * @Then I should be notified that the order's payment has been successfully completed |
||
579 | */ |
||
580 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyCompleted() |
||
586 | |||
587 | /** |
||
588 | * @Then I should be notified that the order's payment has been successfully refunded |
||
589 | */ |
||
590 | public function iShouldBeNotifiedThatTheOrderSPaymentHasBeenSuccessfullyRefunded() |
||
596 | |||
597 | /** |
||
598 | * @Then it should have payment state :paymentState |
||
599 | * @Then it should have payment with state :paymentState |
||
600 | */ |
||
601 | public function itShouldHavePaymentState($paymentState) |
||
608 | |||
609 | /** |
||
610 | * @Then it should have order's payment state :orderPaymentState |
||
611 | */ |
||
612 | public function itShouldHaveOrderPaymentState($orderPaymentState) |
||
620 | |||
621 | /** |
||
622 | * @Then it's payment state should be refunded |
||
623 | */ |
||
624 | public function orderPaymentStateShouldBeRefunded() |
||
632 | |||
633 | /** |
||
634 | * @Then /^I should not be able to mark (this order) as paid again$/ |
||
635 | */ |
||
636 | public function iShouldNotBeAbleToFinalizeItsPayment(OrderInterface $order) |
||
643 | |||
644 | /** |
||
645 | * @Then I should be notified that the order has been successfully shipped |
||
646 | */ |
||
647 | public function iShouldBeNotifiedThatTheOrderHasBeenSuccessfullyShipped() |
||
654 | |||
655 | /** |
||
656 | * @Then /^I should not be able to ship (this order)$/ |
||
657 | */ |
||
658 | public function iShouldNotBeAbleToShipThisOrder(OrderInterface $order) |
||
665 | |||
666 | /** |
||
667 | * @When I cancel this order |
||
668 | */ |
||
669 | public function iCancelThisOrder() |
||
673 | |||
674 | /** |
||
675 | * @Then I should be notified that it has been successfully updated |
||
676 | */ |
||
677 | public function iShouldBeNotifiedAboutItHasBeenSuccessfullyCanceled() |
||
684 | |||
685 | /** |
||
686 | * @Then I should not be able to cancel this order |
||
687 | */ |
||
688 | public function iShouldNotBeAbleToCancelThisOrder() |
||
695 | |||
696 | /** |
||
697 | * @Then this order should have state :state |
||
698 | * @Then its state should be :state |
||
699 | */ |
||
700 | public function itsStateShouldBe($state) |
||
708 | |||
709 | /** |
||
710 | * @Then it should( still) have a :state state |
||
711 | */ |
||
712 | public function itShouldHaveState($state) |
||
719 | |||
720 | /** |
||
721 | * @Then /^(the administrator) should know about (this additional note) for (this order made by "[^"]+")$/ |
||
722 | */ |
||
723 | public function theCustomerServiceShouldKnowAboutThisAdditionalNotes( |
||
736 | |||
737 | /** |
||
738 | * @Then I should see an order with :orderNumber number |
||
739 | */ |
||
740 | public function iShouldSeeOrderWithNumber($orderNumber) |
||
747 | |||
748 | /** |
||
749 | * @Then I should not see an order with :orderNumber number |
||
750 | */ |
||
751 | public function iShouldNotSeeOrderWithNumber($orderNumber) |
||
758 | |||
759 | /** |
||
760 | * @Then I should not see any orders with currency :currencyCode |
||
761 | */ |
||
762 | public function iShouldNotSeeAnyOrderWithCurrency($currencyCode) |
||
769 | |||
770 | /** |
||
771 | * @Then the first order should have number :number |
||
772 | */ |
||
773 | public function theFirstOrderShouldHaveNumber($number) |
||
783 | |||
784 | /** |
||
785 | * @Then it should have shipment in state :shipmentState |
||
786 | */ |
||
787 | public function itShouldHaveShipmentState($shipmentState) |
||
794 | |||
795 | /** |
||
796 | * @Then order :orderNumber should have shipment state :shippingState |
||
797 | */ |
||
798 | public function thisOrderShipmentStateShouldBe($shippingState) |
||
805 | |||
806 | /** |
||
807 | * @Then the order :order should have order payment state :orderPaymentState |
||
808 | * @Then /^(this order) should have order payment state "([^"]+)"$/ |
||
809 | */ |
||
810 | public function theOrderShouldHavePaymentState(OrderInterface $order, $orderPaymentState) |
||
817 | |||
818 | /** |
||
819 | * @Then /^(this order) should have order shipping state "([^"]+)"$/ |
||
820 | */ |
||
821 | public function theOrderShouldHaveShipmentState(OrderInterface $order, $orderShipmentState) |
||
828 | |||
829 | /** |
||
830 | * @Then /^there should be(?:| only) (\d+) payments?$/ |
||
831 | */ |
||
832 | public function theOrderShouldHaveNumberOfPayments($number) |
||
838 | |||
839 | /** |
||
840 | * @Then I should see the order :orderNumber with total :total |
||
841 | */ |
||
842 | public function iShouldSeeTheOrderWithTotal($orderNumber, $total) |
||
849 | |||
850 | /** |
||
851 | * @When /^I want to modify a customer's (?:billing|shipping) address of (this order)$/ |
||
852 | */ |
||
853 | public function iWantToModifyACustomerSShippingAddress(OrderInterface $order) |
||
857 | |||
858 | /** |
||
859 | * @When I save my changes |
||
860 | * @When I try to save my changes |
||
861 | */ |
||
862 | public function iSaveMyChanges() |
||
866 | |||
867 | /** |
||
868 | * @When /^I specify their (?:|new )shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
869 | */ |
||
870 | public function iSpecifyTheirShippingAddressAsFor(AddressInterface $address) |
||
874 | |||
875 | /** |
||
876 | * @When /^I specify their (?:|new )billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
877 | */ |
||
878 | public function iSpecifyTheirBillingAddressAsFor(AddressInterface $address) |
||
882 | |||
883 | /** |
||
884 | * @Then /^I should be notified that the "([^"]+)", the "([^"]+)", the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
885 | */ |
||
886 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $thirdElement, $fourthElement, $type) |
||
893 | |||
894 | /** |
||
895 | * @Then I should see :provinceName as province in the shipping address |
||
896 | */ |
||
897 | public function iShouldSeeAsProvinceInTheShippingAddress($provinceName) |
||
904 | |||
905 | /** |
||
906 | * @Then I should see :provinceName ad province in the billing address |
||
907 | */ |
||
908 | public function iShouldSeeAdProvinceInTheBillingAddress($provinceName) |
||
915 | |||
916 | /** |
||
917 | * @Then /^(the administrator) should know about IP address of (this order made by "[^"]+")$/ |
||
918 | */ |
||
919 | public function theAdministratorShouldKnowAboutIPAddressOfThisOrderMadeBy( |
||
936 | |||
937 | /** |
||
938 | * @When /^I (clear old billing address) information$/ |
||
939 | */ |
||
940 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
944 | |||
945 | /** |
||
946 | * @When /^I (clear old shipping address) information$/ |
||
947 | */ |
||
948 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
952 | |||
953 | /** |
||
954 | * @When /^I do not specify new information$/ |
||
955 | */ |
||
956 | public function iDoNotSpecifyNewInformation() |
||
960 | |||
961 | /** |
||
962 | * @Then /^(the administrator) should see that (order placed by "[^"]+") has "([^"]+)" currency$/ |
||
963 | */ |
||
964 | public function theAdministratorShouldSeeThatThisOrderHasBeenPlacedIn(AdminUserInterface $user, OrderInterface $order, $currency) |
||
975 | |||
976 | /** |
||
977 | * @Then /^(the administrator) should see the order with total "([^"]+)" in order list$/ |
||
978 | */ |
||
979 | public function theAdministratorShouldSeeTheOrderWithTotalInOrderList(AdminUserInterface $user, $total) |
||
990 | |||
991 | /** |
||
992 | * @Then there should be :count changes in the registry |
||
993 | */ |
||
994 | public function thereShouldBeCountChangesInTheRegistry($count) |
||
1001 | |||
1002 | /** |
||
1003 | * @Then I should not be able to refund this payment |
||
1004 | */ |
||
1005 | public function iShouldNotBeAbleToRefundThisPayment() |
||
1009 | |||
1010 | /** |
||
1011 | * @Then I should not see information about payments |
||
1012 | */ |
||
1013 | public function iShouldNotSeeInformationAboutPayments() |
||
1021 | |||
1022 | /** |
||
1023 | * @param string $type |
||
1024 | * @param string $element |
||
1025 | * @param string $expectedMessage |
||
1026 | * |
||
1027 | * @throws \InvalidArgumentException |
||
1028 | */ |
||
1029 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
1037 | } |
||
1038 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.