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 | 19 | 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 | 22 | 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 | 23 | 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 | 14 | 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 | 15 | protected function validateField($field, $data, ResultInterface $result) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Gets a Rule's message and processes that with various tokens |
||
| 296 | * |
||
| 297 | * @param FieldInterface $field |
||
| 298 | * @param RuleInterface $rule |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | 7 | protected function buildMessage(FieldInterface $field, RuleInterface $rule, $value) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Replaces any {} tokens with the matching value from $tokens. |
||
| 316 | * |
||
| 317 | * @param array $tokens Associative array of token names and values |
||
| 318 | * @param string $message |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | * |
||
| 322 | * @since 2.0 |
||
| 323 | */ |
||
| 324 | 7 | protected function processMessageTokens(array $tokens, $message) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $fieldName |
||
| 336 | * |
||
| 337 | * @return RuleInterface[] |
||
| 338 | */ |
||
| 339 | 18 | public function getFieldRules($fieldName) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Allows validation rules to be dynamically added using method chaining. |
||
| 356 | * |
||
| 357 | * @param string $name |
||
| 358 | * @param array $arguments |
||
| 359 | * |
||
| 360 | * @return $this |
||
| 361 | * @throws InvalidRuleException |
||
| 362 | * |
||
| 363 | * @since 2.0 |
||
| 364 | */ |
||
| 365 | 9 | public function __call($name, $arguments) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Sets the failure message for the last added rule |
||
| 377 | * |
||
| 378 | * @param string $message |
||
| 379 | * |
||
| 380 | * @return $this |
||
| 381 | * |
||
| 382 | * @throws LogicException |
||
| 383 | * |
||
| 384 | * @since 2.0 |
||
| 385 | */ |
||
| 386 | 3 | public function setMessage($message) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Creates an instance of the given rule name |
||
| 400 | * |
||
| 401 | * @param string $name |
||
| 402 | * @param mixed $parameters |
||
| 403 | * |
||
| 404 | * @return RuleInterface |
||
| 405 | * |
||
| 406 | * @throws InvalidRuleException |
||
| 407 | * |
||
| 408 | * @since 2.0 |
||
| 409 | */ |
||
| 410 | 13 | public function createRuleInstance($name, $parameters = []) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Returns the full class name for the given validation rule |
||
| 436 | * |
||
| 437 | * @param string $name |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | * |
||
| 441 | * @since 2.0 |
||
| 442 | */ |
||
| 443 | 13 | protected function getRuleClassName($name) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Adds custom validation rules and allows for core rules to be overridden. |
||
| 457 | * When wanting to override a core rule just specify the rule name as $name. |
||
| 458 | * Eg, 'required', 'minLength'. Note the lowercase first letter. |
||
| 459 | * |
||
| 460 | * The name of the rule should not contain any whitespace or special characters as the name will be available |
||
| 461 | * to use as a function name in the method chaining syntax. |
||
| 462 | * |
||
| 463 | * @param string $name |
||
| 464 | * @param string $class |
||
| 465 | * |
||
| 466 | * @return $this |
||
| 467 | * |
||
| 468 | * @since 2.0 |
||
| 469 | */ |
||
| 470 | 3 | public function addCustomRule($name, $class) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Sets a custom message for all fields of the given type that are created after the message has been set. |
||
| 479 | * |
||
| 480 | * @param string $ruleName Name of the rule to set a message for, eg, required, number, exactLength |
||
| 481 | * @param string|null $message Set to null to disable the custom message |
||
| 482 | * |
||
| 483 | * @return $this |
||
| 484 | * |
||
| 485 | * @since 2.0 |
||
| 486 | */ |
||
| 487 | 2 | public function setGlobalMessage($ruleName, $message) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Sets custom messages for one or more rules. Setting the value to "null" will remove the message |
||
| 501 | * |
||
| 502 | * @param string[] $messages |
||
| 503 | * |
||
| 504 | * @return $this |
||
| 505 | * |
||
| 506 | * @since 2.0 |
||
| 507 | */ |
||
| 508 | public function setGlobalMessages($messages) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Removes a global rule message |
||
| 520 | * |
||
| 521 | * @param string $ruleName |
||
| 522 | * |
||
| 523 | * @return $this |
||
| 524 | * |
||
| 525 | * @since 2.0 |
||
| 526 | */ |
||
| 527 | 1 | public function removeGlobalMessage($ruleName) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Gets the global message set for a rule |
||
| 536 | * |
||
| 537 | * @param string $ruleName |
||
| 538 | * |
||
| 539 | * @return null|string Will be null if there is no message |
||
| 540 | */ |
||
| 541 | 12 | public function getGlobalMessage($ruleName) |
|
| 550 | |||
| 551 | } |
||
| 552 |
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: