| Total Complexity | 107 |
| Total Lines | 523 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
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.
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 |
||
| 10 | class Validator |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Application Object |
||
| 14 | * |
||
| 15 | * @var \System\Application |
||
| 16 | */ |
||
| 17 | private $app; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Input name |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $input; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Input value |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $value; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Errors container |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $errors = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor |
||
| 42 | * |
||
| 43 | * @param \System\Application $app |
||
| 44 | */ |
||
| 45 | public function __construct(Application $app) |
||
| 46 | { |
||
| 47 | $this->app = $app; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function input($input, $request = 'post') |
||
| 51 | { |
||
| 52 | $this->input = $input; |
||
| 53 | |||
| 54 | $this->value = $this->app->request->$request($this->input); |
||
| 55 | |||
| 56 | return $this; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Get the value for the input name |
||
| 61 | * |
||
| 62 | * @return mixed |
||
| 63 | */ |
||
| 64 | private function value() |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Determine if the input is not empty |
||
| 71 | * |
||
| 72 | * @param bool $call |
||
| 73 | * @param string $msg |
||
| 74 | * @return $this |
||
| 75 | */ |
||
| 76 | public function require($call = true, $msg = null) |
||
| 77 | { |
||
| 78 | if ($call === false) return $this; |
||
| 79 | |||
| 80 | $value = $this->value(); |
||
| 81 | |||
| 82 | if ($value === '' || $value === null) { |
||
| 83 | $msg = $msg ?: 'this field is required'; |
||
| 84 | |||
| 85 | $this->addError($this->input, $msg); |
||
| 86 | } |
||
| 87 | return $this; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Call the function by given $type |
||
| 92 | * |
||
| 93 | * @param string $type |
||
| 94 | * @return mix |
||
|
|
|||
| 95 | */ |
||
| 96 | public function type($type) |
||
| 97 | { |
||
| 98 | return $this->$type(); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Determine if the input is valid email |
||
| 103 | * |
||
| 104 | * @param bool $call |
||
| 105 | * @param string $msg |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function email($call = true, $msg = null) |
||
| 109 | { |
||
| 110 | if ($call === false) return $this; |
||
| 111 | |||
| 112 | $value = $this->value(); |
||
| 113 | |||
| 114 | if (!$value && $value != '0') return $this; |
||
| 115 | |||
| 116 | if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { |
||
| 117 | $msg = $msg ?: 'e-mail is not valid'; |
||
| 118 | |||
| 119 | $this->addError($this->input, $msg); |
||
| 120 | } |
||
| 121 | return $this; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Determine if the input is an image |
||
| 126 | * |
||
| 127 | * @param bool $call |
||
| 128 | * @param string $customErrorMessage |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function image($call = true, $msg = null) |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Determine if the input has number |
||
| 149 | * |
||
| 150 | * @param bool $call |
||
| 151 | * @param string $msg |
||
| 152 | * @return $this |
||
| 153 | */ |
||
| 154 | public function number($call = true, $msg = null) |
||
| 155 | { |
||
| 156 | if ($call === false) return $this; |
||
| 157 | |||
| 158 | $value = $this->value(); |
||
| 159 | |||
| 160 | if (!$value && $value != '0') return $this; |
||
| 161 | |||
| 162 | if (!is_numeric($value)) { |
||
| 163 | $msg = $msg ?: 'this field must be a number'; |
||
| 164 | |||
| 165 | $this->addError($this->input, $msg); |
||
| 166 | } |
||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Determine if the input has float value |
||
| 172 | * |
||
| 173 | * @param bool $call |
||
| 174 | * @param string $msg |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function float($call = true, $msg = null) |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Determine if the input is a date |
||
| 195 | * Determine if the input between the range if the $options['start'] |
||
| 196 | * or the $options ['end'] is exists |
||
| 197 | * |
||
| 198 | * @param string $options |
||
| 199 | * @param string $msg |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | public function date($options = [], $msg = null) |
||
| 203 | { |
||
| 204 | if ($options === false) return $this; |
||
| 205 | |||
| 206 | $value = $this->value(); |
||
| 207 | |||
| 208 | if (!$value && $value != '0') return $this; |
||
| 209 | |||
| 210 | $options = json_encode($options); |
||
| 211 | $options = json_decode($options); |
||
| 212 | |||
| 213 | $date = new DateV($value, $options); |
||
| 214 | |||
| 215 | if (!$date->isAdate()) { |
||
| 216 | $msg = $msg ?: "this field must be a Date"; |
||
| 217 | $this->addError($this->input, $msg); |
||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | extract($date->dateMethods($options)); |
||
| 221 | |||
| 222 | if (!$date->$method()) { |
||
| 223 | $this->addError($this->input, $msg); |
||
| 224 | } |
||
| 225 | return $this; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Determine if the input has simple text |
||
| 230 | * |
||
| 231 | * @param bool $call |
||
| 232 | * @param string $msg |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | public function text($call = true, $msg = null) |
||
| 236 | { |
||
| 237 | if ($call === false) return $this; |
||
| 238 | |||
| 239 | $value = $this->value(); |
||
| 240 | |||
| 241 | if (!$value && $value != '0') return $this; |
||
| 242 | |||
| 243 | if (!is_string($value)) { |
||
| 244 | $msg = $msg ?: 'the field must be a text'; |
||
| 245 | |||
| 246 | $this->addError($this->input, $msg); |
||
| 247 | } |
||
| 248 | return $this; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Determine if the input has pure string |
||
| 253 | * |
||
| 254 | * @param bool $call |
||
| 255 | * @param string $msg |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function noNumbers($call = true, $msg = null) |
||
| 259 | { |
||
| 260 | if ($call === false) return $this; |
||
| 261 | |||
| 262 | $value = $this->value(); |
||
| 263 | |||
| 264 | if (!$value && $value != '0') return $this; |
||
| 265 | |||
| 266 | if (preg_match('~[0-9]~', $value)) { |
||
| 267 | $msg = $msg ?: 'numbers are not allow'; |
||
| 268 | |||
| 269 | $this->addError($this->input, $msg); |
||
| 270 | } |
||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Determine if the input has pure string |
||
| 276 | * |
||
| 277 | * @param array $excepts |
||
| 278 | * @param string $msg |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | public function characters($excepts, $msg = null) |
||
| 282 | { |
||
| 283 | if ($excepts === false) return $this; |
||
| 284 | |||
| 285 | $value = $this->value(); |
||
| 286 | |||
| 287 | if (!$value && $value != '0') return $this; |
||
| 288 | |||
| 289 | $characters = new Characters($excepts, $value); |
||
| 290 | |||
| 291 | extract($characters->variables()); |
||
| 292 | |||
| 293 | foreach ($methods as $method => $options) { |
||
| 294 | if (call_user_func_array(array($characters, $method), $options[0])) { |
||
| 295 | $msg = $msg ?: $options[1]; |
||
| 296 | $this->addError($this->input, $msg); |
||
| 297 | return $this; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | $re = "/^[0-9\\s$chars$langsRegex]*$/u"; |
||
| 301 | |||
| 302 | if (!preg_match($re, $value)) { |
||
| 303 | $msg = $characters->charactersMsg($chars, $languages, $msg); |
||
| 304 | $this->addError($this->input, $msg); |
||
| 305 | } |
||
| 306 | return $this; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Determine if the input has spaces between the letters or the words |
||
| 311 | * |
||
| 312 | * @param bool $call |
||
| 313 | * @param string $msg |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function noSpaces($call = true, $msg = null) |
||
| 317 | { |
||
| 318 | if ($call === false) return $this; |
||
| 319 | |||
| 320 | $value = $this->value(); |
||
| 321 | |||
| 322 | if (!$value && $value != '0') return $this; |
||
| 323 | |||
| 324 | if (preg_match('/\s/', $value)) { |
||
| 325 | $msg = $msg ?: 'spaces are not allow'; |
||
| 326 | |||
| 327 | $this->addError($this->input, $msg); |
||
| 328 | } |
||
| 329 | return $this; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Determine if the given input has the value that are passed |
||
| 334 | * |
||
| 335 | * @param array $characters |
||
| 336 | * @param string $msg |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | public function alloweJust($characters = [], $msg = null) |
||
| 340 | { |
||
| 341 | if ($characters === false) return $this; |
||
| 342 | |||
| 343 | $value = $this->value(); |
||
| 344 | |||
| 345 | if (!$value && $value != '0') return $this; |
||
| 346 | |||
| 347 | $allowedCharacters = new AlloweJust($this->app, $characters); |
||
| 348 | $characters = $allowedCharacters->getCharacters(); |
||
| 349 | |||
| 350 | if (!in_array($value, $characters)) { |
||
| 351 | $msg = $msg ?: 'wrong value'; |
||
| 352 | $this->addError($this->input, $msg); |
||
| 353 | } |
||
| 354 | return $this; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Determine if the input value should equal length |
||
| 359 | * |
||
| 360 | * @param int $length |
||
| 361 | * @param string $msg |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | public function length($length = null, $msg = null) |
||
| 365 | { |
||
| 366 | if ($length === false) return $this; |
||
| 367 | |||
| 368 | $value = $this->value(); |
||
| 369 | |||
| 370 | if (!$value && $value != '0') return $this; |
||
| 371 | |||
| 372 | if (strlen($value) !== $length) { |
||
| 373 | $msg = $msg ?: `this field can be just ${length} charachter`; |
||
| 374 | |||
| 375 | $this->addError($this->input, $msg); |
||
| 376 | } |
||
| 377 | return $this; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Determine if the input value should be at most the given length |
||
| 382 | * |
||
| 383 | * @param int $length |
||
| 384 | * @param string $msg |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | public function maxLen($length = null, $msg = null) |
||
| 388 | { |
||
| 389 | if ($length === false) return $this; |
||
| 390 | |||
| 391 | $value = $this->value(); |
||
| 392 | |||
| 393 | if (!$value && $value != '0') return $this; |
||
| 394 | |||
| 395 | if (strlen($value) > $length) { |
||
| 396 | $msg = $msg ?: "this field can be maximum $length charachter"; |
||
| 397 | |||
| 398 | $this->addError($this->input, $msg); |
||
| 399 | } |
||
| 400 | return $this; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Determine if the input value should be at least the given length |
||
| 405 | * |
||
| 406 | * @param int $length |
||
| 407 | * @param string $msg |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | public function minLen($length = null, $msg = null) |
||
| 411 | { |
||
| 412 | if ($length === false) return $this; |
||
| 413 | |||
| 414 | $value = $this->value(); |
||
| 415 | |||
| 416 | if (!$value && $value != '0') return $this; |
||
| 417 | |||
| 418 | if (strlen($value) < $length) { |
||
| 419 | $msg = $msg ?: "this field can be minimum $length charachter"; |
||
| 420 | |||
| 421 | $this->addError($this->input, $msg); |
||
| 422 | } |
||
| 423 | return $this; |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Determine if the $input matches the given input |
||
| 428 | * |
||
| 429 | * @param string $input |
||
| 430 | * @param string $msg |
||
| 431 | * @return $this |
||
| 432 | */ |
||
| 433 | public function match($input, $msg = null) |
||
| 434 | { |
||
| 435 | if ($input === false) return $this; |
||
| 436 | |||
| 437 | $value = $this->value(); |
||
| 438 | |||
| 439 | $valueConfirm = $this->app->request->post($input); |
||
| 440 | |||
| 441 | if ($value && $valueConfirm) { |
||
| 442 | if ($value !== $valueConfirm) { |
||
| 443 | $msg = $msg ?: 'passwords doesn\'t match'; |
||
| 444 | |||
| 445 | $this->addError('match', $msg); |
||
| 446 | } |
||
| 447 | } |
||
| 448 | return $this; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Determine if the input is unique in database |
||
| 453 | * |
||
| 454 | * @param array $data |
||
| 455 | * @param string $msg |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | public function unique($data = [], $msg = null) |
||
| 459 | { |
||
| 460 | if ($data === false) return $this; |
||
| 461 | |||
| 462 | $value = $this->value(); |
||
| 463 | |||
| 464 | if (!$data) return $this; |
||
| 465 | |||
| 466 | if (is_array($data)) { |
||
| 467 | list($table, $column) = $data; |
||
| 468 | } else { |
||
| 469 | $table = $data; |
||
| 470 | $column = $this->input; |
||
| 471 | } |
||
| 472 | |||
| 473 | $result = $this->app->db->select($column)->from($table)->where($column . ' = ? ', $value)->fetch(); |
||
| 474 | |||
| 475 | if ($result) { |
||
| 476 | $msg = $msg ?: sprintf('%s is already exist', ucfirst($this->input)); |
||
| 477 | |||
| 478 | $this->addError($this->input, $msg); |
||
| 479 | } |
||
| 480 | return $this; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Determine if all inputs are valid |
||
| 485 | * |
||
| 486 | * @return bool |
||
| 487 | */ |
||
| 488 | public function passes() |
||
| 489 | { |
||
| 490 | return empty($this->errors); |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Determine if there are any invalid inputs |
||
| 495 | * |
||
| 496 | * @return bool |
||
| 497 | */ |
||
| 498 | public function fails() |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Determine if the given input has previous errors |
||
| 505 | * |
||
| 506 | * @param string $input |
||
| 507 | */ |
||
| 508 | private function hasError($input) |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Add input error |
||
| 515 | * |
||
| 516 | * @param string $inputName |
||
| 517 | * @param string $msg |
||
| 518 | * @return void |
||
| 519 | */ |
||
| 520 | public function addError($input, $msg) |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Get all errors |
||
| 527 | * |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | public function getErrors() |
||
| 533 | } |
||
| 534 | } |
||
| 535 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths