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 |
||
| 49 | class Validator extends Component implements ValidatorInterface, LoggerAwareInterface |
||
| 50 | { |
||
| 51 | use LoggerTrait, TranslatorTrait, SaturateTrait; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Return from validation rule to stop any future field validations. Internal contract. |
||
| 55 | */ |
||
| 56 | const STOP_VALIDATION = -99; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @invisible |
||
| 60 | * @var ValidatorConfig |
||
| 61 | */ |
||
| 62 | private $config = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array|\ArrayAccess |
||
| 66 | */ |
||
| 67 | private $data = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Validation rules, see class title for description. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $rules = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Error messages raised while validation. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private $errors = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Errors provided from outside. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | private $registeredErrors = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * If rule has no definer error message this text will be used instead. Localizable. |
||
| 92 | * |
||
| 93 | * @invisible |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $defaultMessage = "[[Condition '{condition}' does not meet.]]"; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @invisible |
||
| 100 | * @var ContainerInterface |
||
| 101 | */ |
||
| 102 | protected $container = null; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | * |
||
| 107 | * @param array $rules Validation rules. |
||
| 108 | * @param array|\ArrayAccess $data Data or model to be validated. |
||
| 109 | * @param ValidatorConfig $config Saturated using shared container |
||
| 110 | * @param ContainerInterface $container Saturated using shared container |
||
| 111 | * |
||
| 112 | * @throws ScopeException |
||
| 113 | */ |
||
| 114 | public function __construct( |
||
| 115 | array $rules = [], |
||
| 116 | $data = [], |
||
| 117 | ValidatorConfig $config = null, |
||
| 118 | ContainerInterface $container = null |
||
| 119 | ) { |
||
| 120 | $this->data = $data; |
||
| 121 | $this->rules = $rules; |
||
| 122 | |||
| 123 | $this->config = $this->saturate($config, ValidatorConfig::class); |
||
| 124 | $this->container = $this->saturate($container, ContainerInterface::class); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | public function setRules(array $rules): ValidatorInterface |
||
| 131 | { |
||
| 132 | if ($this->rules == $rules) { |
||
| 133 | return $this; |
||
| 134 | } |
||
| 135 | |||
| 136 | $this->rules = $rules; |
||
| 137 | $this->reset(); |
||
| 138 | |||
| 139 | return $this; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function setData($data): ValidatorInterface |
||
| 146 | { |
||
| 147 | $data = $this->extractData($data); |
||
| 148 | if ($this->data === $data) { |
||
| 149 | return $this; |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->data = $data; |
||
| 153 | $this->reset(); |
||
| 154 | |||
| 155 | return $this; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | public function getData() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function isValid(): bool |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | public function registerError(string $field, string $error): ValidatorInterface |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | public function flushRegistered(): ValidatorInterface |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function hasErrors(): bool |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function getErrors(): array |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Receive field from context data or return default value. |
||
| 216 | * |
||
| 217 | * @param string $field |
||
| 218 | * @param mixed $default |
||
| 219 | * |
||
| 220 | * @return mixed |
||
| 221 | */ |
||
| 222 | public function getValue(string $field, $default = null) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Reset validation state. |
||
| 233 | */ |
||
| 234 | public function reset() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Validate context data with set of validation rules. |
||
| 242 | */ |
||
| 243 | protected function validate() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Check field with given condition. Can return instance of Checker (data is not valid) to |
||
| 307 | * clarify error. |
||
| 308 | * |
||
| 309 | * @param string $field |
||
| 310 | * @param mixed $value |
||
| 311 | * @param mixed $condition Reference, can be altered if alias exists. |
||
| 312 | * @param array $arguments Rule arguments if any. |
||
| 313 | * |
||
| 314 | * @return bool|CheckerInterface |
||
| 315 | * @throws ValidationException |
||
| 316 | */ |
||
| 317 | protected function check(string $field, $value, &$condition, array $arguments = []) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Does validation config has alias defined for a given checker name or class exists |
||
| 360 | * |
||
| 361 | * @param string $name |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | protected function hasChecker(string $name): bool |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get or create instance of validation checker. |
||
| 382 | * |
||
| 383 | * @param string $name |
||
| 384 | * |
||
| 385 | * @return CheckerInterface |
||
| 386 | * @throws ValidationException |
||
| 387 | */ |
||
| 388 | protected function getChecker(string $name): CheckerInterface |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Fetch validation rule arguments from rule definition. |
||
| 404 | * |
||
| 405 | * @param array $rule |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | private function fetchArguments(array $rule): array |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Fetch error message from rule definition or use default message. Method will check "message" |
||
| 418 | * and "error" properties of definition. |
||
| 419 | * |
||
| 420 | * @param array $rule |
||
| 421 | * @param string $message Default message to use. |
||
| 422 | * |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | private function fetchMessage(array $rule, string $message): string |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Register error message for specified field. Rule definition will be interpolated into |
||
| 440 | * message. |
||
| 441 | * |
||
| 442 | * @param string $field |
||
| 443 | * @param string $message |
||
| 444 | * @param mixed $condition |
||
| 445 | * @param array $arguments |
||
| 446 | */ |
||
| 447 | private function addMessage(string $field, string $message, $condition, array $arguments = []) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param string $field |
||
| 465 | * @param array $condition |
||
| 466 | * @param \Throwable $e |
||
| 467 | */ |
||
| 468 | protected function logException(string $field, $condition, \Throwable $e) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param array|\ArrayAccess|EntityInterface $data |
||
| 486 | * |
||
| 487 | * @return array |
||
| 488 | */ |
||
| 489 | private function extractData($data): array |
||
| 507 | } |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: