| Conditions | 57 |
| Paths | 220 |
| Total Lines | 210 |
| Lines | 96 |
| Ratio | 45.71 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 372 | protected function parseRule($rule, $attribute, &$attributeData, $seed, $routeData) |
||
| 373 | { |
||
| 374 | $faker = Factory::create(); |
||
| 375 | $faker->seed(crc32($seed)); |
||
| 376 | |||
| 377 | $parsedRule = $this->parseStringRule($rule); |
||
| 378 | $parsedRule[0] = $this->normalizeRule($parsedRule[0]); |
||
| 379 | list($rule, $parameters) = $parsedRule; |
||
| 380 | |||
| 381 | switch ($rule) { |
||
| 382 | case 'required': |
||
| 383 | $attributeData['required'] = true; |
||
| 384 | break; |
||
| 385 | case 'accepted': |
||
| 386 | $attributeData['required'] = true; |
||
| 387 | $attributeData['type'] = 'boolean'; |
||
| 388 | $attributeData['value'] = true; |
||
| 389 | break; |
||
| 390 | View Code Duplication | case 'after': |
|
| 391 | $attributeData['type'] = 'date'; |
||
| 392 | $format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850; |
||
| 393 | |||
| 394 | if (strtotime($parameters[0]) === false) { |
||
| 395 | // the `after` date refers to another parameter in the request |
||
| 396 | $paramName = $parameters[0]; |
||
| 397 | $attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription(); |
||
| 398 | $attributeData['value'] = date($format, strtotime('+1 day', strtotime($routeData['parameters'][$paramName]['value']))); |
||
| 399 | } else { |
||
| 400 | $attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription(); |
||
| 401 | $attributeData['value'] = date($format, strtotime('+1 day', strtotime($parameters[0]))); |
||
| 402 | } |
||
| 403 | break; |
||
| 404 | View Code Duplication | case 'alpha': |
|
| 405 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 406 | $attributeData['value'] = $faker->word; |
||
| 407 | break; |
||
| 408 | case 'alpha_dash': |
||
| 409 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 410 | break; |
||
| 411 | case 'alpha_num': |
||
| 412 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 413 | break; |
||
| 414 | View Code Duplication | case 'in': |
|
| 415 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 416 | $attributeData['value'] = $faker->randomElement($parameters); |
||
| 417 | break; |
||
| 418 | View Code Duplication | case 'not_in': |
|
| 419 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 420 | $attributeData['value'] = $faker->word; |
||
| 421 | break; |
||
| 422 | View Code Duplication | case 'min': |
|
| 423 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 424 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 425 | $attributeData['value'] = $faker->numberBetween($parameters[0]); |
||
| 426 | } |
||
| 427 | break; |
||
| 428 | View Code Duplication | case 'max': |
|
| 429 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 430 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 431 | $attributeData['value'] = $faker->numberBetween(0, $parameters[0]); |
||
| 432 | } |
||
| 433 | break; |
||
| 434 | View Code Duplication | case 'between': |
|
| 435 | if (! isset($attributeData['type'])) { |
||
| 436 | $attributeData['type'] = 'numeric'; |
||
| 437 | } |
||
| 438 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 439 | $attributeData['value'] = $faker->numberBetween($parameters[0], $parameters[1]); |
||
| 440 | break; |
||
| 441 | View Code Duplication | case 'before': |
|
| 442 | $attributeData['type'] = 'date'; |
||
| 443 | $format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850; |
||
| 444 | |||
| 445 | if (strtotime($parameters[0]) === false) { |
||
| 446 | // the `before` date refers to another parameter in the request |
||
| 447 | $paramName = $parameters[0]; |
||
| 448 | $attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription(); |
||
| 449 | $attributeData['value'] = date($format, strtotime('-1 day', strtotime($routeData['parameters'][$paramName]['value']))); |
||
| 450 | } else { |
||
| 451 | $attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription(); |
||
| 452 | $attributeData['value'] = date($format, strtotime('-1 day', strtotime($parameters[0]))); |
||
| 453 | } |
||
| 454 | break; |
||
| 455 | View Code Duplication | case 'date_format': |
|
| 456 | $attributeData['type'] = 'date'; |
||
| 457 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 458 | $attributeData['format'] = $parameters[0]; |
||
| 459 | $attributeData['value'] = date($attributeData['format']); |
||
| 460 | break; |
||
| 461 | case 'different': |
||
| 462 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 463 | break; |
||
| 464 | case 'digits': |
||
| 465 | $attributeData['type'] = 'numeric'; |
||
| 466 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 467 | $attributeData['value'] = ($parameters[0] < 9) ? $faker->randomNumber($parameters[0], true) : substr(mt_rand(100000000, mt_getrandmax()), 0, $parameters[0]); |
||
| 468 | break; |
||
| 469 | case 'digits_between': |
||
| 470 | $attributeData['type'] = 'numeric'; |
||
| 471 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 472 | break; |
||
| 473 | View Code Duplication | case 'file': |
|
| 474 | $attributeData['type'] = 'file'; |
||
| 475 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 476 | break; |
||
| 477 | View Code Duplication | case 'image': |
|
| 478 | $attributeData['type'] = 'image'; |
||
| 479 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 480 | break; |
||
| 481 | case 'json': |
||
| 482 | $attributeData['type'] = 'string'; |
||
| 483 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 484 | $attributeData['value'] = json_encode(['foo', 'bar', 'baz']); |
||
| 485 | break; |
||
| 486 | case 'mimetypes': |
||
| 487 | View Code Duplication | case 'mimes': |
|
| 488 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 489 | break; |
||
| 490 | View Code Duplication | case 'required_if': |
|
| 491 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 492 | break; |
||
| 493 | View Code Duplication | case 'required_unless': |
|
| 494 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 495 | break; |
||
| 496 | View Code Duplication | case 'required_with': |
|
| 497 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 498 | break; |
||
| 499 | View Code Duplication | case 'required_with_all': |
|
| 500 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 501 | break; |
||
| 502 | View Code Duplication | case 'required_without': |
|
| 503 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 504 | break; |
||
| 505 | View Code Duplication | case 'required_without_all': |
|
| 506 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 507 | break; |
||
| 508 | case 'same': |
||
| 509 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 510 | break; |
||
| 511 | case 'size': |
||
| 512 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 513 | break; |
||
| 514 | View Code Duplication | case 'timezone': |
|
| 515 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 516 | $attributeData['value'] = $faker->timezone; |
||
| 517 | break; |
||
| 518 | case 'exists': |
||
| 519 | $fieldName = isset($parameters[1]) ? $parameters[1] : $attribute; |
||
| 520 | $attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $fieldName])->getDescription(); |
||
| 521 | break; |
||
| 522 | case 'active_url': |
||
| 523 | $attributeData['type'] = 'url'; |
||
| 524 | $attributeData['value'] = $faker->url; |
||
| 525 | break; |
||
| 526 | case 'regex': |
||
| 527 | $attributeData['type'] = 'string'; |
||
| 528 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 529 | break; |
||
| 530 | case 'boolean': |
||
| 531 | $attributeData['value'] = true; |
||
| 532 | $attributeData['type'] = $rule; |
||
| 533 | break; |
||
| 534 | case 'array': |
||
| 535 | $attributeData['value'] = $faker->word; |
||
| 536 | $attributeData['type'] = $rule; |
||
| 537 | break; |
||
| 538 | case 'date': |
||
| 539 | $attributeData['value'] = $faker->date(); |
||
| 540 | $attributeData['type'] = $rule; |
||
| 541 | break; |
||
| 542 | case 'email': |
||
| 543 | $attributeData['value'] = $faker->safeEmail; |
||
| 544 | $attributeData['type'] = $rule; |
||
| 545 | break; |
||
| 546 | case 'string': |
||
| 547 | $attributeData['value'] = $faker->word; |
||
| 548 | $attributeData['type'] = $rule; |
||
| 549 | break; |
||
| 550 | case 'integer': |
||
| 551 | $attributeData['value'] = $faker->randomNumber(); |
||
| 552 | $attributeData['type'] = $rule; |
||
| 553 | break; |
||
| 554 | case 'numeric': |
||
| 555 | $attributeData['value'] = $faker->randomNumber(); |
||
| 556 | $attributeData['type'] = $rule; |
||
| 557 | break; |
||
| 558 | case 'url': |
||
| 559 | $attributeData['value'] = $faker->url; |
||
| 560 | $attributeData['type'] = $rule; |
||
| 561 | break; |
||
| 562 | case 'ip': |
||
| 563 | $attributeData['value'] = $faker->ipv4; |
||
| 564 | $attributeData['type'] = $rule; |
||
| 565 | break; |
||
| 566 | default: |
||
| 567 | $unknownRuleDescription = Description::parse($rule)->getDescription(); |
||
| 568 | if ($unknownRuleDescription) { |
||
| 569 | $attributeData['description'][] = $unknownRuleDescription; |
||
| 570 | } |
||
| 571 | break; |
||
| 572 | } |
||
| 573 | |||
| 574 | if ($attributeData['value'] === '') { |
||
| 575 | $attributeData['value'] = $faker->word; |
||
| 576 | } |
||
| 577 | |||
| 578 | if (is_null($attributeData['type'])) { |
||
| 579 | $attributeData['type'] = 'string'; |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 794 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.