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 |
||
| 25 | class ShowPage extends SymfonyPage implements ShowPageInterface |
||
| 26 | { |
||
| 27 | /** @var TableAccessorInterface */ |
||
| 28 | private $tableAccessor; |
||
| 29 | |||
| 30 | /** @var MoneyFormatterInterface */ |
||
| 31 | private $moneyFormatter; |
||
| 32 | |||
| 33 | public function __construct( |
||
| 34 | Session $session, |
||
| 35 | $minkParameters, |
||
| 36 | RouterInterface $router, |
||
| 37 | TableAccessorInterface $tableAccessor, |
||
| 38 | MoneyFormatterInterface $moneyFormatter |
||
| 39 | ) { |
||
| 40 | parent::__construct($session, $minkParameters, $router); |
||
| 41 | |||
| 42 | $this->tableAccessor = $tableAccessor; |
||
| 43 | $this->moneyFormatter = $moneyFormatter; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function hasCustomer(string $customerName): bool |
||
| 47 | { |
||
| 48 | $customerText = $this->getElement('customer')->getText(); |
||
| 49 | |||
| 50 | return stripos($customerText, $customerName) !== false; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function hasShippingAddress(string $customerName, string $street, string $postcode, string $city, string $countryName): bool |
||
| 54 | { |
||
| 55 | $shippingAddressText = $this->getElement('shipping_address')->getText(); |
||
| 56 | |||
| 57 | return $this->hasAddress($shippingAddressText, $customerName, $street, $postcode, $city, $countryName); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function hasShippingAddressVisible(): bool |
||
| 61 | { |
||
| 62 | try { |
||
| 63 | $this->getElement('shipping_address'); |
||
| 64 | } catch (ElementNotFoundException $exception) { |
||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | return true; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function hasBillingAddress(string $customerName, string $street, string $postcode, string $city, string $countryName): bool |
||
| 77 | |||
| 78 | public function hasShipment(string $shippingDetails): bool |
||
| 79 | { |
||
| 80 | $shipmentsText = $this->getElement('shipments')->getText(); |
||
| 81 | |||
| 84 | |||
| 85 | public function specifyTrackingCode(string $code): void |
||
| 89 | |||
| 90 | public function canShipOrder(OrderInterface $order): bool |
||
| 94 | |||
| 95 | public function shipOrder(OrderInterface $order): void |
||
| 99 | |||
| 100 | public function hasPayment(string $paymentDetails): bool |
||
| 106 | |||
| 107 | public function canCompleteOrderLastPayment(OrderInterface $order): bool |
||
| 111 | |||
| 112 | public function completeOrderLastPayment(OrderInterface $order): void |
||
| 116 | |||
| 117 | public function refundOrderLastPayment(OrderInterface $order): void |
||
| 121 | |||
| 122 | public function countItems(): int |
||
| 126 | |||
| 127 | public function isProductInTheList(string $productName): bool |
||
| 149 | |||
| 150 | public function getItemsTotal(): string |
||
| 156 | |||
| 157 | public function getTotal(): string |
||
| 163 | |||
| 164 | public function getShippingTotal(): string |
||
| 170 | |||
| 171 | public function getTaxTotal(): string |
||
| 177 | |||
| 178 | public function hasShippingCharge(string $shippingCharge): bool |
||
| 188 | |||
| 189 | public function getOrderPromotionTotal(): string |
||
| 205 | |||
| 206 | public function hasPromotionDiscount(string $promotionDiscount): bool |
||
| 212 | |||
| 213 | public function hasTax(string $tax): bool |
||
| 219 | |||
| 220 | public function getItemCode(string $itemName): string |
||
| 224 | |||
| 225 | public function getItemUnitPrice(string $itemName): string |
||
| 229 | |||
| 230 | public function getItemDiscountedUnitPrice(string $itemName): string |
||
| 234 | |||
| 235 | public function getItemOrderDiscount(string $itemName): string |
||
| 239 | |||
| 240 | public function getItemQuantity(string $itemName): string |
||
| 244 | |||
| 245 | public function getItemSubtotal(string $itemName): string |
||
| 249 | |||
| 250 | public function getItemDiscount(string $itemName): string |
||
| 254 | |||
| 255 | public function getItemTax(string $itemName): string |
||
| 259 | |||
| 260 | public function getItemTaxIncludedInPrice(string $itemName): string |
||
| 264 | |||
| 265 | public function getItemTotal(string $itemName): string |
||
| 269 | |||
| 270 | public function getPaymentAmount(): string |
||
| 276 | |||
| 277 | public function getPaymentsCount(): int |
||
| 287 | |||
| 288 | public function getShipmentsCount(): int |
||
| 298 | |||
| 299 | public function hasCancelButton(): bool |
||
| 303 | |||
| 304 | public function getOrderState(): string |
||
| 308 | |||
| 309 | public function getPaymentState(): string |
||
| 313 | |||
| 314 | public function getShippingState(): string |
||
| 318 | |||
| 319 | public function cancelOrder(): void |
||
| 323 | |||
| 324 | public function deleteOrder(): void |
||
| 328 | |||
| 329 | public function hasNote(string $note): bool |
||
| 335 | |||
| 336 | public function hasShippingProvinceName(string $provinceName): bool |
||
| 342 | |||
| 343 | public function hasBillingProvinceName(string $provinceName): bool |
||
| 349 | |||
| 350 | public function getIpAddressAssigned(): string |
||
| 354 | |||
| 355 | public function getOrderCurrency(): string |
||
| 359 | |||
| 360 | public function hasRefundButton(): bool |
||
| 364 | |||
| 365 | public function getShippingPromotionData(): string |
||
| 369 | |||
| 370 | public function getRouteName(): string |
||
| 374 | |||
| 375 | public function hasInformationAboutNoPayment(): bool |
||
| 379 | |||
| 380 | protected function getDefinedElements(): array |
||
| 407 | |||
| 408 | protected function getTableAccessor(): TableAccessorInterface |
||
| 412 | |||
| 413 | private function hasAddress(string $elementText, string $customerName, string $street, string $postcode, string $city, string $countryName): bool |
||
| 422 | |||
| 423 | private function getItemProperty(string $itemName, string $property): string |
||
| 432 | |||
| 433 | private function getRowWithItem(string $itemName): ?NodeElement |
||
| 437 | |||
| 438 | private function getLastOrderPaymentElement(OrderInterface $order): ?NodeElement |
||
| 447 | |||
| 448 | private function getLastOrderShipmentElement(OrderInterface $order): ?NodeElement |
||
| 457 | |||
| 458 | private function getFormattedMoney(int $orderPromotionTotal): string |
||
| 462 | } |
||
| 463 |
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.