Passed
Push — master ( 7f4da8...1c40c3 )
by Antonio Carlos
12:03
created

IpAddress::isCidr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PragmaRX\Firewall\Support;
4
5
use PragmaRX\Support\IpAddress as SupportIpAddress;
6
7
class IpAddress
8
{
9
    use ServiceInstances;
10
11
    /**
12
     * Check if IP address is valid.
13
     *
14
     * @param $ip
15
     *
16
     * @return bool
17
     */
18 60
    public function isValid($ip)
19
    {
20 60
        if (!$result = SupportIpAddress::ipV4Valid($ip = $this->hostToIp($ip)) ||
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $result = (\PragmaRX\Sup...s()->validCountry($ip)), Probably Intended Meaning: ($result = \PragmaRX\Sup...es()->validCountry($ip)
Loading history...
21 60
            $this->countries()->validCountry($ip)) {
22 9
            $this->messages()->addMessage(sprintf('%s is not a valid IP address', $ip));
23
        }
24
25 60
        return $result;
26
    }
27
28
    /**
29
     * Get the ip address of a host.
30
     *
31
     * @param $ip
32
     *
33
     * @return string
34
     */
35 60
    public function hostToIp($ip)
36
    {
37 60
        if (is_string($ip) && starts_with($ip, $string = 'host:')) {
38 2
            return gethostbyname(str_replace($string, '', $ip));
39
        }
40
41 58
        return $ip;
42
    }
43
44
    /**
45
     * Check if IP is in a valid range.
46
     *
47
     * @param $ip_address
48
     * @param $range
49
     *
50
     * @return bool
51
     */
52 15
    public function validRange($ip_address, $range)
53
    {
54 15
        return $this->config()->get('enable_range_search') &&
55 15
            SupportIpAddress::ipV4Valid($range->ip_address) &&
56 15
            SupportIpAddress::ipv4InRange($ip_address, $range->ip_address);
57
    }
58
59
    /**
60
     * Check if an ip v4 is valid.
61
     *
62
     * @param $item
63
     *
64
     * @return bool
65
     */
66 25
    public function ipV4Valid($item)
67
    {
68 25
        if (realpath($item) !== false) {
69 1
            return false;
70
        }
71
72 25
        return SupportIpAddress::ipV4Valid($item);
73
    }
74
75
    /**
76
     * Check if a string is a CIDR.
77
     *
78
     * @param $country
79
     *
80
     * @return bool|array
81
     */
82 28
    public function isCidr($country)
83
    {
84 28
        return SupportIpAddress::isCidr($country);
85
    }
86
}
87