Complex classes like FlightValidator 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 FlightValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class FlightValidator extends AbstractValidator |
||
12 | { |
||
13 | /** |
||
14 | * @var Product |
||
15 | */ |
||
16 | private $defaultService; |
||
17 | |||
18 | /** |
||
19 | * @inheritDoc |
||
20 | */ |
||
21 | public function __construct(Translate $langs, DoliDB $db, $defaultServiceId) |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function isValid($vol, $context = []) |
||
46 | |||
47 | /** |
||
48 | * @param array $context |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | private function isGroupedFlight($context) |
||
56 | |||
57 | /** |
||
58 | * @param string $hour |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | private function isHourValid($hour) |
||
67 | |||
68 | /** |
||
69 | * @param $defaultServiceId |
||
70 | */ |
||
71 | private function fetchService($defaultServiceId) |
||
76 | |||
77 | /** |
||
78 | * Returns the minimum price. |
||
79 | * |
||
80 | * @return int |
||
81 | */ |
||
82 | private function getMinPrice() |
||
89 | |||
90 | /** |
||
91 | * @param Bbcvols $vol |
||
92 | * @param array $context |
||
93 | * |
||
94 | * @return $this |
||
95 | */ |
||
96 | private function checkBillingInformation($vol, $context) |
||
119 | |||
120 | /** |
||
121 | * @param Bbcvols $vol |
||
122 | * @param array $context |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | private function checkInstructionInformation($vol, $context) |
||
141 | |||
142 | /** |
||
143 | * @param Bbcvols $vol |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | private function checkPassengersInformation($vol) |
||
172 | |||
173 | /** |
||
174 | * @param Bbcvols $vol |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | private function checkKilometers($vol) |
||
192 | |||
193 | /** |
||
194 | * @param Bbcvols $vol |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | private function checkFlightInformation($vol) |
||
222 | } |