| 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 |
||
| 352 | protected function parseRule($rule, $ruleName, &$attributeData, $seed, $routeData) |
||
| 353 | { |
||
| 354 | $faker = Factory::create(); |
||
| 355 | $faker->seed(crc32($seed)); |
||
| 356 | |||
| 357 | $parsedRule = $this->parseStringRule($rule); |
||
| 358 | $parsedRule[0] = $this->normalizeRule($parsedRule[0]); |
||
| 359 | list($rule, $parameters) = $parsedRule; |
||
| 360 | |||
| 361 | switch ($rule) { |
||
| 362 | case 'required': |
||
| 363 | $attributeData['required'] = true; |
||
| 364 | break; |
||
| 365 | case 'accepted': |
||
| 366 | $attributeData['required'] = true; |
||
| 367 | $attributeData['type'] = 'boolean'; |
||
| 368 | $attributeData['value'] = true; |
||
| 369 | break; |
||
| 370 | View Code Duplication | case 'after': |
|
| 371 | $attributeData['type'] = 'date'; |
||
| 372 | $format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850; |
||
| 373 | |||
| 374 | if (strtotime($parameters[0]) === false) { |
||
| 375 | // the `after` date refers to another parameter in the request |
||
| 376 | $paramName = $parameters[0]; |
||
| 377 | $attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription(); |
||
| 378 | $attributeData['value'] = date($format, strtotime('+1 day', strtotime($routeData['parameters'][$paramName]['value']))); |
||
| 379 | } else { |
||
| 380 | $attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription(); |
||
| 381 | $attributeData['value'] = date($format, strtotime('+1 day', strtotime($parameters[0]))); |
||
| 382 | } |
||
| 383 | break; |
||
| 384 | View Code Duplication | case 'alpha': |
|
| 385 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 386 | $attributeData['value'] = $faker->word; |
||
| 387 | break; |
||
| 388 | case 'alpha_dash': |
||
| 389 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 390 | break; |
||
| 391 | case 'alpha_num': |
||
| 392 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 393 | break; |
||
| 394 | View Code Duplication | case 'in': |
|
| 395 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 396 | $attributeData['value'] = $faker->randomElement($parameters); |
||
| 397 | break; |
||
| 398 | View Code Duplication | case 'not_in': |
|
| 399 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 400 | $attributeData['value'] = $faker->word; |
||
| 401 | break; |
||
| 402 | View Code Duplication | case 'min': |
|
| 403 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 404 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 405 | $attributeData['value'] = $faker->numberBetween($parameters[0]); |
||
| 406 | } |
||
| 407 | break; |
||
| 408 | View Code Duplication | case 'max': |
|
| 409 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 410 | if (Arr::get($attributeData, 'type') === 'numeric' || Arr::get($attributeData, 'type') === 'integer') { |
||
| 411 | $attributeData['value'] = $faker->numberBetween(0, $parameters[0]); |
||
| 412 | } |
||
| 413 | break; |
||
| 414 | View Code Duplication | case 'between': |
|
| 415 | if (! isset($attributeData['type'])) { |
||
| 416 | $attributeData['type'] = 'numeric'; |
||
| 417 | } |
||
| 418 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 419 | $attributeData['value'] = $faker->numberBetween($parameters[0], $parameters[1]); |
||
| 420 | break; |
||
| 421 | View Code Duplication | case 'before': |
|
| 422 | $attributeData['type'] = 'date'; |
||
| 423 | $format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850; |
||
| 424 | |||
| 425 | if (strtotime($parameters[0]) === false) { |
||
| 426 | // the `before` date refers to another parameter in the request |
||
| 427 | $paramName = $parameters[0]; |
||
| 428 | $attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription(); |
||
| 429 | $attributeData['value'] = date($format, strtotime('-1 day', strtotime($routeData['parameters'][$paramName]['value']))); |
||
| 430 | } else { |
||
| 431 | $attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription(); |
||
| 432 | $attributeData['value'] = date($format, strtotime('-1 day', strtotime($parameters[0]))); |
||
| 433 | } |
||
| 434 | break; |
||
| 435 | View Code Duplication | case 'date_format': |
|
| 436 | $attributeData['type'] = 'date'; |
||
| 437 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 438 | $attributeData['format'] = $parameters[0]; |
||
| 439 | $attributeData['value'] = date($attributeData['format']); |
||
| 440 | break; |
||
| 441 | case 'different': |
||
| 442 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 443 | break; |
||
| 444 | case 'digits': |
||
| 445 | $attributeData['type'] = 'numeric'; |
||
| 446 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 447 | $attributeData['value'] = ($parameters[0] < 9) ? $faker->randomNumber($parameters[0], true) : substr(mt_rand(100000000, mt_getrandmax()), 0, $parameters[0]); |
||
| 448 | break; |
||
| 449 | case 'digits_between': |
||
| 450 | $attributeData['type'] = 'numeric'; |
||
| 451 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 452 | break; |
||
| 453 | View Code Duplication | case 'file': |
|
| 454 | $attributeData['type'] = 'file'; |
||
| 455 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 456 | break; |
||
| 457 | View Code Duplication | case 'image': |
|
| 458 | $attributeData['type'] = 'image'; |
||
| 459 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 460 | break; |
||
| 461 | case 'json': |
||
| 462 | $attributeData['type'] = 'string'; |
||
| 463 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 464 | $attributeData['value'] = json_encode(['foo', 'bar', 'baz']); |
||
| 465 | break; |
||
| 466 | case 'mimetypes': |
||
| 467 | View Code Duplication | case 'mimes': |
|
| 468 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 469 | break; |
||
| 470 | View Code Duplication | case 'required_if': |
|
| 471 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 472 | break; |
||
| 473 | View Code Duplication | case 'required_unless': |
|
| 474 | $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription(); |
||
| 475 | break; |
||
| 476 | View Code Duplication | case 'required_with': |
|
| 477 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 478 | break; |
||
| 479 | View Code Duplication | case 'required_with_all': |
|
| 480 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 481 | break; |
||
| 482 | View Code Duplication | case 'required_without': |
|
| 483 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription(); |
||
| 484 | break; |
||
| 485 | View Code Duplication | case 'required_without_all': |
|
| 486 | $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' and '))->getDescription(); |
||
| 487 | break; |
||
| 488 | case 'same': |
||
| 489 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 490 | break; |
||
| 491 | case 'size': |
||
| 492 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 493 | break; |
||
| 494 | View Code Duplication | case 'timezone': |
|
| 495 | $attributeData['description'][] = Description::parse($rule)->getDescription(); |
||
| 496 | $attributeData['value'] = $faker->timezone; |
||
| 497 | break; |
||
| 498 | case 'exists': |
||
| 499 | $fieldName = isset($parameters[1]) ? $parameters[1] : $ruleName; |
||
| 500 | $attributeData['description'][] = Description::parse($rule)->with([Str::singular($parameters[0]), $fieldName])->getDescription(); |
||
| 501 | break; |
||
| 502 | case 'active_url': |
||
| 503 | $attributeData['type'] = 'url'; |
||
| 504 | $attributeData['value'] = $faker->url; |
||
| 505 | break; |
||
| 506 | case 'regex': |
||
| 507 | $attributeData['type'] = 'string'; |
||
| 508 | $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription(); |
||
| 509 | break; |
||
| 510 | case 'boolean': |
||
| 511 | $attributeData['value'] = true; |
||
| 512 | $attributeData['type'] = $rule; |
||
| 513 | break; |
||
| 514 | case 'array': |
||
| 515 | $attributeData['value'] = $faker->word; |
||
| 516 | $attributeData['type'] = $rule; |
||
| 517 | break; |
||
| 518 | case 'date': |
||
| 519 | $attributeData['value'] = $faker->date(); |
||
| 520 | $attributeData['type'] = $rule; |
||
| 521 | break; |
||
| 522 | case 'email': |
||
| 523 | $attributeData['value'] = $faker->safeEmail; |
||
| 524 | $attributeData['type'] = $rule; |
||
| 525 | break; |
||
| 526 | case 'string': |
||
| 527 | $attributeData['value'] = $faker->word; |
||
| 528 | $attributeData['type'] = $rule; |
||
| 529 | break; |
||
| 530 | case 'integer': |
||
| 531 | $attributeData['value'] = $faker->randomNumber(); |
||
| 532 | $attributeData['type'] = $rule; |
||
| 533 | break; |
||
| 534 | case 'numeric': |
||
| 535 | $attributeData['value'] = $faker->randomNumber(); |
||
| 536 | $attributeData['type'] = $rule; |
||
| 537 | break; |
||
| 538 | case 'url': |
||
| 539 | $attributeData['value'] = $faker->url; |
||
| 540 | $attributeData['type'] = $rule; |
||
| 541 | break; |
||
| 542 | case 'ip': |
||
| 543 | $attributeData['value'] = $faker->ipv4; |
||
| 544 | $attributeData['type'] = $rule; |
||
| 545 | break; |
||
| 546 | default: |
||
| 547 | $unknownRuleDescription = Description::parse($rule)->getDescription(); |
||
| 548 | if ($unknownRuleDescription) { |
||
| 549 | $attributeData['description'][] = $unknownRuleDescription; |
||
| 550 | } |
||
| 551 | break; |
||
| 552 | } |
||
| 553 | |||
| 554 | if ($attributeData['value'] === '') { |
||
| 555 | $attributeData['value'] = $faker->word; |
||
| 556 | } |
||
| 557 | |||
| 558 | if (is_null($attributeData['type'])) { |
||
| 559 | $attributeData['type'] = 'string'; |
||
| 560 | } |
||
| 561 | } |
||
| 562 | |||
| 679 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: