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 | /** |
||
| 28 | * @var TableAccessorInterface |
||
| 29 | */ |
||
| 30 | private $tableAccessor; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param Session $session |
||
| 34 | * @param array $parameters |
||
| 35 | * @param RouterInterface $router |
||
| 36 | * @param TableAccessorInterface $tableAccessor |
||
| 37 | */ |
||
| 38 | public function __construct( |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | */ |
||
| 52 | public function hasCustomer($customerName) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public function hasShippingAddress($customerName, $street, $postcode, $city, $countryName) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function hasBillingAddress($customerName, $street, $postcode, $city, $countryName) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function hasShipment($shippingDetails) |
||
| 88 | |||
| 89 | public function specifyTrackingCode($code) |
||
| 90 | { |
||
| 91 | $this->getDocument()->fillField('sylius_shipment_ship_tracking', $code); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function canShipOrder(OrderInterface $order) |
||
| 95 | { |
||
| 96 | return $this->getLastOrderShipmentElement($order)->hasButton('Ship'); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function shipOrder(OrderInterface $order) |
||
| 100 | { |
||
| 101 | $this->getLastOrderShipmentElement($order)->pressButton('Ship'); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | public function hasPayment($paymentDetails) |
||
| 108 | { |
||
| 109 | $paymentsText = $this->getElement('payments')->getText(); |
||
| 110 | |||
| 111 | return stripos($paymentsText, $paymentDetails) !== false; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function canCompleteOrderLastPayment(OrderInterface $order) |
||
| 118 | { |
||
| 119 | return $this->getLastOrderPaymentElement($order)->hasButton('Complete'); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function completeOrderLastPayment(OrderInterface $order) |
||
| 126 | { |
||
| 127 | $this->getLastOrderPaymentElement($order)->pressButton('Complete'); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function countItems() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | public function isProductInTheList($productName) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | public function getItemsTotal() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function getTotal() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | public function getShippingTotal() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | public function getTaxTotal() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function hasShippingCharge($shippingCharge) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | public function getPromotionTotal() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | public function hasPromotionDiscount($promotionDiscount) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritdoc} |
||
| 228 | */ |
||
| 229 | public function hasTax($tax) |
||
| 230 | { |
||
| 231 | $taxesText = $this->getElement('taxes')->getText(); |
||
| 232 | |||
| 233 | return stripos($taxesText, $tax) !== false; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $itemName |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getItemUnitPrice($itemName) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $itemName |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getItemDiscountedUnitPrice($itemName) |
||
| 252 | { |
||
| 253 | return $this->getItemProperty($itemName, 'discounted-unit-price'); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $itemName |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function getItemQuantity($itemName) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $itemName |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getItemSubtotal($itemName) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $itemName |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function getItemDiscount($itemName) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $itemName |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getItemTax($itemName) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $itemName |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | public function getItemTotal($itemName) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * {@inheritdoc} |
||
| 308 | */ |
||
| 309 | public function hasCancelButton() |
||
| 310 | { |
||
| 311 | return $this->getDocument()->hasButton('Cancel'); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * {@inheritdoc} |
||
| 316 | */ |
||
| 317 | public function getOrderState() |
||
| 318 | { |
||
| 319 | return $this->getElement('order_state')->getText(); |
||
| 320 | } |
||
| 321 | |||
| 322 | public function cancelOrder() |
||
| 323 | { |
||
| 324 | $this->getDocument()->pressButton('Cancel'); |
||
| 325 | } |
||
| 326 | |||
| 327 | public function deleteOrder() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * {@inheritdoc} |
||
| 334 | */ |
||
| 335 | public function hasNote($note) |
||
| 336 | { |
||
| 337 | $orderNotesElement = $this->getElement('order_notes'); |
||
| 338 | |||
| 339 | return $orderNotesElement->getText() === $note; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * {@inheritdoc} |
||
| 344 | */ |
||
| 345 | public function getRouteName() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | protected function getDefinedElements() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return TableAccessorInterface |
||
| 377 | */ |
||
| 378 | protected function getTableAccessor() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $elementText |
||
| 385 | * @param string $customerName |
||
| 386 | * @param string $street |
||
| 387 | * @param string $postcode |
||
| 388 | * @param string $city |
||
| 389 | * @param string $countryName |
||
| 390 | * |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | private function hasAddress($elementText, $customerName, $street, $postcode, $city, $countryName) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $itemName |
||
| 405 | * @param string $property |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | private function getItemProperty($itemName, $property) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param OrderInterface $order |
||
| 421 | * |
||
| 422 | * @return NodeElement|null |
||
| 423 | */ |
||
| 424 | private function getLastOrderPaymentElement(OrderInterface $order) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param OrderInterface $order |
||
| 436 | * |
||
| 437 | * @return NodeElement|null |
||
| 438 | */ |
||
| 439 | private function getLastOrderShipmentElement(OrderInterface $order) |
||
| 448 | } |
||
| 449 |