Complex classes like ParameterJuicer 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 ParameterJuicer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class ParameterJuicer implements ParameterJuicerInterface |
||
| 38 | { |
||
| 39 | const STRATEGY_IGNORE_EXTRA_VALUES = 0; |
||
| 40 | const STRATEGY_REFUSE_EXTRA_VALUES = 1; |
||
| 41 | const STRATEGY_ACCEPT_EXTRA_VALUES = 2; |
||
| 42 | |||
| 43 | /** @var array list of validators, must be callables */ |
||
| 44 | protected $validators = []; |
||
| 45 | |||
| 46 | /** @var array list of cleaners, must be callables */ |
||
| 47 | protected $cleaners = []; |
||
| 48 | |||
| 49 | /** @var array list of fields, this gives an information if the field |
||
| 50 | is mandatory or optional. */ |
||
| 51 | protected $fields = []; |
||
| 52 | |||
| 53 | /** @var array list of default values */ |
||
| 54 | protected $default_values = []; |
||
| 55 | |||
| 56 | /** @var int strategy of this juicer */ |
||
| 57 | public $strategy = self::STRATEGY_IGNORE_EXTRA_VALUES; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * getName |
||
| 61 | * |
||
| 62 | * @see ParameterJuicerInterface |
||
| 63 | */ |
||
| 64 | public function getName(): string |
||
| 68 | |||
| 69 | /** |
||
| 70 | * addField |
||
| 71 | * |
||
| 72 | * Declare a new field with no validators nor cleaner. It can be declared |
||
| 73 | * if the field is optional or mandatory. |
||
| 74 | * If the field already exists, it is overriden. |
||
| 75 | */ |
||
| 76 | public function addField(string $name, bool $is_mandatory = true): self |
||
| 82 | |||
| 83 | /** |
||
| 84 | * addFields |
||
| 85 | * |
||
| 86 | * Declare several fields at once.Existing fields are overriden. |
||
| 87 | */ |
||
| 88 | public function addFields(array $fields, $are_mandatory = true): self |
||
| 94 | |||
| 95 | /** |
||
| 96 | * removeField |
||
| 97 | * |
||
| 98 | * Remove an existing field with all validators or cleaners associated to |
||
| 99 | * it if any. It throws an exception if the field does not exist. |
||
| 100 | * |
||
| 101 | * @throws \InvalidArgumentException |
||
| 102 | */ |
||
| 103 | public function removeField(string $name): self |
||
| 118 | |||
| 119 | /** |
||
| 120 | * addValidator |
||
| 121 | * |
||
| 122 | * Add a new validator associated to a key. If the field is not already |
||
| 123 | * declared, it is created. |
||
| 124 | * |
||
| 125 | * @throws \InvalidArgumentException |
||
| 126 | */ |
||
| 127 | public function addValidator(string $name, callable $validator): self |
||
| 136 | |||
| 137 | /** |
||
| 138 | * addCleaner |
||
| 139 | * |
||
| 140 | * Add a new cleaner associated to a key. |
||
| 141 | * |
||
| 142 | * @throws \InvalidArgumentException |
||
| 143 | */ |
||
| 144 | public function addCleaner(string $name, callable $cleaner): self |
||
| 153 | |||
| 154 | /** |
||
| 155 | * setDefaultValue |
||
| 156 | * |
||
| 157 | * Set a default value for a field. If the field is not set or its value is |
||
| 158 | * null, this value will be set instead. This is triggered AFTER the |
||
| 159 | * cleaners which is useful because some cleanders can return null and then |
||
| 160 | * default value is applied. |
||
| 161 | * |
||
| 162 | * @throws \InvalidArgumentException |
||
| 163 | */ |
||
| 164 | public function setDefaultValue(string $name, $value): self |
||
| 173 | |||
| 174 | /** |
||
| 175 | * addJuicer |
||
| 176 | * |
||
| 177 | * Add a juicer to clean a validate a subset of data. |
||
| 178 | * |
||
| 179 | * @throws \InvalidArgumentException |
||
| 180 | */ |
||
| 181 | public function addJuicer(string $name, ParameterJuicerInterface $juicer): self |
||
| 188 | |||
| 189 | /** |
||
| 190 | * setStrategy |
||
| 191 | * |
||
| 192 | * Set the extra fields strategy for this juicer. |
||
| 193 | */ |
||
| 194 | public function setStrategy(int $strategy): self |
||
| 200 | |||
| 201 | /** |
||
| 202 | * squash |
||
| 203 | * |
||
| 204 | * Clean & validate the given data according to the definition. |
||
| 205 | */ |
||
| 206 | public function squash(array $values): array |
||
| 213 | |||
| 214 | /** |
||
| 215 | * validate |
||
| 216 | * |
||
| 217 | * Trigger validation on values. |
||
| 218 | * |
||
| 219 | * @see ParameterJuicerInterface |
||
| 220 | */ |
||
| 221 | public function validate(string $name, array $values): ParameterJuicerInterface |
||
| 236 | |||
| 237 | /** |
||
| 238 | * refuseExtraFields |
||
| 239 | * |
||
| 240 | * Fill the exception with refused extra fields if any. |
||
| 241 | */ |
||
| 242 | private function refuseExtraFields(array $values, ValidationException $exception): self |
||
| 254 | |||
| 255 | /** |
||
| 256 | * validateFields |
||
| 257 | * |
||
| 258 | * Check mandatory fields and launch validators. |
||
| 259 | */ |
||
| 260 | private function validateFields(array $values, ValidationException $exception): self |
||
| 286 | |||
| 287 | /** |
||
| 288 | * setDefaultValues |
||
| 289 | * |
||
| 290 | * Apply default values. When a field is not present in the values, the |
||
| 291 | * default value is set. |
||
| 292 | */ |
||
| 293 | private function setDefaultValues(array $values): array |
||
| 303 | |||
| 304 | /** |
||
| 305 | * clean |
||
| 306 | * |
||
| 307 | * Clean and return values. |
||
| 308 | * |
||
| 309 | * @see ParameterJuicerInterface |
||
| 310 | */ |
||
| 311 | public function clean(array $values): array |
||
| 319 | |||
| 320 | /** |
||
| 321 | * triggerCleaning |
||
| 322 | * |
||
| 323 | * Launch cleaners on the values. |
||
| 324 | */ |
||
| 325 | private function triggerCleaning(array $values): array |
||
| 343 | |||
| 344 | /** |
||
| 345 | * checkFieldExists |
||
| 346 | * |
||
| 347 | * Throw an exception if the field does not exist. |
||
| 348 | * |
||
| 349 | * @throws \InvalidArgumentException |
||
| 350 | */ |
||
| 351 | private function checkFieldExists(string $name): self |
||
| 365 | |||
| 366 | /** |
||
| 367 | * launchValidatorsFor |
||
| 368 | * |
||
| 369 | * Triger validators for the given field if any. |
||
| 370 | * |
||
| 371 | * @throws \RuntimeException if the callable fails. |
||
| 372 | */ |
||
| 373 | private function launchValidatorsFor(string $field, $value, ValidationException $exception): self |
||
| 389 | } |
||
| 390 | |||
| 391 |