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 | const FORM_VALIDATORS_CONDITIONAL=0; |
||
43 | const FORM_VALIDATORS_ALWAYS=1; |
||
44 | |||
45 | /** @var array list of validators, must be callables */ |
||
46 | protected $validators = []; |
||
47 | |||
48 | /** @var array list of cleaners, must be callables */ |
||
49 | protected $cleaners = []; |
||
50 | |||
51 | /** @var array list of fields, this gives an information if the field |
||
52 | is mandatory or optional. */ |
||
53 | protected $fields = []; |
||
54 | |||
55 | /** @var array list of default values */ |
||
56 | protected $default_values = []; |
||
57 | |||
58 | /** @var array list of form cleaners, must be callables */ |
||
59 | protected $form_cleaners = []; |
||
60 | |||
61 | /** @var array list of form validators, must be callables */ |
||
62 | protected $form_validators = []; |
||
63 | |||
64 | /** @var int strategy of this juicer */ |
||
65 | public $strategy = self::STRATEGY_IGNORE_EXTRA_VALUES; |
||
66 | |||
67 | /** @var int is form validation triggered when field validation fails */ |
||
68 | public $form_validation_strategy = self::FORM_VALIDATORS_CONDITIONAL; |
||
69 | |||
70 | /** |
||
71 | * addField |
||
72 | * |
||
73 | * Declare a new field with no validators nor cleaner. It can be declared |
||
74 | * if the field is optional or mandatory. |
||
75 | * If the field already exists, it is overriden. |
||
76 | */ |
||
77 | public function addField(string $name, bool $is_mandatory = true): self |
||
83 | |||
84 | /** |
||
85 | * addFields |
||
86 | * |
||
87 | * Declare several fields at once.Existing fields are overriden. |
||
88 | */ |
||
89 | public function addFields(array $fields, $are_mandatory = true): self |
||
95 | |||
96 | /** |
||
97 | * removeField |
||
98 | * |
||
99 | * Remove an existing field with all validators or cleaners associated to |
||
100 | * it if any. It throws an exception if the field does not exist. |
||
101 | * |
||
102 | * @throws \InvalidArgumentException |
||
103 | */ |
||
104 | public function removeField(string $name): self |
||
119 | |||
120 | /** |
||
121 | * addValidator |
||
122 | * |
||
123 | * Add a new validator associated to a key. If the field is not already |
||
124 | * declared, it is created. |
||
125 | * |
||
126 | * @throws \InvalidArgumentException |
||
127 | */ |
||
128 | public function addValidator(string $name, callable $validator): self |
||
137 | |||
138 | /** |
||
139 | * addCleaner |
||
140 | * |
||
141 | * Add a new cleaner associated to a key. |
||
142 | * |
||
143 | * @throws \InvalidArgumentException |
||
144 | */ |
||
145 | public function addCleaner(string $name, callable $cleaner): self |
||
154 | |||
155 | /** |
||
156 | * setDefaultValue |
||
157 | * |
||
158 | * Set a default value for a field. If the field is not set or its value is |
||
159 | * null, this value will be set instead. This is triggered AFTER the |
||
160 | * cleaners which is useful because some cleanders can return null and then |
||
161 | * default value is applied. |
||
162 | * |
||
163 | * @throws \InvalidArgumentException |
||
164 | */ |
||
165 | public function setDefaultValue(string $name, $value): self |
||
180 | |||
181 | /** |
||
182 | * addFormCleaner |
||
183 | * |
||
184 | * Add a new cleaner associated to the whole set of values. |
||
185 | */ |
||
186 | public function addFormCleaner(callable $cleaner): self |
||
192 | |||
193 | /** |
||
194 | * addFormValidator |
||
195 | * |
||
196 | * Add a new validator to the whole set of values. |
||
197 | */ |
||
198 | public function addFormValidator(callable $validator): self |
||
204 | |||
205 | /** |
||
206 | * addJuicer |
||
207 | * |
||
208 | * Add a juicer to clean a validate a subset of data. |
||
209 | * |
||
210 | * @throws \InvalidArgumentException |
||
211 | */ |
||
212 | public function addJuicer(string $name, ParameterJuicerInterface $juicer): self |
||
219 | |||
220 | /** |
||
221 | * setStrategy |
||
222 | * |
||
223 | * Set the extra fields strategy for this juicer. |
||
224 | */ |
||
225 | public function setStrategy(int $strategy): self |
||
231 | |||
232 | /** |
||
233 | * setFormValidationStrategy |
||
234 | * |
||
235 | * Set the form validators strategy |
||
236 | */ |
||
237 | public function setFormValidationStrategy(int $strategy): self |
||
243 | |||
244 | /** |
||
245 | * squash |
||
246 | * |
||
247 | * Clean & validate the given data according to the definition. |
||
248 | */ |
||
249 | public function squash(array $values): array |
||
256 | |||
257 | /** |
||
258 | * validate |
||
259 | * |
||
260 | * Trigger validation on values. |
||
261 | * |
||
262 | * @see ParameterJuicerInterface |
||
263 | */ |
||
264 | public function validate(array $values) |
||
281 | |||
282 | /** |
||
283 | * refuseExtraFields |
||
284 | * |
||
285 | * Fill the exception with refused extra fields if any. |
||
286 | */ |
||
287 | private function refuseExtraFields(array $values, ValidationException $exception): self |
||
300 | |||
301 | /** |
||
302 | * validateFields |
||
303 | * |
||
304 | * Check mandatory fields and launch validators. |
||
305 | */ |
||
306 | private function validateFields(array $values, ValidationException $exception): self |
||
327 | |||
328 | /** |
||
329 | * validateForm |
||
330 | * |
||
331 | * form wide validation |
||
332 | */ |
||
333 | private function validateForm(array $values, ValidationException $exception): self |
||
343 | |||
344 | /** |
||
345 | * setDefaultValues |
||
346 | * |
||
347 | * Apply default values. When a field is not present in the values, the |
||
348 | * default value is set. |
||
349 | */ |
||
350 | private function setDefaultValues(array $values): array |
||
360 | |||
361 | /** |
||
362 | * clean |
||
363 | * |
||
364 | * Clean and return values. |
||
365 | * |
||
366 | * @see ParameterJuicerInterface |
||
367 | */ |
||
368 | public function clean(array $values): array |
||
376 | |||
377 | /** |
||
378 | * triggerCleaning |
||
379 | * |
||
380 | * Launch cleaners on the values. |
||
381 | */ |
||
382 | private function triggerCleaning(array $values): array |
||
404 | |||
405 | /** |
||
406 | * checkFieldExists |
||
407 | * |
||
408 | * Throw an exception if the field does not exist. |
||
409 | * |
||
410 | * @throws \InvalidArgumentException |
||
411 | */ |
||
412 | private function checkFieldExists(string $name): self |
||
426 | |||
427 | /** |
||
428 | * launchValidatorsFor |
||
429 | * |
||
430 | * Triger validators for the given field if any. |
||
431 | */ |
||
432 | private function launchValidatorsFor(string $field, $value, ValidationException $exception): self |
||
442 | |||
443 | /** |
||
444 | * launchValidators |
||
445 | * |
||
446 | * Apply validators against the given value. |
||
447 | * |
||
448 | * @throws \RuntimeException if the callable fails. |
||
449 | */ |
||
450 | private function launchValidators(array $validators, $value): self |
||
460 | } |
||
461 |