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 |
||
42 | class Validator implements ValidatableInterface |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * Contains a list of fields to be validated |
||
47 | * |
||
48 | * @var FieldInterface[] |
||
49 | */ |
||
50 | protected $fields = array(); |
||
51 | |||
52 | /** |
||
53 | * Contains a list of any custom validation rules |
||
54 | * |
||
55 | * @var string[] |
||
56 | */ |
||
57 | protected $customRules = array(); |
||
58 | |||
59 | /** |
||
60 | * @var string[] |
||
61 | */ |
||
62 | protected $messages = array(); |
||
63 | |||
64 | /** |
||
65 | * Keeps track of the last field added for magic method chaining |
||
66 | * |
||
67 | * @var FieldInterface |
||
68 | */ |
||
69 | protected $lastAddedField; |
||
70 | |||
71 | /** |
||
72 | * Keeps track of the last rule added for message setting |
||
73 | * |
||
74 | * @var RuleInterface |
||
75 | */ |
||
76 | protected $lastAddedRule; |
||
77 | |||
78 | /** |
||
79 | * Default namespace to look for rules in when a rule is not known |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $ruleNamespace = 'Fuel\Validation\Rule\\'; |
||
84 | |||
85 | /** |
||
86 | * Adds a rule that can be used to validate a field |
||
87 | * |
||
88 | * @param string|FieldInterface $field |
||
89 | * @param RuleInterface $rule |
||
90 | * |
||
91 | * @return $this |
||
92 | * |
||
93 | * @since 2.0 |
||
94 | */ |
||
95 | 17 | public function addRule($field, RuleInterface $rule) |
|
118 | |||
119 | /** |
||
120 | * Adds a new field to the validation object |
||
121 | * |
||
122 | * @param string|FieldInterface $field |
||
123 | * @param string $label Field name to use in messages, set to null to use $field |
||
124 | * |
||
125 | * @return $this |
||
126 | * |
||
127 | * @throws InvalidArgumentException |
||
128 | * |
||
129 | * @since 2.0 |
||
130 | */ |
||
131 | 20 | public function addField($field, $label = null) |
|
148 | |||
149 | /** |
||
150 | * Returns the given field |
||
151 | * |
||
152 | * @param $name |
||
153 | * |
||
154 | * @return FieldInterface |
||
155 | * |
||
156 | * @throws InvalidFieldException |
||
157 | * |
||
158 | * @since 2.0 |
||
159 | */ |
||
160 | 21 | public function getField($name) |
|
169 | |||
170 | /** |
||
171 | * Takes an array of data and validates that against the assigned rules. |
||
172 | * The array is expected to have keys named after fields. |
||
173 | * This function will call reset() before it runs. |
||
174 | * |
||
175 | * @param array $data |
||
176 | * @param ResultInterface $result |
||
177 | * |
||
178 | * @return ResultInterface |
||
179 | * |
||
180 | * @since 2.0 |
||
181 | */ |
||
182 | 12 | public function run($data, ResultInterface $result = null) |
|
204 | |||
205 | /** |
||
206 | * Takes a field name and an array of data and validates the field against the assigned rules. |
||
207 | * The array is expected to have keys named after fields. |
||
208 | * This function will call reset() before it runs. |
||
209 | * |
||
210 | * @param string $field |
||
211 | * @param array $data |
||
212 | * @param ResultInterface $result |
||
213 | * |
||
214 | * @return ResultInterface |
||
215 | * |
||
216 | * @since 2.0 |
||
217 | */ |
||
218 | 2 | public function runField($field, array $data, ResultInterface $result = null) |
|
237 | |||
238 | /** |
||
239 | * Validates a single field |
||
240 | * |
||
241 | * @param string $field |
||
242 | * @param mixed[] $data |
||
243 | * @param ResultInterface $result |
||
244 | * |
||
245 | * @return bool |
||
246 | * |
||
247 | * @since 2.0 |
||
248 | */ |
||
249 | 13 | protected function validateField($field, $data, ResultInterface $result) |
|
286 | |||
287 | /** |
||
288 | * Gets a Rule's message and processes that with various tokens |
||
289 | * |
||
290 | * @param FieldInterface $field |
||
291 | * @param RuleInterface $rule |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | 6 | protected function buildMessage(FieldInterface $field, RuleInterface $rule, $value) |
|
307 | |||
308 | /** |
||
309 | * Replaces any {} tokens with the matching value from $tokens. |
||
310 | * |
||
311 | * @param array $tokens Associative array of token names and values |
||
312 | * @param string $message |
||
313 | * |
||
314 | * @return string |
||
315 | * |
||
316 | * @since 2.0 |
||
317 | */ |
||
318 | 6 | protected function processMessageTokens(array $tokens, $message) |
|
327 | |||
328 | /** |
||
329 | * @param string $fieldName |
||
330 | * |
||
331 | * @return RuleInterface[] |
||
332 | */ |
||
333 | 16 | public function getFieldRules($fieldName) |
|
347 | |||
348 | /** |
||
349 | * Allows validation rules to be dynamically added using method chaining. |
||
350 | * |
||
351 | * @param string $name |
||
352 | * @param array $arguments |
||
353 | * |
||
354 | * @return $this |
||
355 | * @throws InvalidRuleException |
||
356 | * |
||
357 | * @since 2.0 |
||
358 | */ |
||
359 | 7 | public function __call($name, $arguments) |
|
368 | |||
369 | /** |
||
370 | * Sets the failure message for the last added rule |
||
371 | * |
||
372 | * @param string $message |
||
373 | * |
||
374 | * @return $this |
||
375 | * |
||
376 | * @throws LogicException |
||
377 | * |
||
378 | * @since 2.0 |
||
379 | */ |
||
380 | 3 | public function setMessage($message) |
|
391 | |||
392 | /** |
||
393 | * Creates an instance of the given rule name |
||
394 | * |
||
395 | * @param string $name |
||
396 | * @param mixed $parameters |
||
397 | * |
||
398 | * @return RuleInterface |
||
399 | * |
||
400 | * @throws InvalidRuleException |
||
401 | * |
||
402 | * @since 2.0 |
||
403 | */ |
||
404 | 11 | public function createRuleInstance($name, $parameters = []) |
|
427 | |||
428 | /** |
||
429 | * Returns the full class name for the given validation rule |
||
430 | * |
||
431 | * @param string $name |
||
432 | * |
||
433 | * @return string |
||
434 | * |
||
435 | * @since 2.0 |
||
436 | */ |
||
437 | 11 | protected function getRuleClassName($name) |
|
448 | |||
449 | /** |
||
450 | * Adds custom validation rules and allows for core rules to be overridden. |
||
451 | * When wanting to override a core rule just specify the rule name as $name. |
||
452 | * Eg, 'required', 'minLength'. Note the lowercase first letter. |
||
453 | * |
||
454 | * The name of the rule should not contain any whitespace or special characters as the name will be available |
||
455 | * to use as a function name in the method chaining syntax. |
||
456 | * |
||
457 | * @param string $name |
||
458 | * @param string $class |
||
459 | * |
||
460 | * @return $this |
||
461 | * |
||
462 | * @since 2.0 |
||
463 | */ |
||
464 | 3 | public function addCustomRule($name, $class) |
|
470 | |||
471 | /** |
||
472 | * Sets a custom message for all fields of the given type that are created after the message has been set. |
||
473 | * |
||
474 | * @param string $ruleName Name of the rule to set a message for, eg, required, number, exactLength |
||
475 | * @param string|null $message Set to null to disable the custom message |
||
476 | * |
||
477 | * @return $this |
||
478 | * |
||
479 | * @since 2.0 |
||
480 | */ |
||
481 | 2 | public function setGlobalMessage($ruleName, $message) |
|
492 | |||
493 | /** |
||
494 | * Sets custom messages for one or more rules. Setting the value to "null" will remove the message |
||
495 | * |
||
496 | * @param string[] $messages |
||
497 | * |
||
498 | * @return $this |
||
499 | * |
||
500 | * @since 2.0 |
||
501 | */ |
||
502 | public function setGlobalMessages($messages) |
||
511 | |||
512 | /** |
||
513 | * Removes a global rule message |
||
514 | * |
||
515 | * @param string $ruleName |
||
516 | * |
||
517 | * @return $this |
||
518 | * |
||
519 | * @since 2.0 |
||
520 | */ |
||
521 | 1 | public function removeGlobalMessage($ruleName) |
|
527 | |||
528 | /** |
||
529 | * Gets the global message set for a rule |
||
530 | * |
||
531 | * @param string $ruleName |
||
532 | * |
||
533 | * @return null|string Will be null if there is no message |
||
534 | */ |
||
535 | 10 | public function getGlobalMessage($ruleName) |
|
544 | |||
545 | } |
||
546 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: