| Conditions | 14 |
| Paths | 8 |
| Total Lines | 71 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 55 | public static function validatePhone(Forms\IControl $control, $params = []) : bool |
||
| 56 | { |
||
| 57 | if (!$control instanceof Controls\Phone) { |
||
| 58 | throw new Exceptions\InvalidArgumentException(sprintf('This validator could be used only on text field. You used it on: "%s"', get_class($control))); |
||
| 59 | } |
||
| 60 | |||
| 61 | if ($control->getValuePart(Controls\Phone::FIELD_NUMBER) === NULL || $control->getValuePart(Controls\Phone::FIELD_COUNTRY) === NULL) { |
||
| 62 | return TRUE; |
||
| 63 | } |
||
| 64 | |||
| 65 | try { |
||
| 66 | // Create phone entity |
||
| 67 | $value = Phone\Entities\Phone::fromNumber($control->getValuePart(Controls\Phone::FIELD_NUMBER), $control->getValuePart(Controls\Phone::FIELD_COUNTRY)); |
||
| 68 | |||
| 69 | } catch (Phone\Exceptions\NoValidCountryException $ex) { |
||
| 70 | return FALSE; |
||
| 71 | |||
| 72 | } catch (Phone\Exceptions\NoValidPhoneException $ex) { |
||
| 73 | return FALSE; |
||
| 74 | } |
||
| 75 | |||
| 76 | // Value have to be phone entity |
||
| 77 | if ($value instanceof Phone\Entities\Phone) { |
||
| 78 | $number = $value->getRawOutput(); |
||
| 79 | |||
| 80 | // Get instance of phone number util |
||
| 81 | $phoneNumberUtil = PhoneNumberUtil::getInstance(); |
||
| 82 | |||
| 83 | // Get list of allowed countries from params |
||
| 84 | $allowedCountries = self::determineCountries($control->getAllowedCountries()); |
||
| 85 | |||
| 86 | // Get list of allowed phone types |
||
| 87 | $allowedTypes = self::determineTypes($control->getAllowedPhoneTypes()); |
||
| 88 | |||
| 89 | // Perform validation |
||
| 90 | foreach ($allowedCountries as $country) { |
||
| 91 | try { |
||
| 92 | // For default countries or country field, the following throws NumberParseException if |
||
| 93 | // not parsed correctly against the supplied country |
||
| 94 | // For automatic detection: tries to discover the country code using from the number itself |
||
| 95 | $phoneProto = $phoneNumberUtil->parse($number, $country); |
||
| 96 | |||
| 97 | // For automatic detection, the number should have a country code |
||
| 98 | // Check if type is allowed |
||
| 99 | if ( |
||
| 100 | $phoneProto->hasCountryCode() && |
||
| 101 | $allowedTypes === [] || |
||
| 102 | in_array($phoneNumberUtil->getNumberType($phoneProto), $allowedTypes) |
||
| 103 | ) { |
||
| 104 | // Automatic detection: |
||
| 105 | if ($country == 'ZZ') { |
||
| 106 | // Validate if the international phone number is valid for its contained country |
||
| 107 | return $phoneNumberUtil->isValidNumber($phoneProto); |
||
| 108 | } |
||
| 109 | |||
| 110 | // Validate number against the specified country. Return only if success |
||
| 111 | // If failure, continue loop to next specified country |
||
| 112 | if ($phoneNumberUtil->isValidNumberForRegion($phoneProto, $country)) { |
||
| 113 | return TRUE; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | } catch (libphonenumber\NumberParseException $ex) { |
||
| 118 | // Proceed to default validation error |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | // All specified country validations have failed |
||
| 124 | return FALSE; |
||
| 125 | } |
||
| 126 | } |
||
| 127 |