Complex classes like Validator 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 Validator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Validator |
||
16 | { |
||
17 | /** |
||
18 | * @staticvar array |
||
19 | */ |
||
20 | private static $config = [ |
||
21 | 'salt' => '', |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $rules; |
||
28 | |||
29 | /** |
||
30 | * @var Errors |
||
31 | */ |
||
32 | private $errors; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $skipRemaining; |
||
38 | |||
39 | /** |
||
40 | * These are rules that always run even if a value is not present. |
||
41 | * |
||
42 | * @staticvar array |
||
43 | */ |
||
44 | private static $runsWhenNotPresent = [ |
||
45 | 'required', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * Changes settings for the validator. |
||
50 | * |
||
51 | * @param array $config |
||
52 | */ |
||
53 | public static function configure($config) |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | * @var Errors |
||
61 | */ |
||
62 | public function __construct(array $rules, Errors $errors = null) |
||
74 | |||
75 | /** |
||
76 | * Validates whether an input passes the validator's rules. |
||
77 | * |
||
78 | * @param array $data |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | public function validate(array &$data) |
||
115 | |||
116 | /** |
||
117 | * Parses a string into a list of rules. |
||
118 | * Rule strings have the form "numeric|range:10,30" where |
||
119 | * '|' separates rules and ':' allows a comma-separated list |
||
120 | * of parameters to be specified. This example would generate |
||
121 | * [['numeric', []], ['range', [10, 30]]]. |
||
122 | * |
||
123 | * @param string $rules |
||
|
|||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | private function buildRulesFromStr($str) |
||
146 | |||
147 | /** |
||
148 | * Checks if the rules should be ran when a value is empty or |
||
149 | * not present. |
||
150 | * |
||
151 | * @param array $rules |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | private function runsWhenNotPresent(array $rules) |
||
165 | |||
166 | /** |
||
167 | * Skips remaining rules. |
||
168 | * |
||
169 | * @return self |
||
170 | */ |
||
171 | private function skipRemaining() |
||
177 | |||
178 | //////////////////////////////// |
||
179 | // Rules |
||
180 | //////////////////////////////// |
||
181 | |||
182 | /** |
||
183 | * Validates an alpha string. |
||
184 | * OPTIONAL alpha:5 can specify minimum length. |
||
185 | * |
||
186 | * @param mixed $value |
||
187 | * @param array $parameters |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | private function alpha($value, array $parameters) |
||
195 | |||
196 | /** |
||
197 | * Validates an alpha-numeric string |
||
198 | * OPTIONAL alpha_numeric:6 can specify minimum length. |
||
199 | * |
||
200 | * @param mixed $value |
||
201 | * @param array $parameters |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | private function alpha_numeric($value, array $parameters) |
||
209 | |||
210 | /** |
||
211 | * Validates an alpha-numeric string with dashes and underscores |
||
212 | * OPTIONAL alpha_dash:7 can specify minimum length. |
||
213 | * |
||
214 | * @param mixed $value |
||
215 | * @param array $parameters |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | private function alpha_dash($value, array $parameters) |
||
223 | |||
224 | /** |
||
225 | * Validates a boolean value. |
||
226 | * |
||
227 | * @param mixed $value |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | private function boolean(&$value) |
||
237 | |||
238 | /** |
||
239 | * Validates by calling a given function. |
||
240 | * |
||
241 | * @param mixed $value |
||
242 | * @param array $parameters |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | private function custom(&$value, array $parameters) |
||
253 | |||
254 | /** |
||
255 | * Validates an e-mail address. |
||
256 | * |
||
257 | * @param string $email e-mail address |
||
258 | * @param array $parameters parameters for validation |
||
259 | * |
||
260 | * @return bool success |
||
261 | */ |
||
262 | private function email(&$value, array $parameters) |
||
268 | |||
269 | /** |
||
270 | * Validates a value exists in an array. i.e. enum:blue,red,green,yellow. |
||
271 | * |
||
272 | * @param mixed $value |
||
273 | * @param array $parameters |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | private function enum($value, array $parameters) |
||
281 | |||
282 | /** |
||
283 | * Validates a date string. |
||
284 | * |
||
285 | * @param mixed $value |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | private function date($value) |
||
293 | |||
294 | /** |
||
295 | * Validates an IP address. |
||
296 | * |
||
297 | * @param mixed $value |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | private function ip($value) |
||
305 | |||
306 | /** |
||
307 | * Validates that an array of values matches. The array will |
||
308 | * be flattened to a single value if it matches. |
||
309 | * |
||
310 | * @param mixed $value |
||
311 | * |
||
312 | * @return bool |
||
313 | */ |
||
314 | private function matching(&$value) |
||
333 | |||
334 | /** |
||
335 | * Validates a number. |
||
336 | * OPTIONAL numeric:int specifies a type. |
||
337 | * |
||
338 | * @param mixed $value |
||
339 | * @param array $parameters |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | private function numeric($value, array $parameters) |
||
349 | |||
350 | /** |
||
351 | * Validates a password and hashes the value. |
||
352 | * OPTIONAL password:10 sets the minimum length. |
||
353 | * |
||
354 | * @param mixed $value |
||
355 | * @param array $parameters |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | private function password(&$value, array $parameters) |
||
371 | |||
372 | /** |
||
373 | * Validates that a number falls within a range. |
||
374 | * |
||
375 | * @param mixed $value |
||
376 | * @param array $parameters |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | private function range($value, array $parameters) |
||
394 | |||
395 | /** |
||
396 | * Makes sure that a variable is not empty. |
||
397 | * |
||
398 | * @param mixed $value |
||
399 | * |
||
400 | * @return bool |
||
401 | */ |
||
402 | private function required($value) |
||
414 | |||
415 | /** |
||
416 | * Skips any remaining rules for a field if the |
||
417 | * value is empty. |
||
418 | * |
||
419 | * @param mixed $value |
||
420 | * |
||
421 | * @return bool |
||
422 | */ |
||
423 | public function skip_empty(&$value) |
||
432 | |||
433 | /** |
||
434 | * Validates a string. |
||
435 | * OPTIONAL string:5 supplies a minimum length |
||
436 | * string:1:5 supplies a minimum and maximum length. |
||
437 | * |
||
438 | * @param mixed $value |
||
439 | * @param array $parameters |
||
440 | * |
||
441 | * @return bool |
||
442 | */ |
||
443 | private function string($value, array $parameters) |
||
455 | |||
456 | /** |
||
457 | * Validates a PHP time zone identifier. |
||
458 | * |
||
459 | * @param mixed $value |
||
460 | * |
||
461 | * @return bool |
||
462 | */ |
||
463 | private function time_zone($value) |
||
477 | |||
478 | /** |
||
479 | * Validates a Unix timestamp. If the value is not a timestamp it will be |
||
480 | * converted to one with strtotime(). |
||
481 | * |
||
482 | * @param mixed $value |
||
483 | * |
||
484 | * @return bool |
||
485 | */ |
||
486 | private function timestamp(&$value) |
||
496 | |||
497 | /** |
||
498 | * Converts a Unix timestamp into a format compatible with database |
||
499 | * timestamp types. |
||
500 | * |
||
501 | * @param mixed $value |
||
502 | * |
||
503 | * @return bool |
||
504 | */ |
||
505 | private function db_timestamp(&$value) |
||
515 | |||
516 | /** |
||
517 | * Checks if a value is unique for a model. |
||
518 | * |
||
519 | * @param mixed $value |
||
520 | * @param array $parameters |
||
521 | * @param string $name |
||
522 | * @param Model $model |
||
523 | * |
||
524 | * @return bool |
||
525 | */ |
||
526 | private function unique($value, array $parameters, $name) |
||
537 | |||
538 | /** |
||
539 | * Validates a URL. |
||
540 | * |
||
541 | * @param mixed $value |
||
542 | * |
||
543 | * @return bool |
||
544 | */ |
||
545 | private function url($value) |
||
549 | } |
||
550 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.