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 $requirements; |
||
28 | |||
29 | /** |
||
30 | * @var Errors |
||
31 | */ |
||
32 | private $errors; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $skipRemaining; |
||
38 | |||
39 | /** |
||
40 | * Changes settings for the validator. |
||
41 | * |
||
42 | * @param array $config |
||
43 | */ |
||
44 | public static function configure($config) |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | * @var Errors |
||
52 | */ |
||
53 | public function __construct(array $requirements, Errors $errors = null) |
||
65 | |||
66 | /** |
||
67 | * Validates whether an input matches the requirements. |
||
68 | * |
||
69 | * @param array $data |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function validate(array &$data) |
||
101 | |||
102 | /** |
||
103 | * Parses a string into a list of requirements. |
||
104 | * Requirement strings have the form "numeric|range:10,30" where |
||
105 | * '|' separates filters and ':' allows a comma-separated list |
||
106 | * of parameters to be specified. This example would generate |
||
107 | * [['numeric', []], ['range', [10, 30]]]. |
||
108 | * |
||
109 | * @param string $requirements |
||
|
|||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | private function buildRequirementsFromStr($str) |
||
132 | |||
133 | /** |
||
134 | * Skips remaining requirements. |
||
135 | * |
||
136 | * @return self |
||
137 | */ |
||
138 | private function skipRemaining() |
||
144 | |||
145 | //////////////////////////////// |
||
146 | // FILTERS |
||
147 | //////////////////////////////// |
||
148 | |||
149 | /** |
||
150 | * Validates an alpha string. |
||
151 | * OPTIONAL alpha:5 can specify minimum length. |
||
152 | * |
||
153 | * @param mixed $value |
||
154 | * @param array $parameters |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | private function alpha($value, array $parameters) |
||
162 | |||
163 | /** |
||
164 | * Validates an alpha-numeric string |
||
165 | * OPTIONAL alpha_numeric:6 can specify minimum length. |
||
166 | * |
||
167 | * @param mixed $value |
||
168 | * @param array $parameters |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | private function alpha_numeric($value, array $parameters) |
||
176 | |||
177 | /** |
||
178 | * Validates an alpha-numeric string with dashes and underscores |
||
179 | * OPTIONAL alpha_dash:7 can specify minimum length. |
||
180 | * |
||
181 | * @param mixed $value |
||
182 | * @param array $parameters |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | private function alpha_dash($value, array $parameters) |
||
190 | |||
191 | /** |
||
192 | * Validates a boolean value. |
||
193 | * |
||
194 | * @param mixed $value |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | private function boolean(&$value) |
||
204 | |||
205 | /** |
||
206 | * Validates by calling a given function. |
||
207 | * |
||
208 | * @param mixed $value |
||
209 | * @param array $parameters |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | private function custom(&$value, array $parameters) |
||
220 | |||
221 | /** |
||
222 | * Validates an e-mail address. |
||
223 | * |
||
224 | * @param string $email e-mail address |
||
225 | * @param array $parameters parameters for validation |
||
226 | * |
||
227 | * @return bool success |
||
228 | */ |
||
229 | private function email(&$value, array $parameters) |
||
235 | |||
236 | /** |
||
237 | * Validates a value exists in an array. i.e. enum:blue,red,green,yellow. |
||
238 | * |
||
239 | * @param mixed $value |
||
240 | * @param array $parameters |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | private function enum($value, array $parameters) |
||
248 | |||
249 | /** |
||
250 | * Validates a date string. |
||
251 | * |
||
252 | * @param mixed $value |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | private function date($value) |
||
260 | |||
261 | /** |
||
262 | * Validates an IP address. |
||
263 | * |
||
264 | * @param mixed $value |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | private function ip($value) |
||
272 | |||
273 | /** |
||
274 | * Validates that an array of values matches. The array will |
||
275 | * be flattened to a single value if it matches. |
||
276 | * |
||
277 | * @param mixed $value |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | private function matching(&$value) |
||
300 | |||
301 | /** |
||
302 | * Validates a number. |
||
303 | * OPTIONAL numeric:int specifies a type. |
||
304 | * |
||
305 | * @param mixed $value |
||
306 | * @param array $parameters |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | private function numeric($value, array $parameters) |
||
316 | |||
317 | /** |
||
318 | * Validates a password and hashes the value. |
||
319 | * OPTIONAL password:10 sets the minimum length. |
||
320 | * |
||
321 | * @param mixed $value |
||
322 | * @param array $parameters |
||
323 | * |
||
324 | * @return bool |
||
325 | */ |
||
326 | private function password(&$value, array $parameters) |
||
338 | |||
339 | /** |
||
340 | * Validates that a number falls within a range. |
||
341 | * |
||
342 | * @param mixed $value |
||
343 | * @param array $parameters |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | private function range($value, array $parameters) |
||
361 | |||
362 | /** |
||
363 | * Makes sure that a variable is not empty. |
||
364 | * |
||
365 | * @param mixed $value |
||
366 | * |
||
367 | * @return bool |
||
368 | */ |
||
369 | private function required($value) |
||
373 | |||
374 | /** |
||
375 | * Skips any remaining requirements for a field if the |
||
376 | * value is empty. |
||
377 | * |
||
378 | * @param mixed $value |
||
379 | * |
||
380 | * @return bool |
||
381 | */ |
||
382 | public function skip_empty(&$value) |
||
391 | |||
392 | /** |
||
393 | * Validates a string. |
||
394 | * OPTIONAL string:5 supplies a minimum length |
||
395 | * string:1:5 supplies a minimum and maximum length. |
||
396 | * |
||
397 | * @param mixed $value |
||
398 | * @param array $parameters |
||
399 | * |
||
400 | * @return bool |
||
401 | */ |
||
402 | private function string($value, array $parameters) |
||
414 | |||
415 | /** |
||
416 | * Validates a PHP time zone identifier. |
||
417 | * |
||
418 | * @param mixed $value |
||
419 | * |
||
420 | * @return bool |
||
421 | */ |
||
422 | private function time_zone($value) |
||
436 | |||
437 | /** |
||
438 | * Validates a Unix timestamp. If the value is not a timestamp it will be |
||
439 | * converted to one with strtotime(). |
||
440 | * |
||
441 | * @param mixed $value |
||
442 | * |
||
443 | * @return bool |
||
444 | */ |
||
445 | private function timestamp(&$value) |
||
455 | |||
456 | /** |
||
457 | * Converts a Unix timestamp into a format compatible with database |
||
458 | * timestamp types. |
||
459 | * |
||
460 | * @param mixed $value |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | private function db_timestamp(&$value) |
||
474 | |||
475 | /** |
||
476 | * Validates a URL. |
||
477 | * |
||
478 | * @param mixed $value |
||
479 | * |
||
480 | * @return bool |
||
481 | */ |
||
482 | private function url($value) |
||
486 | } |
||
487 |
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.