| 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 |
||
| 405 | protected function parseRule($rule, $attribute, &$attributeData, $seed, $routeData) |
||
| 406 | { |
||
| 407 | $faker = Factory::create(); |
||
| 408 | $faker->seed(crc32($seed)); |
||
| 409 | |||
| 410 | $parsedRule = $this->parseStringRule($rule); |
||
| 411 | $parsedRule[0] = $this->normalizeRule($parsedRule[0]); |
||
| 412 | list($rule, $parameters) = $parsedRule; |
||
| 413 | |||
| 414 | switch ($rule) { |
||
| 415 | case 'required': |
||
| 416 | $attributeData['required'] = true; |
||
| 417 | break; |
||
| 418 | case 'accepted': |
||
| 419 | $attributeData['required'] = true; |
||
| 420 | $attributeData['type'] = 'boolean'; |
||
| 421 | $attributeData['value'] = true; |
||
| 422 | break; |
||
| 423 | View Code Duplication | case 'after': |
|
| 424 | $attributeData['type'] = 'date'; |
||
| 425 | $format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850; |
||
| 426 | |||
| 427 | if (strtotime($parameters[0]) === false) { |
||
| 428 | // the `after` date refers to another parameter in the request |
||
| 429 | $paramName = $parameters[0]; |
||
| 430 | $attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription(); |
||
| 431 | $attributeData['value'] = date($format, strtotime('+1 day', strtotime($routeData['parameters'][$paramName]['value']))); |
||
| 432 | } else { |
||
| 433 | $attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription(); |
||
| 434 | $attributeData['value'] = date($format, strtotime('+1 day', strtotime($parameters[0]))); |
||
| 435 | } |
||
| 436 | break; |
||
| 437 | View Code Duplication | case 'alpha': |
|
| 438 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 439 | $attributeData['value'] = $faker->word; |
||
| 440 | break; |
||
| 441 | case 'alpha_dash': |
||
| 442 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 443 | break; |
||
| 444 | case 'alpha_num': |
||
| 445 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 446 | break; |
||
| 447 | View Code Duplication | case 'in': |
|
| 448 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 449 | $attributeData['value'] = $faker->randomElement($parameters); |
||
| 450 | break; |
||
| 451 | View Code Duplication | case 'not_in': |
|
| 452 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 453 | $attributeData['value'] = $faker->word; |
||
| 454 | break; |
||
| 455 | View Code Duplication | case 'min': |
|
| 456 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 457 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 458 | $attributeData['value'] = $faker->numberBetween($parameters[0]); |
||
| 459 | } |
||
| 460 | break; |
||
| 461 | View Code Duplication | case 'max': |
|
| 462 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 463 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 464 | $attributeData['value'] = $faker->numberBetween(0, $parameters[0]); |
||
| 465 | } |
||
| 466 | break; |
||
| 467 | View Code Duplication | case 'between': |
|
| 468 | if (! isset($attributeData['type'])) { |
||
| 469 | $attributeData['type'] = 'numeric'; |
||
| 470 | } |
||
| 471 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 472 | $attributeData['value'] = $faker->numberBetween($parameters[0], $parameters[1]); |
||
| 473 | break; |
||
| 474 | View Code Duplication | case 'before': |
|
| 475 | $attributeData['type'] = 'date'; |
||
| 476 | $format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850; |
||
| 477 | |||
| 478 | if (strtotime($parameters[0]) === false) { |
||
| 479 | // the `before` date refers to another parameter in the request |
||
| 480 | $paramName = $parameters[0]; |
||
| 481 | $attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription(); |
||
| 482 | $attributeData['value'] = date($format, strtotime('-1 day', strtotime($routeData['parameters'][$paramName]['value']))); |
||
| 483 | } else { |
||
| 484 | $attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription(); |
||
| 485 | $attributeData['value'] = date($format, strtotime('-1 day', strtotime($parameters[0]))); |
||
| 486 | } |
||
| 487 | break; |
||
| 488 | View Code Duplication | case 'date_format': |
|
| 489 | $attributeData['type'] = 'date'; |
||
| 490 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 491 | $attributeData['format'] = $parameters[0]; |
||
| 492 | $attributeData['value'] = date($attributeData['format']); |
||
| 493 | break; |
||
| 494 | case 'different': |
||
| 495 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 496 | break; |
||
| 497 | case 'digits': |
||
| 498 | $attributeData['type'] = 'numeric'; |
||
| 499 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 500 | $attributeData['value'] = ($parameters[0] < 9) ? $faker->randomNumber($parameters[0], true) : substr(mt_rand(100000000, mt_getrandmax()), 0, $parameters[0]); |
||
| 501 | break; |
||
| 502 | case 'digits_between': |
||
| 503 | $attributeData['type'] = 'numeric'; |
||
| 504 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 505 | break; |
||
| 506 | View Code Duplication | case 'file': |
|
| 507 | $attributeData['type'] = 'file'; |
||
| 508 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 509 | break; |
||
| 510 | View Code Duplication | case 'image': |
|
| 511 | $attributeData['type'] = 'image'; |
||
| 512 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 513 | break; |
||
| 514 | case 'json': |
||
| 515 | $attributeData['type'] = 'string'; |
||
| 516 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 517 | $attributeData['value'] = json_encode(['foo', 'bar', 'baz']); |
||
| 518 | break; |
||
| 519 | case 'mimetypes': |
||
| 520 | View Code Duplication | case 'mimes': |
|
| 521 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 522 | break; |
||
| 523 | View Code Duplication | case 'required_if': |
|
| 524 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 525 | break; |
||
| 526 | View Code Duplication | case 'required_unless': |
|
| 527 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 528 | break; |
||
| 529 | View Code Duplication | case 'required_with': |
|
| 530 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 531 | break; |
||
| 532 | View Code Duplication | case 'required_with_all': |
|
| 533 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 534 | break; |
||
| 535 | View Code Duplication | case 'required_without': |
|
| 536 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 537 | break; |
||
| 538 | View Code Duplication | case 'required_without_all': |
|
| 539 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 540 | break; |
||
| 541 | case 'same': |
||
| 542 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 543 | break; |
||
| 544 | case 'size': |
||
| 545 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 546 | break; |
||
| 547 | View Code Duplication | case 'timezone': |
|
| 548 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 549 | $attributeData['value'] = $faker->timezone; |
||
| 550 | break; |
||
| 551 | case 'exists': |
||
| 552 | $fieldName = isset($parameters[1]) ? $parameters[1] : $attribute; |
||
| 553 | $attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $fieldName])->getDescription(); |
||
| 554 | break; |
||
| 555 | case 'active_url': |
||
| 556 | $attributeData['type'] = 'url'; |
||
| 557 | $attributeData['value'] = $faker->url; |
||
| 558 | break; |
||
| 559 | case 'regex': |
||
| 560 | $attributeData['type'] = 'string'; |
||
| 561 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 562 | break; |
||
| 563 | case 'boolean': |
||
| 564 | $attributeData['value'] = true; |
||
| 565 | $attributeData['type'] = $rule; |
||
| 566 | break; |
||
| 567 | case 'array': |
||
| 568 | $attributeData['value'] = $faker->word; |
||
| 569 | $attributeData['type'] = $rule; |
||
| 570 | break; |
||
| 571 | case 'date': |
||
| 572 | $attributeData['value'] = $faker->date(); |
||
| 573 | $attributeData['type'] = $rule; |
||
| 574 | break; |
||
| 575 | case 'email': |
||
| 576 | $attributeData['value'] = $faker->safeEmail; |
||
| 577 | $attributeData['type'] = $rule; |
||
| 578 | break; |
||
| 579 | case 'string': |
||
| 580 | $attributeData['value'] = $faker->word; |
||
| 581 | $attributeData['type'] = $rule; |
||
| 582 | break; |
||
| 583 | case 'integer': |
||
| 584 | $attributeData['value'] = $faker->randomNumber(); |
||
| 585 | $attributeData['type'] = $rule; |
||
| 586 | break; |
||
| 587 | case 'numeric': |
||
| 588 | $attributeData['value'] = $faker->randomNumber(); |
||
| 589 | $attributeData['type'] = $rule; |
||
| 590 | break; |
||
| 591 | case 'url': |
||
| 592 | $attributeData['value'] = $faker->url; |
||
| 593 | $attributeData['type'] = $rule; |
||
| 594 | break; |
||
| 595 | case 'ip': |
||
| 596 | $attributeData['value'] = $faker->ipv4; |
||
| 597 | $attributeData['type'] = $rule; |
||
| 598 | break; |
||
| 599 | default: |
||
| 600 | $unknownRuleDescription = Description::parse($rule)->getDescription(); |
||
| 601 | if ($unknownRuleDescription) { |
||
| 602 | $attributeData['description'][] = $unknownRuleDescription; |
||
| 603 | } |
||
| 604 | break; |
||
| 605 | } |
||
| 606 | |||
| 607 | if ($attributeData['value'] === '') { |
||
| 608 | $attributeData['value'] = $faker->word; |
||
| 609 | } |
||
| 610 | |||
| 611 | if (is_null($attributeData['type'])) { |
||
| 612 | $attributeData['type'] = 'string'; |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 827 |
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.