| Conditions | 15 |
| Paths | 1 |
| Total Lines | 126 |
| Code Lines | 64 |
| Lines | 78 |
| Ratio | 61.9 % |
| 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 |
||
| 15 | public static function addrules() |
||
| 16 | { |
||
| 17 | \Valitron\Validator::addRule('street_address', function ($field, $value, array $params, array $fields) { |
||
| 18 | return !preg_match('/^(Private Bag X\d+|P\.?O\.?\s?Box\s\d+).*$/i', $value); |
||
| 19 | }, 'must be a physical street address'); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Validate the possibility of a mobile number being a valid Namibian Mobile Number. |
||
| 23 | */ |
||
| 24 | View Code Duplication | \Valitron\Validator::addRule('na_mobile_number', function ($field, $value, array $params, array $fields) { |
|
| 25 | $msisdn = str_replace(array(' ', '-', '(', ')'), '', $value); |
||
| 26 | $country = 'NA'; |
||
| 27 | |||
| 28 | $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); |
||
| 29 | try { |
||
| 30 | $phoneNumberProto = $phoneUtil->parse($msisdn, $country); |
||
| 31 | } catch (\libphonenumber\NumberParseException $e) { |
||
| 32 | return false; |
||
| 33 | } |
||
| 34 | |||
| 35 | if (!$phoneUtil->isValidNumber($phoneNumberProto)) { |
||
| 36 | return false; |
||
| 37 | } |
||
| 38 | |||
| 39 | if ( |
||
| 40 | !in_array( |
||
| 41 | $phoneUtil->getNumberType($phoneNumberProto), |
||
| 42 | [ |
||
| 43 | 1, |
||
| 44 | 2, |
||
| 45 | ] |
||
| 46 | ) |
||
| 47 | ) { |
||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | if ( |
||
| 52 | !is_null($country) |
||
| 53 | ) { |
||
| 54 | if ($country == $phoneUtil->getRegionCodeForNumber($phoneNumberProto)) { |
||
| 55 | return true; |
||
| 56 | } |
||
| 57 | |||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | return true; |
||
| 62 | }, 'must be a valid Namibian Mobile Number'); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Validate the possibility of the South African Identity Number beng a valid South |
||
| 66 | * African identity number. |
||
| 67 | */ |
||
| 68 | \Valitron\Validator::addRule('za_identity_number', function ($field, $value, array $params, array $fields) { |
||
| 69 | if (!ctype_digit($value)) { |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | $match = preg_match("!^(\d{2})(\d{2})(\d{2})\d\d{6}$!", $value, $matches); |
||
| 74 | if (!$match) { |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | list(, $year, $month, $day) = $matches; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Check citizenship of the users id (0 = .za, 1 = permanent resident) |
||
| 81 | */ |
||
| 82 | if (!in_array($value{10}, array(0, 1))) { |
||
| 83 | return false; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Seen 8 or 9 here. |
||
| 88 | */ |
||
| 89 | if (!in_array($value{11}, [8, 9])) { |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | $idvalid = \PayBreak\Luhn\Luhn::validateNumber($value); |
||
| 94 | |||
| 95 | return ($idvalid); |
||
| 96 | }, 'must be a valid South African Identity Number'); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Validate the possibility of a mobile number being a valid South African Mobile Number. |
||
| 100 | */ |
||
| 101 | View Code Duplication | \Valitron\Validator::addRule('za_mobile_number', function ($field, $value, array $params, array $fields) { |
|
| 102 | $msisdn = str_replace(array(' ', '-', '(', ')'), '', $value); |
||
| 103 | $country = 'ZA'; |
||
| 104 | |||
| 105 | $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); |
||
| 106 | try { |
||
| 107 | $phoneNumberProto = $phoneUtil->parse($msisdn, $country); |
||
| 108 | } catch (\libphonenumber\NumberParseException $e) { |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (!$phoneUtil->isValidNumber($phoneNumberProto)) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | if ( |
||
| 117 | !in_array( |
||
| 118 | $phoneUtil->getNumberType($phoneNumberProto), |
||
| 119 | [ |
||
| 120 | 1, |
||
| 121 | 2, |
||
| 122 | ] |
||
| 123 | ) |
||
| 124 | ) { |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( |
||
| 129 | !is_null($country) |
||
| 130 | ) { |
||
| 131 | if ($country == $phoneUtil->getRegionCodeForNumber($phoneNumberProto)) { |
||
| 132 | return true; |
||
| 133 | } |
||
| 134 | |||
| 135 | return false; |
||
| 136 | } |
||
| 137 | |||
| 138 | return true; |
||
| 139 | }, 'must be a valid South African Mobile Number'); |
||
| 140 | } |
||
| 141 | } |
||
| 142 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.