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 |
||
169 | |||
170 | /** |
||
171 | * addJuicer |
||
172 | * |
||
173 | * Add a juicer to clean a validate a subset of data. |
||
174 | * |
||
175 | * @throws \InvalidArgumentException |
||
176 | */ |
||
177 | public function addJuicer(string $name, ParameterJuicerInterface $juicer): self |
||
184 | |||
185 | /** |
||
186 | * setStrategy |
||
187 | * |
||
188 | * Set the extra fields strategy for this juicer. |
||
189 | */ |
||
190 | public function setStrategy(int $strategy): self |
||
196 | |||
197 | /** |
||
198 | * squash |
||
199 | * |
||
200 | * Clean & validate the given data according to the definition. |
||
201 | */ |
||
202 | public function squash(array $values): array |
||
209 | |||
210 | /** |
||
211 | * validate |
||
212 | * |
||
213 | * Trigger validation on values. |
||
214 | * |
||
215 | * @see ParameterJuicerInterface |
||
216 | */ |
||
217 | public function validate(array $values): ParameterJuicerInterface |
||
232 | |||
233 | /** |
||
234 | * refuseExtraFields |
||
235 | * |
||
236 | * Fill the exception with refused extra fields if any. |
||
237 | */ |
||
238 | private function refuseExtraFields(array $values, ValidationException $exception): self |
||
251 | |||
252 | /** |
||
253 | * validateFields |
||
254 | * |
||
255 | * Check mandatory fields and launch validators. |
||
256 | */ |
||
257 | private function validateFields(array $values, ValidationException $exception): self |
||
278 | |||
279 | /** |
||
280 | * setDefaultValues |
||
281 | * |
||
282 | * Apply default values. When a field is not present in the values, the |
||
283 | * default value is set. |
||
284 | */ |
||
285 | private function setDefaultValues(array $values): array |
||
295 | |||
296 | /** |
||
297 | * clean |
||
298 | * |
||
299 | * Clean and return values. |
||
300 | * |
||
301 | * @see ParameterJuicerInterface |
||
302 | */ |
||
303 | public function clean(array $values): array |
||
311 | |||
312 | /** |
||
313 | * triggerCleaning |
||
314 | * |
||
315 | * Launch cleaners on the values. |
||
316 | */ |
||
317 | private function triggerCleaning(array $values): array |
||
335 | |||
336 | /** |
||
337 | * checkFieldExists |
||
338 | * |
||
339 | * Throw an exception if the field does not exist. |
||
340 | * |
||
341 | * @throws \InvalidArgumentException |
||
342 | */ |
||
343 | private function checkFieldExists(string $name): self |
||
357 | |||
358 | /** |
||
359 | * launchValidatorsFor |
||
360 | * |
||
361 | * Triger validators for the given field if any. |
||
362 | * |
||
363 | * @throws \RuntimeException if the callable fails. |
||
364 | */ |
||
365 | private function launchValidatorsFor(string $field, $value, ValidationException $exception): self |
||
381 | } |
||
382 |