IPAddress::getVersion()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace ValueObjects\Web;
4
5
use ValueObjects\Exception\InvalidNativeArgumentException;
6
7
class IPAddress extends Domain
8
{
9
    /**
10
     * Returns a new IPAddress
11
     *
12
     * @param string $value
13
     */
14 3
    public function __construct($value)
15
    {
16 3
        $filteredValue = filter_var($value, FILTER_VALIDATE_IP);
17
18 3
        if ($filteredValue === false) {
19 1
            throw new InvalidNativeArgumentException($value, array('string (valid ip address)'));
20
        }
21
22 2
        $this->value = $filteredValue;
23 2
    }
24
25
    /**
26
     * Returns the version (IPv4 or IPv6) of the ip address
27
     *
28
     * @return IPAddressVersion
29
     */
30 1
    public function getVersion()
31
    {
32 1
        $isIPv4 = filter_var($this->toNative(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
33
34 1
        if (false !== $isIPv4) {
35 1
            return IPAddressVersion::IPV4();
36
        }
37
38 1
        return IPAddressVersion::IPV6();
39
    }
40
}
41