IPValidate   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 62
ccs 14
cts 14
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 3 1
A getDomain() 0 5 2
A getType() 0 14 3
1
<?php
2
3
namespace Pamo\IPValidate;
4
5
/**
6
 * IP validator
7
 */
8
class IPValidate
9
{
10
    /**
11
     * @var string $ipV4 type.
12
     * @var string $ipV6 type.
13
     */
14
    private $ipV4 = FILTER_FLAG_IPV4;
15
    private $ipV6 = FILTER_FLAG_IPV6;
16
17
18
19
    /**
20
     * Valide an IP Address.
21
     *
22
     * @param string $ipAddress to validate.
23
     *
24
     * @return bool
25
     */
26 1
    public function isValid(string $ipAddress) : bool
27
    {
28 1
        return filter_var($ipAddress, FILTER_VALIDATE_IP);
29
    }
30
31
32
33
    /**
34
     * Get an IP Address type.
35
     *
36
     * @param string $ipAddress to get type for.
37
     *
38
     * @return string
39
     */
40 1
    public function getType(string $ipAddress) : string
41
    {
42 1
        $type = "";
43
44
        switch ($ipAddress) {
45 1
            case (filter_var($ipAddress, FILTER_VALIDATE_IP, $this->ipV4)):
46 1
                $type = "IPv4";
47 1
                break;
48 1
            case (filter_var($ipAddress, FILTER_VALIDATE_IP, $this->ipV6)):
49 1
                $type = "IPv6";
50 1
                break;
51
        }
52
53 1
        return $type;
54
    }
55
56
57
58
    /**
59
     * Get IP Address Domain name if exists and IP is valid.
60
     *
61
     * @param string $ipAddress IP Address to get the domain name for.
62
     *
63
     * @return string
64
     */
65 1
    public function getDomain(string $ipAddress) : string
66
    {
67 1
        $domain = gethostbyaddr($ipAddress);
68
69 1
        return ($domain != $ipAddress ? $domain : "unavailable");
70
    }
71
}
72