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) |
||
| 93 | |||
| 94 | public function canShipOrder(OrderInterface $order) |
||
| 98 | |||
| 99 | public function shipOrder(OrderInterface $order) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | public function hasPayment($paymentDetails) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function canCompleteOrderLastPayment(OrderInterface $order) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function completeOrderLastPayment(OrderInterface $order) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function refundOrderLastPayment(OrderInterface $order) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | public function countItems() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | public function isProductInTheList($productName) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function getItemsTotal() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function getTotal() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | public function getShippingTotal() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | public function getTaxTotal() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function hasShippingCharge($shippingCharge) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | public function getPromotionTotal() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function hasPromotionDiscount($promotionDiscount) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | */ |
||
| 237 | public function hasTax($tax) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function getItemCode($itemName) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function getItemUnitPrice($itemName) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | public function getItemDiscountedUnitPrice($itemName) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function getItemQuantity($itemName) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | public function getItemSubtotal($itemName) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function getItemDiscount($itemName) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function getItemTax($itemName) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | public function getItemTotal($itemName) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function getPaymentAmount() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritdoc} |
||
| 320 | */ |
||
| 321 | public function getPaymentsCount() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritdoc} |
||
| 330 | */ |
||
| 331 | public function hasCancelButton() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * {@inheritdoc} |
||
| 338 | */ |
||
| 339 | public function getOrderState() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritdoc} |
||
| 346 | */ |
||
| 347 | public function getPaymentState() |
||
| 351 | |||
| 352 | public function cancelOrder() |
||
| 356 | |||
| 357 | public function deleteOrder() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * {@inheritdoc} |
||
| 364 | */ |
||
| 365 | public function hasNote($note) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * {@inheritdoc} |
||
| 374 | */ |
||
| 375 | public function hasShippingProvinceName($provinceName) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * {@inheritdoc} |
||
| 384 | */ |
||
| 385 | public function hasBillingProvinceName($provinceName) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * {@inheritdoc} |
||
| 394 | */ |
||
| 395 | public function getIpAddressAssigned() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc} |
||
| 402 | */ |
||
| 403 | public function getOrderCurrency() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * {@inheritdoc} |
||
| 410 | */ |
||
| 411 | public function getRouteName() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * {@inheritdoc} |
||
| 418 | */ |
||
| 419 | protected function getDefinedElements() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return TableAccessorInterface |
||
| 446 | */ |
||
| 447 | protected function getTableAccessor() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $elementText |
||
| 454 | * @param string $customerName |
||
| 455 | * @param string $street |
||
| 456 | * @param string $postcode |
||
| 457 | * @param string $city |
||
| 458 | * @param string $countryName |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | private function hasAddress($elementText, $customerName, $street, $postcode, $city, $countryName) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $itemName |
||
| 474 | * @param string $property |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | private function getItemProperty($itemName, $property) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @param OrderInterface $order |
||
| 490 | * |
||
| 491 | * @return NodeElement|null |
||
| 492 | */ |
||
| 493 | private function getLastOrderPaymentElement(OrderInterface $order) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param OrderInterface $order |
||
| 505 | * |
||
| 506 | * @return NodeElement|null |
||
| 507 | */ |
||
| 508 | private function getLastOrderShipmentElement(OrderInterface $order) |
||
| 517 | } |
||
| 518 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.