IPAddress   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 34
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getVersion() 0 10 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