Complex classes like Component 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 Component, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Component |
||
| 8 | { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Custom validation rules that you would like to apply. |
||
| 12 | * |
||
| 13 | * @var callable[] |
||
| 14 | */ |
||
| 15 | public $rules = array(); |
||
| 16 | |||
| 17 | |||
| 18 | /** |
||
| 19 | * Before you check if ``$this->certified()``, these are the error messages associated with each validation rule. After ``$this->certified()``, these are all the errors we encountered (if any). You can customize and add as you see fit. |
||
| 20 | * |
||
| 21 | * @var string[] |
||
| 22 | */ |
||
| 23 | public $errors = array( |
||
| 24 | 'remote' => 'Please fix this field.', |
||
| 25 | 'required' => 'This field is required.', |
||
| 26 | 'equalTo' => 'Please enter the same value again.', |
||
| 27 | 'notEqualTo' => 'Please enter a different value, values must not be the same.', |
||
| 28 | 'number' => 'Please enter a valid number.', |
||
| 29 | 'integer' => 'A positive or negative non-decimal number please.', |
||
| 30 | 'digits' => 'Please enter only digits.', |
||
| 31 | 'min' => 'Please enter a value greater than or equal to {0}.', |
||
| 32 | 'max' => 'Please enter a value less than or equal to {0}.', |
||
| 33 | 'range' => 'Please enter a value between {0} and {1}.', |
||
| 34 | 'alphaNumeric' => 'Letters, numbers, and underscores only please.', |
||
| 35 | 'minLength' => 'Please enter at least {0} characters.', |
||
| 36 | 'maxLength' => 'Please enter no more than {0} characters.', |
||
| 37 | 'rangeLength' => 'Please enter a value between {0} and {1} characters long.', |
||
| 38 | 'minWords' => 'Please enter at least {0} words.', |
||
| 39 | 'maxWords' => 'Please enter {0} words or less.', |
||
| 40 | 'rangeWords' => 'Please enter between {0} and {1} words.', |
||
| 41 | 'pattern' => 'Invalid format.', |
||
| 42 | 'date' => 'Please enter a valid date.', |
||
| 43 | 'email' => 'Please enter a valid email address.', |
||
| 44 | 'url' => 'Please enter a valid URL.', |
||
| 45 | 'ipv4' => 'Please enter a valid IP v4 address.', |
||
| 46 | 'ipv6' => 'Please enter a valid IP v6 address.', |
||
| 47 | 'inList' => 'Please make a valid selection.', |
||
| 48 | 'noWhiteSpace' => 'No white space please.', |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * This is where we save all of the information ``$this->set()``ed for each field. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $data = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * These are the user submitted values for each field. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $values = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Whether the form has been submitted or not. Null if we don't know. |
||
| 67 | * |
||
| 68 | * @var null|bool |
||
| 69 | */ |
||
| 70 | protected $submitted = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Either false or an array of all the submitted values. |
||
| 74 | * |
||
| 75 | * @var false|array |
||
| 76 | */ |
||
| 77 | protected $certified = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The rules we reserve for validation until the end. |
||
| 81 | * |
||
| 82 | * @var string[] |
||
| 83 | */ |
||
| 84 | protected $reserved = array('default', 'required', 'equalTo', 'notEqualTo'); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The rules we define in-house. |
||
| 88 | * |
||
| 89 | * @var string[] |
||
| 90 | */ |
||
| 91 | protected $methods = array('number', 'integer', 'digits', 'min', 'max', 'range', 'alphaNumeric', 'minLength', 'maxLength', 'rangeLength', 'minWords', 'maxWords', 'rangeWords', 'pattern', 'date', 'email', 'url', 'ipv4', 'ipv6', 'inList', 'yesNo', 'trueFalse', 'noWhiteSpace', 'singleSpace'); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * So that we have something to work with no matter what happens to ``$this->errors`` (anything can happen) public property. |
||
| 95 | * |
||
| 96 | * @var string[] |
||
| 97 | */ |
||
| 98 | protected $default_errors = array(); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Creates a BootPress\Validator\Component object. |
||
| 102 | * |
||
| 103 | * @param array $values The user submitted variables you want to validate. |
||
| 104 | * |
||
| 105 | * ```php |
||
| 106 | * $validator = new Validator($_POST); |
||
| 107 | * ``` |
||
| 108 | */ |
||
| 109 | 19 | public function __construct(array $values = array()) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * This allows you to set individually (or all at once) the validation rules and filters for each form field. The value of every $field you set here is automatically ``trim()``ed and returned when ``$this->submittted()``. |
||
| 117 | * |
||
| 118 | * @param string $field The name of your form field. If this is an ``array($field => $rules, $field, ...)`` then we loop through each one and call this method ourselves over and over. |
||
| 119 | * |
||
| 120 | * Your $field names can be an array by adding brackets to the end ie. 'name[]'. They can also be multi-dimensional arrays such as 'name[first]', or 'name[players][]', or 'name[parent][child]', etc. The important thing to remember is that you must always use the exact name given here when referencing them in other methods. |
||
| 121 | * |
||
| 122 | * @param string|array $rules A pipe delimited set (or an array) of rules to validate and filter the $field with. You can also specify custom messages by making this an ``array($rule => $message, ...)``. Parameters are comma-delimited, and placed within '**[]**' two brackets. The available options are: |
||
| 123 | * |
||
| 124 | * - '**remote[rule]**' - Set ``$form->rules['rule'] = function($value){}`` to determine the validity of a submitted value. The function should return a boolean true or false. |
||
| 125 | * - '**default**' - A default value if the field is empty, or not even set. |
||
| 126 | * - '**required**' - This field must have a value, and cannot be empty. |
||
| 127 | * - '**equalTo[field]**' - Must match the same value as contained in the other form field. |
||
| 128 | * - '**notEqualTo[field]**' - Must NOT match the same value as contained in the other form field. |
||
| 129 | * - Numbers: |
||
| 130 | * - '**number**' - Must be a valid decimal number, positive or negative, integer or float, commas okay. Defaults to 0. |
||
| 131 | * - '**integer**' - Must be a postive or negative integer number, no commas. Defaults to 0. |
||
| 132 | * - '**digits**' - Must be a positive integer number, no commas. Defaults to 0. |
||
| 133 | * - '**min[number]**' - Must be greater than or equal to [number]. |
||
| 134 | * - '**max[number]**' - Must be less than or equal to [number]. |
||
| 135 | * - '**range[min, max]**' - Must be greater than or equal to [min], and less than or equal to [max]. |
||
| 136 | * - Strings: |
||
| 137 | * - '**alphaNumeric**' - Alpha (a-z), numeric (0-9), and underscore (_) characters only. |
||
| 138 | * - '**minLength[integer]**' - String length must be greater than or equal to [integer]. |
||
| 139 | * - '**maxLength[integer]**' - String length must be less than or equal to [integer]. |
||
| 140 | * - '**rangeLength[minLength, maxLength]**' - String length must be greater than or equal to [minLength], and less than or equal to [maxLength]. |
||
| 141 | * - '**minWords[integer]**' - Number of words must be greater than or equal to [integer]. |
||
| 142 | * - '**maxWords[integer]**' - Number of words must be less than or equal to [integer]. |
||
| 143 | * - '**rangeWords[minWords, maxWords]**' - Number of words must be greater than or equal to [minWords], and less than or equal to [maxWords]. |
||
| 144 | * - '**pattern[regex]**' - Must match the supplied [ECMA Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) compatible [regex]. |
||
| 145 | * - '**date**' - Must be a valid looking date. No particular format is enforced. |
||
| 146 | * - '**email**' - Must be a valid looking email. |
||
| 147 | * - '**url**' - Must be a valid looking url. |
||
| 148 | * - '**ipv4**' - Must be a valid looking ipv4 address. |
||
| 149 | * - '**ipv6**' - Must be a valid looking ipv6 address. |
||
| 150 | * - '**inList[1,2,3]**' - Must be one of a comma-separated list of acceptable values. |
||
| 151 | * - '**noWhiteSpace**' - Must contain no white space. |
||
| 152 | * - Filters: |
||
| 153 | * - '**singleSpace**' - Removes any doubled-up whitespace so that you only have single spaces between words. |
||
| 154 | * - '**trueFalse**' - Returns a **1** (true) or **0** (false) integer. |
||
| 155 | * - '**yesNo**' - Returns a '**Y**' or '**N**' value. |
||
| 156 | * |
||
| 157 | * ```php |
||
| 158 | * $validator->set('name', 'required'); |
||
| 159 | * |
||
| 160 | * $validator->set('email', 'required|email'); |
||
| 161 | * |
||
| 162 | * $validator->set(array( |
||
| 163 | * 'password' => 'required|alphaNumeric|minLength[5]|noWhiteSpace', |
||
| 164 | * 'confim' => array('required', 'matches[password]'), |
||
| 165 | * )); |
||
| 166 | * |
||
| 167 | * $validator->set('field', array('required' => 'Do this or else.')); |
||
| 168 | * ``` |
||
| 169 | */ |
||
| 170 | 11 | public function set($field, $rules = '') |
|
| 350 | |||
| 351 | /** |
||
| 352 | * This method goes through all of the fields you set above, determines if the form has been sent, and picks out any errors. |
||
| 353 | * |
||
| 354 | * @return false|array Returns an array of validated and filtered form values for every ``$validator->set('field')`` IF the form was submitted (ie. at least one field has it's $_GET or $_POST counterpart), AND there were no errors. |
||
| 355 | * |
||
| 356 | * ```php |
||
| 357 | * if ($vars = $validator->certified()) { |
||
| 358 | * // process $vars |
||
| 359 | * } |
||
| 360 | * ``` |
||
| 361 | */ |
||
| 362 | 3 | public function certified() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Allows you to know if a form $field has been required, or not. |
||
| 412 | * |
||
| 413 | * @param string $field |
||
| 414 | * |
||
| 415 | * @return bool Whether the field is required or not. |
||
| 416 | */ |
||
| 417 | 1 | public function required($field) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Returns the submitted value of the $field that should be used when displaying the form. |
||
| 424 | * |
||
| 425 | * The array feature comes in handy when you want to save the values to your database. |
||
| 426 | * |
||
| 427 | * @param string|array $field |
||
| 428 | * |
||
| 429 | * @return mixed |
||
| 430 | */ |
||
| 431 | 10 | public function value($field) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Returns an error message (if any) that should be used when displaying the form. |
||
| 447 | * |
||
| 448 | * @param string $field |
||
| 449 | * |
||
| 450 | * @return null|string |
||
| 451 | */ |
||
| 452 | 2 | public function error($field) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Returns all of the rules set up for the $field. |
||
| 459 | * |
||
| 460 | * @param string $field |
||
| 461 | * |
||
| 462 | * @return string[] |
||
| 463 | * |
||
| 464 | * ```php |
||
| 465 | * foreach ($validator->rules($field) as $validate => $param) { |
||
| 466 | * $attributes["data-rule-{$validate}"] = htmlspecialchars($param); |
||
| 467 | * } |
||
| 468 | * ``` |
||
| 469 | */ |
||
| 470 | 11 | public function rules($field) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Returns a $field's rules and associated error messages. |
||
| 477 | * |
||
| 478 | * @param string $field |
||
| 479 | * |
||
| 480 | * @return string[] |
||
| 481 | * |
||
| 482 | * ```php |
||
| 483 | * foreach ($validator->messages($field) as $rule => $message) { |
||
| 484 | * $attributes["data-msg-{$rule}"] = htmlspecialchars($message); |
||
| 485 | * } |
||
| 486 | * ``` |
||
| 487 | */ |
||
| 488 | 11 | public function messages($field) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Includes Jörn's jQuery Validation code that this component was meant to sync perfectly with. |
||
| 495 | * |
||
| 496 | * @param string $form The jQuery identifier of your form. |
||
| 497 | * @param string[] $options The rules and custom messages should be added to each inputs data-... attributes using ``$this->rules()`` and ``$this->messages()``. Any other fine-tuning can be done here. The $options values must be pre json encoded ie. quotes around strings ('"string"'), brackets for arrays ('[]'), quoted bools ('false'). The reason for this is because we cannot json_encode functions properly ('function(){}'). |
||
| 498 | * |
||
| 499 | * ```php |
||
| 500 | * $validator->jquery('#form', array('debug'=>'true')); |
||
| 501 | * ``` |
||
| 502 | * |
||
| 503 | * @see https://jqueryvalidation.org/validate/ |
||
| 504 | */ |
||
| 505 | 2 | public function jquery($form, array $options = array()) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Returns the unique id assigned to the $field. |
||
| 518 | * |
||
| 519 | * @param string $field |
||
| 520 | * |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | 11 | public function id($field) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Determines if the $value is a valid decimal number. Can be positive or negative, integer or float, and commas to separate thousandths are okay. |
||
| 535 | * |
||
| 536 | * @param string $value |
||
| 537 | * |
||
| 538 | * @return bool |
||
| 539 | */ |
||
| 540 | 1 | public static function number($value) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Deterimes if the $value is a positive or negative integer number, no commas. |
||
| 547 | * |
||
| 548 | * @param string $value |
||
| 549 | * |
||
| 550 | * @return bool |
||
| 551 | */ |
||
| 552 | 1 | public static function integer($value) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Deterimes if the $value is a positive integer number, no commas. |
||
| 559 | * |
||
| 560 | * @param string $value |
||
| 561 | * |
||
| 562 | * @return bool |
||
| 563 | */ |
||
| 564 | 1 | public static function digits($value) |
|
| 568 | |||
| 569 | /** |
||
| 570 | * Determines if the $value is greater than or equal to $param. |
||
| 571 | * |
||
| 572 | * @param float $value |
||
| 573 | * @param float $param |
||
| 574 | * |
||
| 575 | * @return bool |
||
| 576 | */ |
||
| 577 | 2 | public static function min($value, $param) |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Determines if the $value is less than or equal to $param. |
||
| 584 | * |
||
| 585 | * @param float $value |
||
| 586 | * @param float $param |
||
| 587 | * |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | 1 | public static function max($value, $param) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Determines if the $value is greater than or equal to $param[0], and less than or equal to $param[1]. |
||
| 597 | * |
||
| 598 | * @param float $value |
||
| 599 | * @param float[] $param |
||
| 600 | * |
||
| 601 | * @return bool |
||
| 602 | */ |
||
| 603 | 1 | public static function range($value, array $param) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Deterimes if the $value has alpha (a-z), numeric (0-9), and underscore (_) characters only. |
||
| 610 | * |
||
| 611 | * @param string $value |
||
| 612 | * |
||
| 613 | * @return bool |
||
| 614 | */ |
||
| 615 | 1 | public static function alphaNumeric($value) |
|
| 619 | |||
| 620 | /** |
||
| 621 | * Determines if the $value's length is greater than or equal to $param. |
||
| 622 | * |
||
| 623 | * @param string $value |
||
| 624 | * @param int $param |
||
| 625 | * |
||
| 626 | * @return bool |
||
| 627 | */ |
||
| 628 | 2 | public static function minLength($value, $param) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Determines if the $value's length is less than or equal to $param. |
||
| 637 | * |
||
| 638 | * @param string $value |
||
| 639 | * @param int $param |
||
| 640 | * |
||
| 641 | * @return bool |
||
| 642 | */ |
||
| 643 | 1 | public static function maxLength($value, $param) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Determines if the $value's length is greater than or equal to $param[0], and less than or equal to $param[1]. |
||
| 652 | * |
||
| 653 | * @param string $value |
||
| 654 | * @param int[] $param |
||
| 655 | * |
||
| 656 | * @return bool |
||
| 657 | */ |
||
| 658 | 1 | public static function rangeLength($value, array $param) |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Determines if the number of $value's words are greater than or equal to $param. |
||
| 667 | * |
||
| 668 | * @param string $value |
||
| 669 | * @param int $param |
||
| 670 | * |
||
| 671 | * @return bool |
||
| 672 | */ |
||
| 673 | 1 | public static function minWords($value, $param) |
|
| 679 | |||
| 680 | /** |
||
| 681 | * Determines if the number of $value's words are less than or equal to $param. |
||
| 682 | * |
||
| 683 | * @param string $value |
||
| 684 | * @param int $param |
||
| 685 | * |
||
| 686 | * @return bool |
||
| 687 | */ |
||
| 688 | 1 | public static function maxWords($value, $param) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Determines if the number of $value's words are greater than or equal to $param[0], and less than or equal to $param[1]. |
||
| 697 | * |
||
| 698 | * @param string $value |
||
| 699 | * @param int[] $param |
||
| 700 | * |
||
| 701 | * @return bool |
||
| 702 | */ |
||
| 703 | 1 | public static function rangeWords($value, array $param) |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Determines if the $value matches the supplied regex ($param). |
||
| 712 | * |
||
| 713 | * @param string $value |
||
| 714 | * @param string $param |
||
| 715 | * |
||
| 716 | * @return bool |
||
| 717 | */ |
||
| 718 | 1 | public static function pattern($value, $param) |
|
| 722 | |||
| 723 | /** |
||
| 724 | * Determines if the $value is a parseable date. |
||
| 725 | * |
||
| 726 | * @param string $value |
||
| 727 | * |
||
| 728 | * @return bool |
||
| 729 | */ |
||
| 730 | 1 | public static function date($value) |
|
| 734 | |||
| 735 | /** |
||
| 736 | * Determines if the $value is a valid looking email. |
||
| 737 | * |
||
| 738 | * @param string $value |
||
| 739 | * |
||
| 740 | * @return bool |
||
| 741 | */ |
||
| 742 | 2 | public static function email($value) |
|
| 746 | |||
| 747 | /** |
||
| 748 | * Determines if the $value is a valid looking url. |
||
| 749 | * |
||
| 750 | * @param string $value |
||
| 751 | * |
||
| 752 | * @return bool |
||
| 753 | */ |
||
| 754 | 1 | public static function url($value) |
|
| 758 | |||
| 759 | /** |
||
| 760 | * Determines if the $value is a valid looking ipv4 address. |
||
| 761 | * |
||
| 762 | * @param string $value |
||
| 763 | * |
||
| 764 | * @return bool |
||
| 765 | */ |
||
| 766 | 1 | public static function ipv4($value) |
|
| 770 | |||
| 771 | /** |
||
| 772 | * Determines if the $value is a valid looking ipv6 address. |
||
| 773 | * |
||
| 774 | * @param string $value |
||
| 775 | * |
||
| 776 | * @return bool |
||
| 777 | */ |
||
| 778 | 1 | public static function ipv6($value) |
|
| 782 | |||
| 783 | /** |
||
| 784 | * Determines if the $value exists in the $param array. |
||
| 785 | * |
||
| 786 | * @param string $value |
||
| 787 | * @param array $param |
||
| 788 | * |
||
| 789 | * @return bool |
||
| 790 | */ |
||
| 791 | 2 | public static function inList($value, array $param) |
|
| 795 | |||
| 796 | /** |
||
| 797 | * Determines if the $value contains any white space. |
||
| 798 | * |
||
| 799 | * @param string $value |
||
| 800 | * |
||
| 801 | * @return bool |
||
| 802 | */ |
||
| 803 | 1 | public static function noWhiteSpace($value) |
|
| 807 | |||
| 808 | /** |
||
| 809 | * Removes any doubled-up whitespace from the $value. |
||
| 810 | * |
||
| 811 | * @param string $value |
||
| 812 | * |
||
| 813 | * @return string |
||
| 814 | */ |
||
| 815 | 2 | public static function singleSpace($value) |
|
| 819 | |||
| 820 | /** |
||
| 821 | * Returns a **1** (true) or **0** (false) integer depending on the $value. |
||
| 822 | * |
||
| 823 | * @param mixed $value |
||
| 824 | * |
||
| 825 | * @return int |
||
| 826 | */ |
||
| 827 | 1 | public static function trueFalse($value) |
|
| 828 | { |
||
| 829 | 1 | if ((is_numeric($value) && $value > 0) || strtoupper($value) == 'Y') { |
|
| 830 | 1 | return 1; |
|
| 831 | } |
||
| 832 | |||
| 833 | 1 | return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 1 : 0; |
|
| 834 | } |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Returns a '**Y**' or '**N**' string depending on the $value. |
||
| 838 | * |
||
| 839 | * @param mixed $value |
||
| 840 | * |
||
| 841 | * @return string |
||
| 842 | */ |
||
| 843 | 1 | public static function yesNo($value) |
|
| 844 | { |
||
| 845 | 1 | if ((is_numeric($value) && $value > 0) || strtoupper($value) == 'Y') { |
|
| 846 | 1 | return 'Y'; |
|
| 847 | } |
||
| 848 | |||
| 849 | 1 | return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'Y' : 'N'; |
|
| 850 | } |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Returns the number of words in $value. |
||
| 854 | * |
||
| 855 | * @param string $value |
||
| 856 | * |
||
| 857 | * @return integer |
||
| 858 | */ |
||
| 859 | 3 | private static function numWords($value) |
|
| 868 | |||
| 869 | /** |
||
| 870 | * A helper method to validate a $value via a callable $param. |
||
| 871 | * |
||
| 872 | * @param string $value |
||
| 873 | * @param string $param |
||
| 874 | * |
||
| 875 | * @return string |
||
| 876 | */ |
||
| 877 | 1 | private function remote($value, $param) |
|
| 886 | |||
| 887 | /** |
||
| 888 | * A helper method to determine the value in an array based on a string eg. 'array[value]' |
||
| 889 | * |
||
| 890 | * @param mixed $array |
||
| 891 | * @param array $indexes |
||
| 892 | * @param int $i |
||
| 893 | * |
||
| 894 | * @return mixed |
||
| 895 | */ |
||
| 896 | 2 | private function reduce($array, array $indexes, $i = 0) |
|
| 904 | |||
| 905 | /** |
||
| 906 | * A helper method to remove any reference to an array ie. 'array[value]' would be just 'array'. |
||
| 907 | * |
||
| 908 | * @param string $field |
||
| 909 | * |
||
| 910 | * @return string |
||
| 911 | */ |
||
| 912 | 4 | private function base($field) |
|
| 916 | |||
| 917 | /** |
||
| 918 | * A helper method to validate a string or an array of values based on it's $rule's and $param's. |
||
| 919 | * |
||
| 920 | * @param mixed $value |
||
| 921 | * @param string $rule |
||
| 922 | * @param mixed $param |
||
| 923 | * |
||
| 924 | * @return array The derived value, and error (if any). |
||
| 925 | */ |
||
| 926 | 1 | private function validate($value, $rule, $param) |
|
| 967 | |||
| 968 | /** |
||
| 969 | * A helper method to retrieve the associated error message when something goes wrong. |
||
| 970 | * |
||
| 971 | * @param string $rule |
||
| 972 | * @param mixed $param |
||
| 973 | * |
||
| 974 | * @return null|string |
||
| 975 | */ |
||
| 976 | 1 | private function errorMessage($rule, $param = null) |
|
| 988 | } |
||
| 989 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: