Complex classes like Chain 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 Chain, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Chain |
||
21 | { |
||
22 | /** |
||
23 | * The key we want to validate. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $key; |
||
28 | |||
29 | /** |
||
30 | * The name that we can use in error messages. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $name; |
||
35 | |||
36 | /** |
||
37 | * The array of all rules for this chain. |
||
38 | * |
||
39 | * @var Rule[] |
||
40 | */ |
||
41 | protected $rules = []; |
||
42 | |||
43 | /** |
||
44 | * The message stack to append messages to. |
||
45 | * |
||
46 | * @var MessageStack |
||
47 | */ |
||
48 | protected $messageStack; |
||
49 | |||
50 | /** |
||
51 | * Construct the chain. |
||
52 | * |
||
53 | * @param string $key |
||
54 | * @param string $name |
||
55 | * @param bool $required |
||
56 | * @param bool $allowEmpty |
||
57 | */ |
||
58 | 219 | public function __construct($key, $name, $required, $allowEmpty) |
|
59 | { |
||
60 | 219 | $this->key = $key; |
|
61 | 219 | $this->name = $name; |
|
62 | |||
63 | 219 | $this->addRule(new Rule\Required($required)); |
|
64 | 219 | $this->addRule(new Rule\NotEmpty($allowEmpty)); |
|
65 | 219 | } |
|
66 | |||
67 | /** |
||
68 | * Overwrite the default __clone behaviour to make sure the rules are cloned too. |
||
69 | */ |
||
70 | 3 | public function __clone() |
|
71 | { |
||
72 | 3 | $rules = []; |
|
73 | 3 | foreach ($this->rules as $rule) { |
|
74 | 3 | $rules[] = clone $rule; |
|
75 | 3 | } |
|
76 | 3 | $this->rules = $rules; |
|
77 | 3 | } |
|
78 | |||
79 | /** |
||
80 | * Validate the value to consist only out of alphanumeric characters. |
||
81 | * |
||
82 | * @param bool $allowWhitespace |
||
83 | * @return $this |
||
84 | */ |
||
85 | 7 | public function alnum($allowWhitespace = false) |
|
89 | |||
90 | /** |
||
91 | * Validate that the value only consists our of alphabetic characters. |
||
92 | * |
||
93 | * @param bool $allowWhitespace |
||
94 | * @return $this |
||
95 | */ |
||
96 | 7 | public function alpha($allowWhitespace = false) |
|
100 | |||
101 | /** |
||
102 | * Validate that the value is between $min and $max (inclusive). |
||
103 | * |
||
104 | * @param int $min |
||
105 | * @param int $max |
||
106 | * @return $this |
||
107 | */ |
||
108 | 6 | public function between($min, $max) |
|
109 | { |
||
110 | 6 | return $this->addRule(new Rule\Between($min, $max)); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Validate that the value is a boolean. |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | 9 | public function bool() |
|
122 | |||
123 | /** |
||
124 | * Validate by executing a callback function, and returning its result. |
||
125 | * |
||
126 | * @param callable $callable |
||
127 | * @return $this |
||
128 | */ |
||
129 | 5 | public function callback(callable $callable) |
|
133 | |||
134 | /** |
||
135 | * Validates that the value is a valid credit card number. |
||
136 | * @return $this |
||
137 | */ |
||
138 | 15 | public function creditCard() |
|
142 | |||
143 | /** |
||
144 | * Validates that the value is a date. If format is passed, it *must* be in that format. |
||
145 | * |
||
146 | * @param string|null $format |
||
147 | * @return $this |
||
148 | */ |
||
149 | 8 | public function datetime($format = null) |
|
153 | |||
154 | /** |
||
155 | * Validates that all characters of the value are decimal digits. |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | 4 | public function digits() |
|
163 | |||
164 | /** |
||
165 | * Validates a value to be a nested array, which can then be validated using a new Validator instance. |
||
166 | * |
||
167 | * @param callable $callback |
||
168 | * @return $this |
||
169 | */ |
||
170 | 5 | public function each(callable $callback) |
|
174 | |||
175 | /** |
||
176 | * Validates that the value is a valid email address (format only). |
||
177 | * @return $this |
||
178 | */ |
||
179 | 6 | public function email() |
|
183 | |||
184 | /** |
||
185 | * Validates that the value is equal to $value. |
||
186 | * |
||
187 | * @param string $value |
||
188 | * @return $this |
||
189 | */ |
||
190 | 2 | public function equals($value) |
|
194 | |||
195 | /** |
||
196 | * Validates that the value represents a float. |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | 6 | public function float() |
|
204 | |||
205 | /** |
||
206 | * Validates that the value is greater than $value. |
||
207 | * |
||
208 | * @param int $value |
||
209 | * @return $this |
||
210 | */ |
||
211 | 3 | public function greaterThan($value) |
|
215 | |||
216 | /** |
||
217 | * Validates that the value is in the array with optional "loose" checking. |
||
218 | * |
||
219 | * @param string $hashAlgorithm |
||
220 | * @param bool $allowUppercase |
||
221 | * @return $this |
||
222 | * @see \Particle\Validator\Rule\Hash |
||
223 | */ |
||
224 | 12 | public function hash($hashAlgorithm, $allowUppercase = false) |
|
225 | { |
||
226 | 12 | return $this->addRule(new Rule\Hash($hashAlgorithm, $allowUppercase)); |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Validates that the value is in the array with optional "loose" checking. |
||
231 | * |
||
232 | * @param array $array |
||
233 | * @param bool $strict |
||
234 | * @return $this |
||
235 | */ |
||
236 | 4 | public function inArray(array $array, $strict = true) |
|
240 | |||
241 | /** |
||
242 | * Validates the value represents a valid integer |
||
243 | * |
||
244 | * @param bool $strict |
||
245 | * @return $this |
||
246 | * @see \Particle\Validator\Rule\Integer |
||
247 | */ |
||
248 | 18 | public function integer($strict = false) |
|
252 | |||
253 | /** |
||
254 | * Validates the value is an array |
||
255 | * |
||
256 | * @return $this |
||
257 | * @see \Particle\Validator\Rule\IsArray |
||
258 | */ |
||
259 | 7 | public function isArray() |
|
263 | |||
264 | /** |
||
265 | * Validates that the value represents a valid JSON string |
||
266 | * |
||
267 | * @return $this |
||
268 | * @see \Particle\Validator\Rule\Json |
||
269 | */ |
||
270 | 11 | public function json() |
|
274 | |||
275 | /** |
||
276 | * Validate the value to be of precisely length $length. |
||
277 | * |
||
278 | * @param int $length |
||
279 | * @return $this |
||
280 | */ |
||
281 | 13 | public function length($length) |
|
285 | |||
286 | /** |
||
287 | * Validates that the length of the value is between $min and $max. |
||
288 | * |
||
289 | * If $max is null, it has no upper limit. The default is inclusive. |
||
290 | * |
||
291 | * @param int $min |
||
292 | * @param int|null $max |
||
293 | * @return $this |
||
294 | */ |
||
295 | 7 | public function lengthBetween($min, $max) |
|
299 | |||
300 | /** |
||
301 | * Validates that the value is less than $value. |
||
302 | * |
||
303 | * @param int $value |
||
304 | * @return $this |
||
305 | */ |
||
306 | 3 | public function lessThan($value) |
|
310 | |||
311 | /** |
||
312 | * Mount a rule object onto this chain. |
||
313 | * |
||
314 | * @param Rule $rule |
||
315 | * @return $this |
||
316 | */ |
||
317 | 1 | public function mount(Rule $rule) |
|
321 | |||
322 | /** |
||
323 | * Validates that the value is either a integer or a float. |
||
324 | * |
||
325 | * @return $this |
||
326 | */ |
||
327 | 11 | public function numeric() |
|
331 | |||
332 | /** |
||
333 | * Validates that the value is a valid phone number for $countryCode. |
||
334 | * |
||
335 | * @param string $countryCode |
||
336 | * @see \Particle\Validator\Rule\Phone |
||
337 | * @return $this |
||
338 | */ |
||
339 | 13 | public function phone($countryCode) |
|
343 | |||
344 | /** |
||
345 | * Validates that the value matches the regular expression $regex. |
||
346 | * |
||
347 | * @param string $regex |
||
348 | * @return $this |
||
349 | */ |
||
350 | 2 | public function regex($regex) |
|
354 | |||
355 | /** |
||
356 | * Validates that the value represents a string. |
||
357 | * |
||
358 | * @return $this |
||
359 | */ |
||
360 | 5 | public function string() |
|
364 | |||
365 | /** |
||
366 | * Validates that the value is a valid URL. The schemes array is to selectively whitelist URL schemes. |
||
367 | * |
||
368 | * @param array $schemes |
||
369 | * @return $this |
||
370 | */ |
||
371 | 7 | public function url(array $schemes = []) |
|
375 | |||
376 | /** |
||
377 | * Validates that the value is a valid UUID |
||
378 | * |
||
379 | * @param int $version |
||
380 | * @return $this |
||
381 | */ |
||
382 | 12 | public function uuid($version = Rule\Uuid::UUID_V4) |
|
386 | |||
387 | /** |
||
388 | * Set a callable or boolean value which may be used to alter the required requirement on validation time. |
||
389 | * |
||
390 | * This may be incredibly helpful when doing conditional validation. |
||
391 | * |
||
392 | * @param callable|bool $required |
||
393 | * @return $this |
||
394 | */ |
||
395 | 5 | public function required($required) |
|
396 | { |
||
397 | 5 | $this->getRequiredRule()->setRequired($required); |
|
398 | 5 | return $this; |
|
399 | } |
||
400 | |||
401 | /** |
||
402 | * Set a callable or boolean value which may be used to alter the allow empty requirement on validation time. |
||
403 | * |
||
404 | * This may be incredibly helpful when doing conditional validation. |
||
405 | * |
||
406 | * @param callable|bool $allowEmpty |
||
407 | * @return $this |
||
408 | */ |
||
409 | 11 | public function allowEmpty($allowEmpty) |
|
410 | { |
||
411 | 11 | $this->getNotEmptyRule()->setAllowEmpty($allowEmpty); |
|
412 | 11 | return $this; |
|
413 | } |
||
414 | |||
415 | /** |
||
416 | * Attach a representation of this Chain to the Output\Structure $structure. |
||
417 | * |
||
418 | * @internal |
||
419 | * @param Structure $structure |
||
420 | * @param MessageStack $messageStack |
||
421 | * @return Structure |
||
422 | */ |
||
423 | 2 | public function output(Structure $structure, MessageStack $messageStack) |
|
435 | |||
436 | /** |
||
437 | * Validates the values in the $values array and appends messages to $messageStack. Returns the result as a bool. |
||
438 | * |
||
439 | * @param MessageStack $messageStack |
||
440 | * @param Container $input |
||
441 | * @param Container $output |
||
442 | * @return bool |
||
443 | */ |
||
444 | 216 | public function validate(MessageStack $messageStack, Container $input, Container $output) |
|
463 | |||
464 | /** |
||
465 | * Shortcut method for storing a rule on this chain, and returning the chain. |
||
466 | * |
||
467 | * @param Rule $rule |
||
468 | * @return $this |
||
469 | */ |
||
470 | 219 | protected function addRule(Rule $rule) |
|
476 | |||
477 | /** |
||
478 | * Returns the first rule, which is always the required rule. |
||
479 | * |
||
480 | * @return Rule\Required |
||
481 | */ |
||
482 | 5 | protected function getRequiredRule() |
|
486 | |||
487 | /** |
||
488 | * Returns the second rule, which is always the allow empty rule. |
||
489 | * |
||
490 | * @return Rule\NotEmpty |
||
491 | */ |
||
492 | 11 | protected function getNotEmptyRule() |
|
496 | } |
||
497 |