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 declare(strict_types=1); |
||
37 | class ParameterJuicer implements ParameterJuicerInterface |
||
38 | { |
||
39 | /** |
||
40 | * discard extra values (default) |
||
41 | */ |
||
42 | const STRATEGY_IGNORE_EXTRA_VALUES = 0; |
||
43 | /** |
||
44 | * error when extra values |
||
45 | */ |
||
46 | const STRATEGY_REFUSE_EXTRA_VALUES = 1; |
||
47 | /** |
||
48 | * accept extra values CAUTION: |
||
49 | * This could lead to unclean & unvalidated values to leak into |
||
50 | * safe area. Use this only if you know what you are doing. |
||
51 | */ |
||
52 | const STRATEGY_ACCEPT_EXTRA_VALUES = 2; |
||
53 | /** |
||
54 | * do not lauch form validation if field validation fails (default) |
||
55 | */ |
||
56 | const FORM_VALIDATORS_CONDITIONAL=0; |
||
57 | /** |
||
58 | * always launch form validation |
||
59 | */ |
||
60 | const FORM_VALIDATORS_ALWAYS=1; |
||
61 | |||
62 | /** @var array list of validators, must be callables */ |
||
63 | protected $validators = []; |
||
64 | |||
65 | /** @var array list of cleaners, must be callables */ |
||
66 | protected $cleaners = []; |
||
67 | |||
68 | /** @var array list of fields, this gives an information if the field |
||
69 | is mandatory or optional. */ |
||
70 | protected $fields = []; |
||
71 | |||
72 | /** @var array list of default values */ |
||
73 | protected $default_values = []; |
||
74 | |||
75 | /** @var array list of form cleaners, must be callables */ |
||
76 | protected $form_cleaners = []; |
||
77 | |||
78 | /** @var array list of form validators, must be callables */ |
||
79 | protected $form_validators = []; |
||
80 | |||
81 | /** @var int strategy of this juicer */ |
||
82 | public $strategy = self::STRATEGY_IGNORE_EXTRA_VALUES; |
||
83 | |||
84 | /** @var int is form validation triggered when field validation fails */ |
||
85 | public $form_validation_strategy = self::FORM_VALIDATORS_CONDITIONAL; |
||
86 | |||
87 | /** |
||
88 | * addField |
||
89 | * |
||
90 | * Declare a new field with no validators nor cleaner. It can be declared |
||
91 | * if the field is optional or mandatory. |
||
92 | * If the field already exists, it is overriden. |
||
93 | */ |
||
94 | public function addField(string $name, bool $is_mandatory = true): self |
||
100 | |||
101 | /** |
||
102 | * addFields |
||
103 | * |
||
104 | * Declare several fields at once.Existing fields are overriden. |
||
105 | */ |
||
106 | public function addFields(array $fields, $are_mandatory = true): self |
||
112 | |||
113 | /** |
||
114 | * removeField |
||
115 | * |
||
116 | * Remove an existing field with all validators or cleaners associated to |
||
117 | * it if any. It throws an exception if the field does not exist. |
||
118 | * |
||
119 | * @throws \InvalidArgumentException |
||
120 | */ |
||
121 | public function removeField(string $name): self |
||
136 | |||
137 | /** |
||
138 | * addValidator |
||
139 | * |
||
140 | * Add a new validator associated to a key. If the field is not already |
||
141 | * declared, it is created. |
||
142 | * |
||
143 | * @throws \InvalidArgumentException |
||
144 | */ |
||
145 | public function addValidator(string $name, callable $validator): self |
||
154 | |||
155 | /** |
||
156 | * addCleaner |
||
157 | * |
||
158 | * Add a new cleaner associated to a key. |
||
159 | * |
||
160 | * @throws \InvalidArgumentException |
||
161 | */ |
||
162 | public function addCleaner(string $name, callable $cleaner): self |
||
171 | |||
172 | /** |
||
173 | * setDefaultValue |
||
174 | * |
||
175 | * Set a default value for a field. If the field is not set or its value is |
||
176 | * null, this value will be set instead. This is triggered AFTER the |
||
177 | * cleaners which is useful because some cleanders can return null and then |
||
178 | * default value is applied. |
||
179 | * |
||
180 | * @throws \InvalidArgumentException |
||
181 | */ |
||
182 | public function setDefaultValue(string $name, $value): self |
||
197 | |||
198 | /** |
||
199 | * addFormCleaner |
||
200 | * |
||
201 | * Add a new cleaner associated to the whole set of values. |
||
202 | */ |
||
203 | public function addFormCleaner(callable $cleaner): self |
||
209 | |||
210 | /** |
||
211 | * addFormValidator |
||
212 | * |
||
213 | * Add a new validator to the whole set of values. |
||
214 | */ |
||
215 | public function addFormValidator(callable $validator): self |
||
221 | |||
222 | /** |
||
223 | * addJuicer |
||
224 | * |
||
225 | * Add a juicer to clean a validate a subset of data. |
||
226 | * |
||
227 | * @throws \InvalidArgumentException |
||
228 | */ |
||
229 | public function addJuicer(string $name, ParameterJuicerInterface $juicer): self |
||
235 | |||
236 | /** |
||
237 | * setStrategy |
||
238 | * |
||
239 | * Set the extra fields strategy for this juicer. |
||
240 | */ |
||
241 | public function setStrategy(int $strategy): self |
||
247 | |||
248 | /** |
||
249 | * setFormValidationStrategy |
||
250 | * |
||
251 | * Set the form validators strategy |
||
252 | */ |
||
253 | public function setFormValidationStrategy(int $strategy): self |
||
259 | |||
260 | /** |
||
261 | * squash |
||
262 | * |
||
263 | * Clean & validate the given data according to the definition. |
||
264 | */ |
||
265 | public function squash(array $values): array |
||
272 | |||
273 | /** |
||
274 | * validate |
||
275 | * |
||
276 | * Trigger validation on values. |
||
277 | * |
||
278 | * @see ParameterJuicerInterface |
||
279 | */ |
||
280 | public function validate(array $values) |
||
297 | |||
298 | /** |
||
299 | * refuseExtraFields |
||
300 | * |
||
301 | * Fill the exception with refused extra fields if any. |
||
302 | */ |
||
303 | private function refuseExtraFields(array $values, ValidationException $exception): self |
||
316 | |||
317 | /** |
||
318 | * validateFields |
||
319 | * |
||
320 | * Check mandatory fields and launch validators. |
||
321 | */ |
||
322 | private function validateFields(array $values, ValidationException $exception): self |
||
343 | |||
344 | /** |
||
345 | * validateForm |
||
346 | * |
||
347 | * form wide validation |
||
348 | */ |
||
349 | private function validateForm(array $values, ValidationException $exception): self |
||
359 | |||
360 | /** |
||
361 | * setDefaultValues |
||
362 | * |
||
363 | * Apply default values. When a field is not present in the values, the |
||
364 | * default value is set. |
||
365 | */ |
||
366 | private function setDefaultValues(array $values): array |
||
376 | |||
377 | /** |
||
378 | * clean |
||
379 | * |
||
380 | * Clean and return values. |
||
381 | * |
||
382 | * @see ParameterJuicerInterface |
||
383 | */ |
||
384 | public function clean(array $values): array |
||
392 | |||
393 | /** |
||
394 | * triggerCleaning |
||
395 | * |
||
396 | * Launch cleaners on the values. |
||
397 | */ |
||
398 | private function triggerCleaning(array $values): array |
||
420 | |||
421 | /** |
||
422 | * checkFieldExists |
||
423 | * |
||
424 | * Throw an exception if the field does not exist. |
||
425 | * |
||
426 | * @throws \InvalidArgumentException |
||
427 | */ |
||
428 | private function checkFieldExists(string $name): self |
||
442 | |||
443 | /** |
||
444 | * launchValidatorsFor |
||
445 | * |
||
446 | * Triger validators for the given field if any. |
||
447 | */ |
||
448 | private function launchValidatorsFor(string $field, $value, ValidationException $exception): self |
||
458 | |||
459 | /** |
||
460 | * launchValidators |
||
461 | * |
||
462 | * Apply validators against the given value. |
||
463 | * |
||
464 | * @throws \RuntimeException if the callable fails. |
||
465 | */ |
||
466 | private function launchValidators(array $validators, $value): self |
||
476 | } |
||
477 |