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 | * addField |
||
| 61 | * |
||
| 62 | * Declare a new field with no validators nor cleaner. It can be declared |
||
| 63 | * if the field is optional or mandatory. |
||
| 64 | * If the field already exists, it is overriden. |
||
| 65 | */ |
||
| 66 | public function addField(string $name, bool $is_mandatory = true): self |
||
| 72 | |||
| 73 | /** |
||
| 74 | * addFields |
||
| 75 | * |
||
| 76 | * Declare several fields at once.Existing fields are overriden. |
||
| 77 | */ |
||
| 78 | public function addFields(array $fields, $are_mandatory = true): self |
||
| 84 | |||
| 85 | /** |
||
| 86 | * removeField |
||
| 87 | * |
||
| 88 | * Remove an existing field with all validators or cleaners associated to |
||
| 89 | * it if any. It throws an exception if the field does not exist. |
||
| 90 | * |
||
| 91 | * @throws \InvalidArgumentException |
||
| 92 | */ |
||
| 93 | public function removeField(string $name): self |
||
| 108 | |||
| 109 | /** |
||
| 110 | * addValidator |
||
| 111 | * |
||
| 112 | * Add a new validator associated to a key. If the field is not already |
||
| 113 | * declared, it is created. |
||
| 114 | * |
||
| 115 | * @throws \InvalidArgumentException |
||
| 116 | */ |
||
| 117 | public function addValidator(string $name, callable $validator): self |
||
| 126 | |||
| 127 | /** |
||
| 128 | * addCleaner |
||
| 129 | * |
||
| 130 | * Add a new cleaner associated to a key. |
||
| 131 | * |
||
| 132 | * @throws \InvalidArgumentException |
||
| 133 | */ |
||
| 134 | public function addCleaner(string $name, callable $cleaner): self |
||
| 143 | |||
| 144 | /** |
||
| 145 | * setDefaultValue |
||
| 146 | * |
||
| 147 | * Set a default value for a field. If the field is not set or its value is |
||
| 148 | * null, this value will be set instead. This is triggered AFTER the |
||
| 149 | * cleaners which is useful because some cleanders can return null and then |
||
| 150 | * default value is applied. |
||
| 151 | * |
||
| 152 | * @throws \InvalidArgumentException |
||
| 153 | */ |
||
| 154 | public function setDefaultValue(string $name, $value): self |
||
| 163 | |||
| 164 | /** |
||
| 165 | * addJuicer |
||
| 166 | * |
||
| 167 | * Add a juicer to clean a validate a subset of data. |
||
| 168 | * |
||
| 169 | * @throws \InvalidArgumentException |
||
| 170 | */ |
||
| 171 | public function addJuicer(string $name, ParameterJuicerInterface $juicer): self |
||
| 178 | |||
| 179 | /** |
||
| 180 | * setStrategy |
||
| 181 | * |
||
| 182 | * Set the extra fields strategy for this juicer. |
||
| 183 | */ |
||
| 184 | public function setStrategy(int $strategy): self |
||
| 190 | |||
| 191 | /** |
||
| 192 | * squash |
||
| 193 | * |
||
| 194 | * Clean & validate the given data according to the definition. |
||
| 195 | */ |
||
| 196 | public function squash(array $values): array |
||
| 203 | |||
| 204 | /** |
||
| 205 | * validate |
||
| 206 | * |
||
| 207 | * Trigger validation on values. |
||
| 208 | * |
||
| 209 | * @see ParameterJuicerInterface |
||
| 210 | */ |
||
| 211 | public function validate(array $values): ParameterJuicerInterface |
||
| 226 | |||
| 227 | /** |
||
| 228 | * refuseExtraFields |
||
| 229 | * |
||
| 230 | * Fill the exception with refused extra fields if any. |
||
| 231 | */ |
||
| 232 | private function refuseExtraFields(array $values, ValidationException $exception): self |
||
| 245 | |||
| 246 | /** |
||
| 247 | * validateFields |
||
| 248 | * |
||
| 249 | * Check mandatory fields and launch validators. |
||
| 250 | */ |
||
| 251 | private function validateFields(array $values, ValidationException $exception): self |
||
| 273 | |||
| 274 | /** |
||
| 275 | * setDefaultValues |
||
| 276 | * |
||
| 277 | * Apply default values. When a field is not present in the values, the |
||
| 278 | * default value is set. |
||
| 279 | */ |
||
| 280 | private function setDefaultValues(array $values): array |
||
| 290 | |||
| 291 | /** |
||
| 292 | * clean |
||
| 293 | * |
||
| 294 | * Clean and return values. |
||
| 295 | * |
||
| 296 | * @see ParameterJuicerInterface |
||
| 297 | */ |
||
| 298 | public function clean(array $values): array |
||
| 306 | |||
| 307 | /** |
||
| 308 | * triggerCleaning |
||
| 309 | * |
||
| 310 | * Launch cleaners on the values. |
||
| 311 | */ |
||
| 312 | private function triggerCleaning(array $values): array |
||
| 330 | |||
| 331 | /** |
||
| 332 | * checkFieldExists |
||
| 333 | * |
||
| 334 | * Throw an exception if the field does not exist. |
||
| 335 | * |
||
| 336 | * @throws \InvalidArgumentException |
||
| 337 | */ |
||
| 338 | private function checkFieldExists(string $name): self |
||
| 352 | |||
| 353 | /** |
||
| 354 | * launchValidatorsFor |
||
| 355 | * |
||
| 356 | * Triger validators for the given field if any. |
||
| 357 | * |
||
| 358 | * @throws \RuntimeException if the callable fails. |
||
| 359 | */ |
||
| 360 | private function launchValidatorsFor(string $field, $value, ValidationException $exception): self |
||
| 376 | } |
||
| 377 | |||
| 378 |