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 declare(strict_types=1); |
||
| 16 | class Validator |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected static $validRuleProperties = [ |
||
| 22 | 'required', |
||
| 23 | 'nonEmpty', |
||
| 24 | 'min', |
||
| 25 | 'max', |
||
| 26 | 'minLength', |
||
| 27 | 'maxLength', |
||
| 28 | 'regexp', |
||
| 29 | 'regexpExpl', |
||
| 30 | 'email', |
||
| 31 | 'textKey', |
||
| 32 | 'date', |
||
| 33 | 'dateTime', |
||
| 34 | 'nullable', |
||
| 35 | 'custom' |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ValidatorTranslatorInterface |
||
| 40 | */ |
||
| 41 | protected $translator; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $validatedData = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $fieldsRuleProperties = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor. |
||
| 55 | * |
||
| 56 | * @param array $fieldsRuleProperties |
||
| 57 | * @param ValidatorTranslatorInterface|SymfonyTranslatorInterface|null $translator |
||
| 58 | */ |
||
| 59 | 47 | public function __construct(array $fieldsRuleProperties = [], $translator = null) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @param array $newFieldsRuleProperties |
||
| 76 | */ |
||
| 77 | 10 | public function addFieldsRuleProperties(array $newFieldsRuleProperties): void |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $fieldName |
||
| 93 | */ |
||
| 94 | 1 | public function removeFieldRuleProperties(string $fieldName): void |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $fieldName |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | 2 | public function getFieldRuleProperties(string $fieldName): array |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Validate and return error messages (if any). |
||
| 114 | * |
||
| 115 | * @param array|null $data The data (e.g. from a form post) to be validated and set |
||
| 116 | * @return array An array with all (if any) of error messages |
||
| 117 | */ |
||
| 118 | 10 | public function validate(?array $data): array |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param mixed $value |
||
| 157 | * @param array $ruleProperties |
||
| 158 | * @return string|null |
||
| 159 | */ |
||
| 160 | 40 | public function validateValue($value, array $ruleProperties): ?string |
|
| 161 | { |
||
| 162 | // Field name |
||
| 163 | 40 | if (isset($ruleProperties['textKey'])) { |
|
| 164 | 1 | $fieldName = $this->translator->trans($ruleProperties['textKey']); |
|
| 165 | 1 | unset($ruleProperties['textKey']); |
|
| 166 | } else { |
||
| 167 | 39 | $fieldName = $this->translator->trans('errorTheNoneSpecifiedField'); |
|
| 168 | } |
||
| 169 | |||
| 170 | // Unset rule properties that are not rules |
||
| 171 | 40 | $rules = $ruleProperties; |
|
| 172 | 40 | unset($rules['textKey']); |
|
| 173 | 40 | unset($rules['regexpExpl']); |
|
| 174 | 40 | unset($rules['trim']); |
|
| 175 | |||
| 176 | 40 | $isValueSet = (is_scalar($value) || is_null($value)) && ((string) $value) !== ''; |
|
| 177 | |||
| 178 | 40 | foreach ($rules as $rule => $ruleContents) { |
|
| 179 | 40 | $errorMsg = null; |
|
| 180 | |||
| 181 | switch ($rule) { |
||
| 182 | 40 | case 'required': |
|
| 183 | 9 | if (!is_bool($ruleContents)) { |
|
| 184 | 1 | throw new \InvalidArgumentException("Invalid required validation rule[{$rule}]"); |
|
| 185 | } |
||
| 186 | |||
| 187 | 8 | if ($ruleContents && !$isValueSet) { |
|
| 188 | 4 | $errorMsg = $this->translator->trans('errorFieldXIsRequired', ['%field%' => $fieldName]); |
|
| 189 | } |
||
| 190 | |||
| 191 | 8 | break; |
|
| 192 | 35 | case 'nonEmpty': |
|
| 193 | 4 | if (!is_bool($ruleContents)) { |
|
| 194 | 1 | throw new \InvalidArgumentException("Invalid nonEmpty validation rule[{$rule}]"); |
|
| 195 | } |
||
| 196 | |||
| 197 | 3 | if ($ruleContents && empty($value)) { |
|
| 198 | 2 | $errorMsg = $this->translator->trans('errorFieldXIsRequired', ['%field%' => $fieldName]); |
|
| 199 | } |
||
| 200 | |||
| 201 | 3 | break; |
|
| 202 | 31 | case 'min': |
|
| 203 | 3 | if (!is_numeric($ruleContents)) { |
|
| 204 | 1 | throw new \InvalidArgumentException("Invalid min validation rule[{$ruleContents}]"); |
|
| 205 | } |
||
| 206 | |||
| 207 | 2 | if ($isValueSet && (!is_numeric($value) || $value < $ruleContents)) { |
|
| 208 | 1 | $errorMsg = $this->translator->trans( |
|
| 209 | 1 | 'errorFieldMustBeMinNumber', |
|
| 210 | 1 | ['%field%' => $fieldName, '%number%' => $ruleContents] |
|
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | 2 | break; |
|
| 215 | 30 | case 'max': |
|
| 216 | 3 | if (!is_numeric($ruleContents)) { |
|
| 217 | 1 | throw new \InvalidArgumentException("Invalid max validation rule[{$ruleContents}]"); |
|
| 218 | } |
||
| 219 | |||
| 220 | 2 | if ($isValueSet && (!is_numeric($value) || $value > $ruleContents)) { |
|
| 221 | 1 | $errorMsg = $this->translator->trans( |
|
| 222 | 1 | 'errorFieldMustBeMaxNumber', |
|
| 223 | [ |
||
| 224 | 1 | '%field%' => $fieldName, |
|
| 225 | 1 | '%number%' => $ruleContents |
|
| 226 | ] |
||
| 227 | ); |
||
| 228 | } |
||
| 229 | |||
| 230 | 2 | break; |
|
| 231 | 29 | case 'minLength': |
|
| 232 | 6 | if (!is_int($ruleContents) || $ruleContents < 1) { |
|
| 233 | 1 | throw new \InvalidArgumentException("Invalid min length validation rule[{$ruleContents}]"); |
|
| 234 | } |
||
| 235 | |||
| 236 | 5 | if ($isValueSet && is_string($value) && mb_strlen($value) < $ruleContents) { |
|
| 237 | 1 | $errorMsg = $this->translator->trans( |
|
| 238 | 1 | 'errorFieldMustBeMinXLength', |
|
| 239 | 1 | ['%field%' => $fieldName, '%numberOf%' => $ruleContents] |
|
| 240 | ); |
||
| 241 | } |
||
| 242 | |||
| 243 | 5 | break; |
|
| 244 | 25 | case 'maxLength': |
|
| 245 | 4 | if (!is_int($ruleContents) || $ruleContents < 1) { |
|
| 246 | 1 | throw new \InvalidArgumentException("Invalid max length validation rule[{$ruleContents}]"); |
|
| 247 | } |
||
| 248 | |||
| 249 | 3 | if ($isValueSet && is_string($value) && mb_strlen($value) > $ruleContents) { |
|
| 250 | 1 | $errorMsg = $this->translator->trans( |
|
| 251 | 1 | 'errorFieldMustBeMaxXLength', |
|
| 252 | 1 | ['%field%' => $fieldName, '%numberOf%' => $ruleContents] |
|
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | 3 | break; |
|
| 257 | 23 | case 'length': |
|
| 258 | 4 | if (!is_int($ruleContents) || $ruleContents < 1) { |
|
| 259 | 1 | throw new \InvalidArgumentException("Invalid length validation rule[{$ruleContents}]"); |
|
| 260 | } |
||
| 261 | |||
| 262 | 3 | if ($isValueSet && is_string($value) && mb_strlen($value) !== $ruleContents) { |
|
| 263 | 1 | $errorMsg = $this->translator->trans( |
|
| 264 | 1 | 'errorFieldMustBeXLength', |
|
| 265 | 1 | ['%field%' => $fieldName, '%numberOf%' => $ruleContents] |
|
| 266 | ); |
||
| 267 | } |
||
| 268 | |||
| 269 | 3 | break; |
|
| 270 | 20 | case 'regexp': |
|
| 271 | 3 | if (!$ruleContents) { |
|
| 272 | 1 | throw new \InvalidArgumentException("Invalid regexp validation rule[{$ruleContents}]"); |
|
| 273 | } |
||
| 274 | |||
| 275 | 2 | if ($isValueSet && !preg_match('/' . $ruleContents . '/', (string) $value)) { |
|
| 276 | 1 | $errorMsg = $this->translator->trans( |
|
| 277 | 1 | 'errorFieldInvalidFormat', |
|
| 278 | 1 | ['%field%' => $fieldName] |
|
| 279 | ); |
||
| 280 | |||
| 281 | 1 | if (isset($ruleProperties['regexpExpl'])) { |
|
| 282 | 1 | $errorMsg .= $this->translator->trans( |
|
| 283 | 1 | 'errorFieldValidCharactersAreX', |
|
| 284 | 1 | ['%characters%' => $ruleProperties['regexpExpl']] |
|
| 285 | ); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | 2 | break; |
|
| 290 | 19 | case 'email': |
|
| 291 | 4 | if (!is_bool($ruleContents)) { |
|
| 292 | 1 | throw new \InvalidArgumentException("Invalid email validation rule[{$rule}]"); |
|
| 293 | } |
||
| 294 | |||
| 295 | 3 | if ($ruleContents && $isValueSet && !filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 296 | 1 | $errorMsg = $this->translator->trans('errorInvalidEmail'); |
|
| 297 | } |
||
| 298 | |||
| 299 | 3 | break; |
|
| 300 | 17 | case 'date': |
|
| 301 | // No break |
||
| 302 | 15 | case 'dateTime': |
|
| 303 | 6 | if (!is_bool($ruleContents)) { |
|
| 304 | 1 | throw new \InvalidArgumentException("Invalid date validation rule[{$rule}]"); |
|
| 305 | } |
||
| 306 | |||
| 307 | 5 | if ($ruleContents && $isValueSet) { |
|
| 308 | $isValueOk = function ($format) use ($value) { |
||
| 309 | 4 | return (\DateTime::createFromFormat($format, $value) !== false |
|
| 310 | 4 | && !\DateTime::getLastErrors()["warning_count"] |
|
| 311 | 4 | && !\DateTime::getLastErrors()["error_count"]); |
|
| 312 | 4 | }; |
|
| 313 | |||
| 314 | // Allow datetime with and without seconds |
||
| 315 | 4 | if (($rule === 'date' && !$isValueOk('Y-m-d')) |
|
| 316 | 3 | || ($rule === 'dateTime' && !$isValueOk('Y-m-d H:i') |
|
| 317 | 4 | && !$isValueOk('Y-m-d H:i:s')) |
|
| 318 | ) { |
||
| 319 | 2 | $errorMsg = $this->translator->trans( |
|
| 320 | 2 | ($rule === 'date') ? 'errorInvalidDate' : 'errorInvalidDateTime' |
|
| 321 | ); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | 5 | break; |
|
| 326 | 11 | case 'custom': |
|
| 327 | 2 | if (!is_callable($ruleContents)) { |
|
| 328 | 1 | throw new \InvalidArgumentException("Invalid custom validation rule[{$rule}]"); |
|
| 329 | } |
||
| 330 | |||
| 331 | 1 | $errorMsg = $ruleContents($value); |
|
| 332 | |||
| 333 | 1 | break; |
|
| 334 | 9 | case 'nullable': |
|
| 335 | 8 | break; |
|
| 336 | default: |
||
| 337 | 1 | throw new \InvalidArgumentException("Unknown validation rule[{$rule}]"); |
|
| 338 | } |
||
| 339 | |||
| 340 | 28 | if ($errorMsg) { |
|
| 341 | 28 | return $errorMsg; |
|
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | 21 | return null; |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $key |
||
| 350 | * @param mixed $default |
||
| 351 | * @return array|mixed |
||
| 352 | */ |
||
| 353 | 5 | public function getValidatedData(string $key = null, $default = null) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | 1 | public static function getValidRuleProperties(): array |
|
| 369 | } |
||
| 370 |