Completed
Push — master ( 0d8f27...c602d8 )
by Tyler
03:09
created

Validator::isNANP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tylercd100\Validator\Phone;
4
5
class Validator
6
{
7 15
    public function __call($a, $b)
8
    {
9 15
        return empty($b[0]) || $this->{$a}($b[0]);
10
    }
11
12
    /**
13
     * Checks through all validation methods to verify it is in a
14
     * phone number format of some type
15
     * @param  [type]  $value [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
16
     * @return boolean        [description]
17
     */
18 3
    protected function isPhone($value)
19
    {
20 3
        return $this->isE164($value) || $this->isNANP($value);
21
    }
22
23
    /**
24
     * Format example +15555555555
25
     * @param  string  $value The phone number to check
26
     * @return boolean        is it correct format?
27
     */
28 9
    protected function isE164($value)
29
    {
30 9
        $conditions = [];
31 9
        $conditions[] = strpos($value, "+") === 0;
32 9
        $conditions[] = strlen($value) >= 9;
33 9
        $conditions[] = strlen($value) <= 16;
34 9
        $conditions[] = preg_match("/[^\d+]/i", $value) === 0;
35 9
        return (bool) array_product($conditions);
36
    }
37
38
    /**
39
     * Format examples: (555) 555-5555, 1 (555) 555-5555, 1-555-555-5555, 555-555-5555, 1 555 555-5555
40
     * https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers#United_States.2C_Canada.2C_and_other_NANP_countries
41
     * @param  string  $value The phone number to check
42
     * @return boolean        is it correct format?
43
     */
44 3
    protected function isNANP($value)
45
    {
46 3
        $conditions[] = preg_match("/^(?:\+1|1)?\s?-?\(?\d{3}\)?(\s|-)?\d{3}-\d{4}$/i", $value) > 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$conditions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $conditions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
47 3
        return (bool) array_product($conditions);
48
    }
49
}
50