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) |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function getItemsTotal() |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function getTotal() |
||
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | public function getShippingTotal() |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function getTaxTotal() |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function hasShippingCharge($shippingCharge) |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function getPromotionTotal() |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | public function hasPromotionDiscount($promotionDiscount) |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | public function hasShippingPromotion($promotionName) |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | public function hasTax($tax) |
||
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | public function getItemCode($itemName) |
||
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function getItemUnitPrice($itemName) |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function getItemDiscountedUnitPrice($itemName) |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function getItemQuantity($itemName) |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function getItemSubtotal($itemName) |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function getItemDiscount($itemName) |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | public function getItemTax($itemName) |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function getItemTotal($itemName) |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | public function getPaymentAmount() |
||
324 | |||
325 | /** |
||
326 | * {@inheritdoc} |
||
327 | */ |
||
328 | public function getPaymentsCount() |
||
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | */ |
||
338 | public function hasCancelButton() |
||
342 | |||
343 | /** |
||
344 | * {@inheritdoc} |
||
345 | */ |
||
346 | public function getOrderState() |
||
350 | |||
351 | /** |
||
352 | * {@inheritdoc} |
||
353 | */ |
||
354 | public function getPaymentState() |
||
358 | |||
359 | public function cancelOrder() |
||
363 | |||
364 | public function deleteOrder() |
||
368 | |||
369 | /** |
||
370 | * {@inheritdoc} |
||
371 | */ |
||
372 | public function hasNote($note) |
||
378 | |||
379 | /** |
||
380 | * {@inheritdoc} |
||
381 | */ |
||
382 | public function hasShippingProvinceName($provinceName) |
||
388 | |||
389 | /** |
||
390 | * {@inheritdoc} |
||
391 | */ |
||
392 | public function hasBillingProvinceName($provinceName) |
||
398 | |||
399 | /** |
||
400 | * {@inheritdoc} |
||
401 | */ |
||
402 | public function getIpAddressAssigned() |
||
406 | |||
407 | /** |
||
408 | * {@inheritdoc} |
||
409 | */ |
||
410 | public function getOrderCurrency() |
||
414 | |||
415 | /** |
||
416 | * {@inheritdoc} |
||
417 | */ |
||
418 | public function hasRefundButton() |
||
422 | |||
423 | /** |
||
424 | * {@inheritdoc} |
||
425 | */ |
||
426 | public function getShippingPromotionData() |
||
430 | |||
431 | /** |
||
432 | * {@inheritdoc} |
||
433 | */ |
||
434 | public function getRouteName() |
||
438 | |||
439 | /** |
||
440 | * {@inheritdoc} |
||
441 | */ |
||
442 | protected function getDefinedElements() |
||
467 | |||
468 | /** |
||
469 | * @return TableAccessorInterface |
||
470 | */ |
||
471 | protected function getTableAccessor() |
||
475 | |||
476 | /** |
||
477 | * @param string $elementText |
||
478 | * @param string $customerName |
||
479 | * @param string $street |
||
480 | * @param string $postcode |
||
481 | * @param string $city |
||
482 | * @param string $countryName |
||
483 | * |
||
484 | * @return bool |
||
485 | */ |
||
486 | private function hasAddress($elementText, $customerName, $street, $postcode, $city, $countryName) |
||
495 | |||
496 | /** |
||
497 | * @param string $itemName |
||
498 | * @param string $property |
||
499 | * |
||
500 | * @return string |
||
501 | */ |
||
502 | private function getItemProperty($itemName, $property) |
||
511 | |||
512 | /** |
||
513 | * @param OrderInterface $order |
||
514 | * |
||
515 | * @return NodeElement|null |
||
516 | */ |
||
517 | private function getLastOrderPaymentElement(OrderInterface $order) |
||
526 | |||
527 | /** |
||
528 | * @param OrderInterface $order |
||
529 | * |
||
530 | * @return NodeElement|null |
||
531 | */ |
||
532 | private function getLastOrderShipmentElement(OrderInterface $order) |
||
541 | } |
||
542 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.