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) |
||
| 63 | { |
||
| 64 | $patern = '(([0-9]{4})|([0-9]{2}:[0-9]{2}(:[0-9]{2})?))'; |
||
| 65 | return !(preg_match($patern, $hour) == 0 || (strlen($hour) != 4 && strlen($hour) != 8)); |
||
| 66 | } |
||
| 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) |
||
| 97 | { |
||
| 98 | if ($vol->isLinkedToOrder() || $vol->isBilled()) { |
||
| 99 | return $this; |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($this->isGroupedFlight($context) && $vol->getFlightType()->isBillingRequired() && $vol->isFree()) { |
||
| 103 | $this->addError('cost', 'Erreur ce type de vol doit être payant.'); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($vol->getFlightType()->isBillingRequired() && !$vol->hasReceiver()) { |
||
| 107 | $this->addError('cost', |
||
| 108 | 'Erreur ce type de vol doit être payant, mais personne n\'a été signalé comme recepteur d\'argent.'); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (!$this->isGroupedFlight($context) && $vol->getFlightType()->isBillingRequired() && ($vol->getAmountPerPassenger()) < $this->getMinPrice()) { |
||
| 112 | $this->addError('cost', |
||
| 113 | sprintf('Le montant demandé pour ce vol n\'est pas suffisant un minimum de %s euros est demandé', |
||
| 114 | $this->getMinPrice())); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $this; |
||
| 118 | } |
||
| 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 | } |