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 = []) |
||
47 | |||
48 | /** |
||
49 | * @param array $context |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | private function isGroupedFlight($context) |
||
57 | |||
58 | /** |
||
59 | * @param string $hour |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | private function isHourValid($hour) |
||
68 | |||
69 | /** |
||
70 | * @param $defaultServiceId |
||
71 | */ |
||
72 | private function fetchService($defaultServiceId) |
||
77 | |||
78 | /** |
||
79 | * Returns the minimum price. |
||
80 | * |
||
81 | * @return int |
||
82 | */ |
||
83 | private function getMinPrice() |
||
90 | |||
91 | /** |
||
92 | * @param Bbcvols $vol |
||
93 | * @param array $context |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | private function checkBillingInformation($vol, $context) |
||
120 | |||
121 | /** |
||
122 | * @param Bbcvols $vol |
||
123 | * @param array $context |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | private function checkInstructionInformation($vol, $context) |
||
142 | |||
143 | /** |
||
144 | * @param Bbcvols $vol |
||
145 | * |
||
146 | * @return $this |
||
147 | */ |
||
148 | private function checkPassengersInformation($vol) |
||
174 | |||
175 | /** |
||
176 | * @param Bbcvols $vol |
||
177 | * |
||
178 | * @return $this |
||
179 | */ |
||
180 | private function checkKilometers($vol) |
||
194 | |||
195 | /** |
||
196 | * @param Bbcvols $vol |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | private function checkFlightInformation($vol) |
||
224 | |||
225 | /** |
||
226 | * @param Bbcvols $vol |
||
227 | * |
||
228 | * @return $this |
||
229 | */ |
||
230 | private function checkOrderInformation(Bbcvols $vol) |
||
251 | } |