PhoneValidator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 87
ccs 24
cts 27
cp 0.8889
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C validatePhone() 0 71 14
1
<?php
2
/**
3
 * PhoneValidator.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:FormPhone!
9
 * @subpackage     Forms
10
 * @since          1.0.0
11
 *
12
 * @date           12.12.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\FormPhone\Forms;
18
19
use Nette;
20
use Nette\Forms;
21
22
use libphonenumber;
23
use libphonenumber\PhoneNumberUtil;
24
25
use IPub\FormPhone;
26
use IPub\FormPhone\Controls;
27
use IPub\FormPhone\Exceptions;
28
29
use IPub\Phone;
30
use Tracy\Debugger;
31
32
/**
33
 * Phone number control form field validator
34
 *
35
 * @package        iPublikuj:FormPhone!
36
 * @subpackage     Forms
37
 *
38
 * @author         Adam Kadlec <[email protected]>
39
 */
40 1
class PhoneValidator extends Phone\Forms\PhoneValidator
41
{
42
	/**
43
	 * Define validator calling constant
44
	 */
45
	const PHONE = 'IPub\FormPhone\Forms\PhoneValidator::validatePhone';
46
47
	/**
48
	 * @param Forms\IControl $control
49
	 * @param array|NULL $params
50
	 *
51
	 * @return bool
52
	 *
53
	 * @throws Exceptions\NoValidCountryException
54
	 */
55
	public static function validatePhone(Forms\IControl $control, $params = []) : bool
56
	{
57 1
		if (!$control instanceof Controls\Phone) {
58 1
			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 1
		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 1
			$value = Phone\Entities\Phone::fromNumber($control->getValuePart(Controls\Phone::FIELD_NUMBER), $control->getValuePart(Controls\Phone::FIELD_COUNTRY));
68
69 1
		} catch (Phone\Exceptions\NoValidCountryException $ex) {
0 ignored issues
show
Bug introduced by
The class IPub\Phone\Exceptions\NoValidCountryException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
70
			return FALSE;
71
72 1
		} catch (Phone\Exceptions\NoValidPhoneException $ex) {
0 ignored issues
show
Bug introduced by
The class IPub\Phone\Exceptions\NoValidPhoneException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
73 1
			return FALSE;
74
		}
75
76
		// Value have to be phone entity
77 1
		if ($value instanceof Phone\Entities\Phone) {
0 ignored issues
show
Bug introduced by
The class IPub\Phone\Entities\Phone does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
78 1
			$number = $value->getRawOutput();
79
80
			// Get instance of phone number util
81 1
			$phoneNumberUtil = PhoneNumberUtil::getInstance();
82
83
			// Get list of allowed countries from params
84 1
			$allowedCountries = self::determineCountries($control->getAllowedCountries());
85
86
			// Get list of allowed phone types
87 1
			$allowedTypes = self::determineTypes($control->getAllowedPhoneTypes());
88
89
			// Perform validation
90 1
			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 1
					$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 1
						$phoneProto->hasCountryCode() &&
101 1
						$allowedTypes === [] ||
102 1
						in_array($phoneNumberUtil->getNumberType($phoneProto), $allowedTypes)
103
					) {
104
						// Automatic detection:
105 1
						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 1
						if ($phoneNumberUtil->isValidNumberForRegion($phoneProto, $country)) {
113 1
							return TRUE;
114
						}
115
					}
116
117 1
				} catch (libphonenumber\NumberParseException $ex) {
0 ignored issues
show
Bug introduced by
The class libphonenumber\NumberParseException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
118
					// Proceed to default validation error
119
				}
120
			}
121
		}
122
123
		// All specified country validations have failed
124 1
		return FALSE;
125
	}
126
}
127