Complex classes like ShowPage 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 ShowPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class ShowPage extends SymfonyPage implements ShowPageInterface |
||
| 27 | { |
||
| 28 | /** @var TableAccessorInterface */ |
||
| 29 | private $tableAccessor; |
||
| 30 | |||
| 31 | /** @var MoneyFormatterInterface */ |
||
| 32 | private $moneyFormatter; |
||
| 33 | |||
| 34 | public function __construct( |
||
| 35 | Session $session, |
||
| 36 | $minkParameters, |
||
| 37 | RouterInterface $router, |
||
| 38 | TableAccessorInterface $tableAccessor, |
||
| 39 | MoneyFormatterInterface $moneyFormatter |
||
| 40 | ) { |
||
| 41 | parent::__construct($session, $minkParameters, $router); |
||
| 42 | |||
| 43 | $this->tableAccessor = $tableAccessor; |
||
| 44 | $this->moneyFormatter = $moneyFormatter; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function hasCustomer(string $customerName): bool |
||
| 48 | { |
||
| 49 | $customerText = $this->getElement('customer')->getText(); |
||
| 50 | |||
| 51 | return stripos($customerText, $customerName) !== false; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function hasShippingAddress(string $customerName, string $street, string $postcode, string $city, string $countryName): bool |
||
| 55 | { |
||
| 56 | $shippingAddressText = $this->getElement('shipping_address')->getText(); |
||
| 57 | |||
| 58 | return $this->hasAddress($shippingAddressText, $customerName, $street, $postcode, $city, $countryName); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function hasShippingAddressVisible(): bool |
||
| 62 | { |
||
| 63 | try { |
||
| 64 | $this->getElement('shipping_address'); |
||
| 65 | } catch (ElementNotFoundException $exception) { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | return true; |
||
| 70 | } |
||
| 71 | |||
| 72 | public function hasBillingAddress(string $customerName, string $street, string $postcode, string $city, string $countryName): bool |
||
| 73 | { |
||
| 74 | $billingAddressText = $this->getElement('billing_address')->getText(); |
||
| 75 | |||
| 76 | return $this->hasAddress($billingAddressText, $customerName, $street, $postcode, $city, $countryName); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function hasShipment(string $shippingDetails): bool |
||
| 80 | { |
||
| 81 | $shipmentsText = $this->getElement('shipments')->getText(); |
||
| 82 | |||
| 83 | return stripos($shipmentsText, $shippingDetails) !== false; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function specifyTrackingCode(string $code): void |
||
| 87 | { |
||
| 88 | $this->getDocument()->fillField('sylius_shipment_ship_tracking', $code); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function canShipOrder(OrderInterface $order): bool |
||
| 92 | { |
||
| 93 | return $this->getLastOrderShipmentElement($order)->hasButton('Ship'); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function shipOrder(OrderInterface $order): void |
||
| 97 | { |
||
| 98 | $this->getLastOrderShipmentElement($order)->pressButton('Ship'); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function hasPayment(string $paymentDetails): bool |
||
| 102 | { |
||
| 103 | $paymentsText = $this->getElement('payments')->getText(); |
||
| 104 | |||
| 105 | return stripos($paymentsText, $paymentDetails) !== false; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function canCompleteOrderLastPayment(OrderInterface $order): bool |
||
| 112 | |||
| 113 | public function completeOrderLastPayment(OrderInterface $order): void |
||
| 114 | { |
||
| 115 | $this->getLastOrderPaymentElement($order)->pressButton('Complete'); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function refundOrderLastPayment(OrderInterface $order): void |
||
| 119 | { |
||
| 120 | $this->getLastOrderPaymentElement($order)->pressButton('Refund'); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function countItems(): int |
||
| 124 | { |
||
| 127 | |||
| 128 | public function isProductInTheList(string $productName): bool |
||
| 150 | |||
| 151 | public function getItemsTotal(): string |
||
| 157 | |||
| 158 | public function getTotal(): string |
||
| 164 | |||
| 165 | public function getShippingTotal(): string |
||
| 171 | |||
| 172 | public function getTaxTotal(): string |
||
| 178 | |||
| 179 | public function hasShippingCharge(string $shippingCharge): bool |
||
| 189 | |||
| 190 | public function getOrderPromotionTotal(): string |
||
| 206 | |||
| 207 | public function hasPromotionDiscount(string $promotionDiscount): bool |
||
| 213 | |||
| 214 | public function hasTax(string $tax): bool |
||
| 220 | |||
| 221 | public function getItemCode(string $itemName): string |
||
| 225 | |||
| 226 | public function getItemUnitPrice(string $itemName): string |
||
| 230 | |||
| 231 | public function getItemDiscountedUnitPrice(string $itemName): string |
||
| 235 | |||
| 236 | public function getItemOrderDiscount(string $itemName): string |
||
| 240 | |||
| 241 | public function getItemQuantity(string $itemName): string |
||
| 245 | |||
| 246 | public function getItemSubtotal(string $itemName): string |
||
| 250 | |||
| 251 | public function getItemDiscount(string $itemName): string |
||
| 255 | |||
| 256 | public function getItemTax(string $itemName): string |
||
| 260 | |||
| 261 | public function getItemTaxIncludedInPrice(string $itemName): string |
||
| 266 | |||
| 267 | public function getItemTotal(string $itemName): string |
||
| 271 | |||
| 272 | public function getPaymentAmount(): string |
||
| 278 | |||
| 279 | public function getPaymentsCount(): int |
||
| 289 | |||
| 290 | public function getShipmentsCount(): int |
||
| 300 | |||
| 301 | public function hasCancelButton(): bool |
||
| 305 | |||
| 306 | public function getOrderState(): string |
||
| 310 | |||
| 311 | public function getPaymentState(): string |
||
| 315 | |||
| 316 | public function getShippingState(): string |
||
| 320 | |||
| 321 | public function cancelOrder(): void |
||
| 325 | |||
| 326 | public function deleteOrder(): void |
||
| 330 | |||
| 331 | public function hasNote(string $note): bool |
||
| 337 | |||
| 338 | public function hasShippingProvinceName(string $provinceName): bool |
||
| 344 | |||
| 345 | public function hasBillingProvinceName(string $provinceName): bool |
||
| 351 | |||
| 352 | public function getIpAddressAssigned(): string |
||
| 356 | |||
| 357 | public function getOrderCurrency(): string |
||
| 361 | |||
| 362 | public function hasRefundButton(): bool |
||
| 366 | |||
| 367 | public function getShippingPromotionData(): string |
||
| 371 | |||
| 372 | public function getRouteName(): string |
||
| 376 | |||
| 377 | protected function getDefinedElements(): array |
||
| 404 | |||
| 405 | protected function getTableAccessor(): TableAccessorInterface |
||
| 409 | |||
| 410 | private function hasAddress(string $elementText, string $customerName, string $street, string $postcode, string $city, string $countryName): bool |
||
| 419 | |||
| 420 | private function getItemProperty(string $itemName, string $property): string |
||
| 429 | |||
| 430 | private function getRowWithItem(string $itemName): ?NodeElement |
||
| 434 | |||
| 435 | private function getLastOrderPaymentElement(OrderInterface $order): ?NodeElement |
||
| 444 | |||
| 445 | private function getLastOrderShipmentElement(OrderInterface $order): ?NodeElement |
||
| 454 | |||
| 455 | private function getFormattedMoney(int $orderPromotionTotal): string |
||
| 459 | } |
||
| 460 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.