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 | 252 | public function __construct($key, $name, $required, $allowEmpty) |
|
66 | |||
67 | /** |
||
68 | * Overwrite the default __clone behaviour to make sure the rules are cloned too. |
||
69 | */ |
||
70 | 3 | public function __clone() |
|
78 | |||
79 | /** |
||
80 | * Set a callable or boolean value which may be used to alter the allow empty requirement on validation time. |
||
81 | * |
||
82 | * This may be incredibly helpful when doing conditional validation. |
||
83 | * |
||
84 | * @param callable|bool $allowEmpty |
||
85 | * @return $this |
||
86 | */ |
||
87 | 11 | public function allowEmpty($allowEmpty) |
|
92 | |||
93 | /** |
||
94 | * Validate the value to consist only out of alphanumeric characters. |
||
95 | * |
||
96 | * @param bool $allowWhitespace |
||
97 | * @return $this |
||
98 | */ |
||
99 | 7 | public function alnum($allowWhitespace = Rule\Alnum::DISALLOW_SPACES) |
|
103 | |||
104 | /** |
||
105 | * Validate that the value only consists our of alphabetic characters. |
||
106 | * |
||
107 | * @param bool $allowWhitespace |
||
108 | * @return $this |
||
109 | */ |
||
110 | 7 | public function alpha($allowWhitespace = Rule\Alpha::DISALLOW_SPACES) |
|
114 | |||
115 | /** |
||
116 | * Validate that the value is between $min and $max (inclusive). |
||
117 | * |
||
118 | * @param int $min |
||
119 | * @param int $max |
||
120 | * @return $this |
||
121 | */ |
||
122 | 6 | public function between($min, $max) |
|
126 | |||
127 | /** |
||
128 | * Validate that the value is a boolean. |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | 9 | public function bool() |
|
136 | |||
137 | /** |
||
138 | * Validate by executing a callback function, and returning its result. |
||
139 | * |
||
140 | * @param callable $callable |
||
141 | * @return $this |
||
142 | */ |
||
143 | 5 | public function callback(callable $callable) |
|
147 | |||
148 | /** |
||
149 | * Validates that the value is a valid credit card number. |
||
150 | * @return $this |
||
151 | */ |
||
152 | 15 | public function creditCard() |
|
156 | |||
157 | /** |
||
158 | * Validates that the value is a date. If format is passed, it *must* be in that format. |
||
159 | * |
||
160 | * @param string|null $format |
||
161 | * @return $this |
||
162 | */ |
||
163 | 9 | public function datetime($format = null) |
|
167 | |||
168 | /** |
||
169 | * Validates that all characters of the value are decimal digits. |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | 4 | public function digits() |
|
177 | |||
178 | /** |
||
179 | * Validates a value to be nested repeating arrays, which can then be validated using a new Validator instance. |
||
180 | * |
||
181 | * @param callable $callback |
||
182 | * @return $this |
||
183 | */ |
||
184 | 5 | public function each(callable $callback) |
|
188 | |||
189 | /** |
||
190 | * Validates that the value is a valid email address (format only). |
||
191 | * @return $this |
||
192 | */ |
||
193 | 7 | public function email() |
|
197 | |||
198 | /** |
||
199 | * Validates that the value is equal to $value. |
||
200 | * |
||
201 | * @param string $value |
||
202 | * @return $this |
||
203 | */ |
||
204 | 2 | public function equals($value) |
|
208 | |||
209 | /** |
||
210 | * Validates that the value represents a float. |
||
211 | * |
||
212 | * @return $this |
||
213 | */ |
||
214 | 7 | public function float() |
|
218 | |||
219 | /** |
||
220 | * Validates that the value is greater than $value. |
||
221 | * |
||
222 | * @param int $value |
||
223 | * @return $this |
||
224 | */ |
||
225 | 3 | public function greaterThan($value) |
|
229 | |||
230 | /** |
||
231 | * Validates that the value is in the array with optional "loose" checking. |
||
232 | * |
||
233 | * @param string $hashAlgorithm |
||
234 | * @param bool $allowUppercase |
||
235 | * @return $this |
||
236 | * @see \Particle\Validator\Rule\Hash |
||
237 | */ |
||
238 | 12 | public function hash($hashAlgorithm, $allowUppercase = Rule\Hash::DISALLOW_UPPERCASE) |
|
242 | |||
243 | /** |
||
244 | * Validates that the value is in the array with optional "loose" checking. |
||
245 | * |
||
246 | * @param array $array |
||
247 | * @param bool $strict |
||
248 | * @return $this |
||
249 | */ |
||
250 | 4 | public function inArray(array $array, $strict = Rule\InArray::STRICT) |
|
254 | |||
255 | /** |
||
256 | * Validates a value to be a nested array, which can then be validated using a new Validator instance. |
||
257 | * |
||
258 | * @param callable $callback |
||
259 | * @return Chain |
||
260 | */ |
||
261 | 2 | public function inner(callable $callback) |
|
265 | |||
266 | /** |
||
267 | * Validates the value represents a valid integer |
||
268 | * |
||
269 | * @param bool $strict |
||
270 | * @return $this |
||
271 | * @see \Particle\Validator\Rule\Integer |
||
272 | */ |
||
273 | 19 | public function integer($strict = false) |
|
277 | |||
278 | /** |
||
279 | * Validates the value is an array |
||
280 | * |
||
281 | * @return $this |
||
282 | * @see \Particle\Validator\Rule\IsArray |
||
283 | */ |
||
284 | 7 | public function isArray() |
|
288 | |||
289 | /** |
||
290 | * Validates that the value represents a valid JSON string |
||
291 | * |
||
292 | * @return $this |
||
293 | * @see \Particle\Validator\Rule\Json |
||
294 | */ |
||
295 | 11 | public function json() |
|
299 | |||
300 | /** |
||
301 | * Validate the value to be of precisely length $length. |
||
302 | * |
||
303 | * @param int $length |
||
304 | * @return $this |
||
305 | */ |
||
306 | 13 | public function length($length) |
|
310 | |||
311 | /** |
||
312 | * Validates that the length of the value is between $min and $max. |
||
313 | * |
||
314 | * If $max is null, it has no upper limit. The default is inclusive. |
||
315 | * |
||
316 | * @param int $min |
||
317 | * @param int|null $max |
||
318 | * @return $this |
||
319 | */ |
||
320 | 7 | public function lengthBetween($min, $max) |
|
324 | |||
325 | /** |
||
326 | * Validates that the value is less than $value. |
||
327 | * |
||
328 | * @param int $value |
||
329 | * @return $this |
||
330 | */ |
||
331 | 3 | public function lessThan($value) |
|
335 | |||
336 | /** |
||
337 | * Mount a rule object onto this chain. |
||
338 | * |
||
339 | * @param Rule $rule |
||
340 | * @return $this |
||
341 | */ |
||
342 | 1 | public function mount(Rule $rule) |
|
346 | |||
347 | /** |
||
348 | * Validates that the value is either a integer or a float. |
||
349 | * |
||
350 | * @return $this |
||
351 | */ |
||
352 | 11 | public function numeric() |
|
356 | |||
357 | /** |
||
358 | * Validates that the value is a valid phone number for $countryCode. |
||
359 | * |
||
360 | * @param string $countryCode |
||
361 | * @see \Particle\Validator\Rule\Phone |
||
362 | * @return $this |
||
363 | */ |
||
364 | 13 | public function phone($countryCode) |
|
368 | |||
369 | /** |
||
370 | * Validates that the value matches the regular expression $regex. |
||
371 | * |
||
372 | * @param string $regex |
||
373 | * @return $this |
||
374 | */ |
||
375 | 2 | public function regex($regex) |
|
379 | |||
380 | /** |
||
381 | * Set a callable or boolean value which may be used to alter the required requirement on validation time. |
||
382 | * |
||
383 | * This may be incredibly helpful when doing conditional validation. |
||
384 | * |
||
385 | * @param callable|bool $required |
||
386 | * @return $this |
||
387 | */ |
||
388 | 5 | public function required($required) |
|
393 | |||
394 | /** |
||
395 | * Validates that the value represents a string. |
||
396 | * |
||
397 | * @return $this |
||
398 | */ |
||
399 | 6 | public function string() |
|
403 | |||
404 | /** |
||
405 | * Validates that the value is a valid URL. The schemes array is to selectively whitelist URL schemes. |
||
406 | * |
||
407 | * @param array $schemes |
||
408 | * @return $this |
||
409 | */ |
||
410 | 7 | public function url(array $schemes = []) |
|
414 | |||
415 | /** |
||
416 | * Validates that the value is a valid UUID |
||
417 | * |
||
418 | * @param int $version |
||
419 | * @return $this |
||
420 | */ |
||
421 | 42 | public function uuid($version = Rule\Uuid::UUID_VALID) |
|
425 | |||
426 | /** |
||
427 | * Attach a representation of this Chain to the Output\Structure $structure. |
||
428 | * |
||
429 | * @internal |
||
430 | * @param Structure $structure |
||
431 | * @param MessageStack $messageStack |
||
432 | * @return Structure |
||
433 | */ |
||
434 | 2 | public function output(Structure $structure, MessageStack $messageStack) |
|
446 | |||
447 | /** |
||
448 | * Validates the values in the $values array and appends messages to $messageStack. Returns the result as a bool. |
||
449 | * |
||
450 | * @param MessageStack $messageStack |
||
451 | * @param Container $input |
||
452 | * @param Container $output |
||
453 | * @return bool |
||
454 | */ |
||
455 | 247 | public function validate(MessageStack $messageStack, Container $input, Container $output) |
|
474 | |||
475 | /** |
||
476 | * Shortcut method for storing a rule on this chain, and returning the chain. |
||
477 | * |
||
478 | * @param Rule $rule |
||
479 | * @return $this |
||
480 | */ |
||
481 | 252 | protected function addRule(Rule $rule) |
|
487 | |||
488 | /** |
||
489 | * Returns the first rule, which is always the required rule. |
||
490 | * |
||
491 | * @return Rule\Required |
||
492 | */ |
||
493 | 5 | protected function getRequiredRule() |
|
497 | |||
498 | /** |
||
499 | * Returns the second rule, which is always the allow empty rule. |
||
500 | * |
||
501 | * @return Rule\NotEmpty |
||
502 | */ |
||
503 | 11 | protected function getNotEmptyRule() |
|
507 | } |
||
508 |