Complex classes like OrderSpec 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 OrderSpec, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | final class OrderSpec extends ObjectBehavior |
||
| 33 | { |
||
| 34 | function it_implements_an_order_interface(): void |
||
| 35 | { |
||
| 36 | $this->shouldImplement(OrderInterface::class); |
||
| 37 | } |
||
| 38 | |||
| 39 | function it_extends_an_order(): void |
||
| 40 | { |
||
| 41 | $this->shouldHaveType(BaseOrder::class); |
||
| 42 | } |
||
| 43 | |||
| 44 | function it_does_not_have_a_customer_defined_by_default(): void |
||
| 45 | { |
||
| 46 | $this->getCustomer()->shouldReturn(null); |
||
| 47 | } |
||
| 48 | |||
| 49 | function its_allows_defining_customer(CustomerInterface $customer): void |
||
| 50 | { |
||
| 51 | $this->setCustomer($customer); |
||
| 52 | $this->getCustomer()->shouldReturn($customer); |
||
| 53 | } |
||
| 54 | |||
| 55 | function its_channel_is_mutable(ChannelInterface $channel): void |
||
| 56 | { |
||
| 57 | $this->setChannel($channel); |
||
| 58 | $this->getChannel()->shouldReturn($channel); |
||
| 59 | } |
||
| 60 | |||
| 61 | function it_does_not_have_shipping_address_by_default(): void |
||
| 62 | { |
||
| 63 | $this->getShippingAddress()->shouldReturn(null); |
||
| 64 | } |
||
| 65 | |||
| 66 | function it_allows_defining_shipping_address(AddressInterface $address): void |
||
| 67 | { |
||
| 68 | $this->setShippingAddress($address); |
||
| 69 | $this->getShippingAddress()->shouldReturn($address); |
||
| 70 | } |
||
| 71 | |||
| 72 | function it_does_not_have_billing_address_by_default(): void |
||
| 73 | { |
||
| 74 | $this->getBillingAddress()->shouldReturn(null); |
||
| 75 | } |
||
| 76 | |||
| 77 | function it_allows_defining_billing_address(AddressInterface $address): void |
||
| 78 | { |
||
| 79 | $this->setBillingAddress($address); |
||
| 80 | $this->getBillingAddress()->shouldReturn($address); |
||
| 81 | } |
||
| 82 | |||
| 83 | function its_checkout_state_is_mutable(): void |
||
| 84 | { |
||
| 85 | $this->setCheckoutState(OrderCheckoutStates::STATE_CART); |
||
| 86 | $this->getCheckoutState()->shouldReturn(OrderCheckoutStates::STATE_CART); |
||
| 87 | } |
||
| 88 | |||
| 89 | function its_payment_state_is_mutable(): void |
||
| 90 | { |
||
| 91 | $this->setPaymentState(PaymentInterface::STATE_COMPLETED); |
||
| 92 | $this->getPaymentState()->shouldReturn(PaymentInterface::STATE_COMPLETED); |
||
| 93 | } |
||
| 94 | |||
| 95 | function it_initializes_item_units_collection_by_default(): void |
||
| 96 | { |
||
| 97 | $this->getItemUnits()->shouldHaveType(Collection::class); |
||
| 98 | } |
||
| 99 | |||
| 100 | function it_initializes_shipments_collection_by_default(): void |
||
| 101 | { |
||
| 102 | $this->getShipments()->shouldHaveType(Collection::class); |
||
| 103 | } |
||
| 104 | |||
| 105 | function it_adds_shipment_properly(ShipmentInterface $shipment): void |
||
| 106 | { |
||
| 107 | $this->shouldNotHaveShipment($shipment); |
||
| 108 | |||
| 109 | $shipment->setOrder($this)->shouldBeCalled(); |
||
| 110 | $this->addShipment($shipment); |
||
| 111 | |||
| 112 | $this->shouldHaveShipment($shipment); |
||
| 113 | } |
||
| 114 | |||
| 115 | function it_removes_a_shipment_properly(ShipmentInterface $shipment): void |
||
| 116 | { |
||
| 117 | $shipment->setOrder($this)->shouldBeCalled(); |
||
| 118 | $this->addShipment($shipment); |
||
| 119 | |||
| 120 | $this->shouldHaveShipment($shipment); |
||
| 121 | |||
| 122 | $shipment->setOrder(null)->shouldBeCalled(); |
||
| 123 | $this->removeShipment($shipment); |
||
| 124 | |||
| 125 | $this->shouldNotHaveShipment($shipment); |
||
| 126 | } |
||
| 127 | |||
| 128 | function it_removes_shipments(ShipmentInterface $shipment): void |
||
| 129 | { |
||
| 130 | $this->addShipment($shipment); |
||
| 131 | $this->hasShipment($shipment)->shouldReturn(true); |
||
| 132 | |||
| 133 | $this->removeShipments(); |
||
| 134 | |||
| 135 | $this->hasShipment($shipment)->shouldReturn(false); |
||
| 136 | } |
||
| 137 | |||
| 138 | function it_returns_shipping_adjustments( |
||
| 139 | AdjustmentInterface $shippingAdjustment, |
||
| 140 | AdjustmentInterface $taxAdjustment |
||
| 141 | ): void { |
||
| 142 | $shippingAdjustment->getType()->willReturn(AdjustmentInterface::SHIPPING_ADJUSTMENT); |
||
| 143 | $shippingAdjustment->setAdjustable($this)->shouldBeCalled(); |
||
| 144 | $shippingAdjustment->isNeutral()->willReturn(true); |
||
| 145 | |||
| 146 | $taxAdjustment->getType()->willReturn(AdjustmentInterface::TAX_ADJUSTMENT); |
||
| 147 | $taxAdjustment->setAdjustable($this)->shouldBeCalled(); |
||
| 148 | $taxAdjustment->isNeutral()->willReturn(true); |
||
| 149 | |||
| 150 | $this->addAdjustment($shippingAdjustment); |
||
| 151 | $this->addAdjustment($taxAdjustment); |
||
| 152 | |||
| 153 | $this->getAdjustments()->count()->shouldReturn(2); |
||
| 154 | |||
| 155 | $shippingAdjustments = $this->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT); |
||
| 156 | $shippingAdjustments->count()->shouldReturn(1); |
||
| 157 | $shippingAdjustments->first()->shouldReturn($shippingAdjustment); |
||
| 158 | } |
||
| 159 | |||
| 160 | function it_removes_shipping_adjustments( |
||
| 161 | AdjustmentInterface $shippingAdjustment, |
||
| 162 | AdjustmentInterface $taxAdjustment |
||
| 163 | ): void { |
||
| 164 | $shippingAdjustment->getType()->willReturn(AdjustmentInterface::SHIPPING_ADJUSTMENT); |
||
| 165 | $shippingAdjustment->setAdjustable($this)->shouldBeCalled(); |
||
| 166 | $shippingAdjustment->isNeutral()->willReturn(true); |
||
| 167 | |||
| 168 | $taxAdjustment->getType()->willReturn(AdjustmentInterface::TAX_ADJUSTMENT); |
||
| 169 | $taxAdjustment->setAdjustable($this)->shouldBeCalled(); |
||
| 170 | $taxAdjustment->isNeutral()->willReturn(true); |
||
| 171 | |||
| 172 | $this->addAdjustment($shippingAdjustment); |
||
| 173 | $this->addAdjustment($taxAdjustment); |
||
| 174 | |||
| 175 | $this->getAdjustments()->count()->shouldReturn(2); |
||
| 176 | |||
| 177 | $shippingAdjustment->isLocked()->willReturn(false); |
||
| 178 | $shippingAdjustment->setAdjustable(null)->shouldBeCalled(); |
||
| 179 | $this->removeAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT); |
||
| 180 | |||
| 181 | $this->getAdjustments()->count()->shouldReturn(1); |
||
| 182 | $this->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->count()->shouldReturn(0); |
||
| 183 | } |
||
| 184 | |||
| 185 | function it_returns_tax_adjustments( |
||
| 186 | AdjustmentInterface $shippingAdjustment, |
||
| 187 | AdjustmentInterface $taxAdjustment |
||
| 188 | ): void { |
||
| 189 | $shippingAdjustment->getType()->willReturn(AdjustmentInterface::SHIPPING_ADJUSTMENT); |
||
| 190 | $shippingAdjustment->setAdjustable($this)->shouldBeCalled(); |
||
| 191 | $shippingAdjustment->isNeutral()->willReturn(true); |
||
| 192 | |||
| 193 | $taxAdjustment->getType()->willReturn(AdjustmentInterface::TAX_ADJUSTMENT); |
||
| 194 | $taxAdjustment->setAdjustable($this)->shouldBeCalled(); |
||
| 195 | $taxAdjustment->isNeutral()->willReturn(true); |
||
| 196 | |||
| 197 | $this->addAdjustment($shippingAdjustment); |
||
| 198 | $this->addAdjustment($taxAdjustment); |
||
| 199 | |||
| 200 | $this->getAdjustments()->count()->shouldReturn(2); |
||
| 201 | |||
| 202 | $taxAdjustments = $this->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT); |
||
| 203 | $taxAdjustments->count()->shouldReturn(1); |
||
| 204 | $taxAdjustments->first()->shouldReturn($taxAdjustment); |
||
| 205 | } |
||
| 206 | |||
| 207 | function it_removes_tax_adjustments( |
||
| 208 | AdjustmentInterface $shippingAdjustment, |
||
| 209 | AdjustmentInterface $taxAdjustment |
||
| 210 | ): void { |
||
| 211 | $shippingAdjustment->getType()->willReturn(AdjustmentInterface::SHIPPING_ADJUSTMENT); |
||
| 212 | $shippingAdjustment->setAdjustable($this)->shouldBeCalled(); |
||
| 213 | $shippingAdjustment->isNeutral()->willReturn(true); |
||
| 214 | |||
| 215 | $taxAdjustment->getType()->willReturn(AdjustmentInterface::TAX_ADJUSTMENT); |
||
| 216 | $taxAdjustment->setAdjustable($this)->shouldBeCalled(); |
||
| 217 | $taxAdjustment->isNeutral()->willReturn(true); |
||
| 218 | |||
| 219 | $this->addAdjustment($shippingAdjustment); |
||
| 220 | $this->addAdjustment($taxAdjustment); |
||
| 221 | |||
| 222 | $this->getAdjustments()->count()->shouldReturn(2); |
||
| 223 | |||
| 224 | $taxAdjustment->isLocked()->willReturn(false); |
||
| 225 | $taxAdjustment->setAdjustable(null)->shouldBeCalled(); |
||
| 226 | $this->removeAdjustments(AdjustmentInterface::TAX_ADJUSTMENT); |
||
| 227 | |||
| 228 | $this->getAdjustments()->count()->shouldReturn(1); |
||
| 229 | $this->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)->count()->shouldReturn(0); |
||
| 230 | } |
||
| 231 | |||
| 232 | function it_does_not_have_a_currency_code_defined_by_default(): void |
||
| 233 | { |
||
| 234 | $this->getCurrencyCode()->shouldReturn(null); |
||
| 235 | } |
||
| 236 | |||
| 237 | function it_allows_defining_a_currency_code(): void |
||
| 238 | { |
||
| 239 | $this->setCurrencyCode('PLN'); |
||
| 240 | $this->getCurrencyCode()->shouldReturn('PLN'); |
||
| 241 | } |
||
| 242 | |||
| 243 | function it_has_no_default_locale_code(): void |
||
| 244 | { |
||
| 245 | $this->getLocaleCode()->shouldReturn(null); |
||
| 246 | } |
||
| 247 | |||
| 248 | function its_locale_code_is_mutable(): void |
||
| 249 | { |
||
| 250 | $this->setLocaleCode('en'); |
||
| 251 | $this->getLocaleCode()->shouldReturn('en'); |
||
| 252 | } |
||
| 253 | |||
| 254 | function it_has_a_cart_shipping_state_by_default(): void |
||
| 255 | { |
||
| 256 | $this->getShippingState()->shouldReturn(OrderShippingStates::STATE_CART); |
||
| 257 | } |
||
| 258 | |||
| 259 | function its_shipping_state_is_mutable(): void |
||
| 264 | |||
| 265 | function it_adds_and_removes_payments(PaymentInterface $payment): void |
||
| 266 | { |
||
| 267 | $payment->getState()->willReturn(PaymentInterface::STATE_NEW); |
||
| 268 | $payment->setOrder($this)->shouldBeCalled(); |
||
| 269 | |||
| 270 | $this->addPayment($payment); |
||
| 271 | $this->shouldHavePayment($payment); |
||
| 272 | |||
| 273 | $payment->setOrder(null)->shouldBeCalled(); |
||
| 274 | |||
| 275 | $this->removePayment($payment); |
||
| 276 | $this->shouldNotHavePayment($payment); |
||
| 277 | } |
||
| 278 | |||
| 279 | function it_returns_last_payment_with_given_state( |
||
| 280 | PaymentInterface $payment1, |
||
| 281 | PaymentInterface $payment2, |
||
| 282 | PaymentInterface $payment3, |
||
| 283 | PaymentInterface $payment4 |
||
| 284 | ): void { |
||
| 285 | $payment1->getState()->willReturn(PaymentInterface::STATE_CART); |
||
| 286 | $payment1->setOrder($this)->shouldBeCalled(); |
||
| 287 | |||
| 288 | $payment2->getState()->willReturn(PaymentInterface::STATE_CANCELLED); |
||
| 289 | $payment2->setOrder($this)->shouldBeCalled(); |
||
| 290 | |||
| 304 | |||
| 305 | function it_returns_a_null_if_there_is_no_payments_after_trying_to_get_last_payment(): void |
||
| 309 | |||
| 310 | function it_returns_last_payment_with_any_state_if_there_is_no_target_state_specified( |
||
| 335 | |||
| 336 | function it_adds_and_removes_shipments(ShipmentInterface $shipment): void |
||
| 348 | |||
| 349 | function it_has_a_promotion_coupon(PromotionCouponInterface $coupon): void |
||
| 354 | |||
| 355 | function it_counts_promotions_subjects(OrderItemInterface $item1, OrderItemInterface $item2): void |
||
| 370 | |||
| 371 | function it_adds_and_removes_promotions(PromotionInterface $promotion): void |
||
| 379 | |||
| 380 | function it_returns_0_tax_total_when_there_are_no_items_and_adjustments(): void |
||
| 384 | |||
| 385 | function it_returns_a_tax_of_all_items_as_tax_total_when_there_are_no_tax_adjustments( |
||
| 401 | |||
| 402 | function it_returns_a_tax_of_all_items_and_non_neutral_shipping_tax_as_tax_total( |
||
| 433 | |||
| 434 | function it_returns_a_tax_of_all_items_and_neutral_shipping_tax_as_tax_total( |
||
| 465 | |||
| 466 | function it_includes_a_non_neutral_tax_adjustments_in_shipping_total( |
||
| 485 | |||
| 486 | function it_returns_a_shipping_total_decreased_by_shipping_promotion( |
||
| 512 | |||
| 513 | function it_does_not_include_neutral_tax_adjustments_in_shipping_total( |
||
| 532 | |||
| 533 | function it_returns_0_as_promotion_total_when_there_are_no_order_promotion_adjustments(): void |
||
| 537 | |||
| 538 | function it_returns_a_sum_of_all_order_promotion_adjustments_applied_to_items_as_order_promotion_total( |
||
| 555 | |||
| 556 | function it_does_not_include_a_shipping_promotion_adjustment_in_order_promotion_total( |
||
| 575 | |||
| 576 | function it_has_a_token_value(): void |
||
| 582 | |||
| 583 | function it_has_customer_ip(): void |
||
| 588 | } |
||
| 589 |