Completed
Branch master (9dda00)
by Michael
04:54 queued 02:33
created

IpCheck::addressType()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 37
Code Lines 20

Duplication

Lines 8
Ratio 21.62 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 12
nop 1
dl 8
loc 37
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
//adopted from poweradmin (https://github.com/poweradmin)
4
5
/**
6
 * Class IpCheck
7
 */
8
class IpCheck
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    public $ipin;
11
    public $ipout;
12
    public $ipver;
13
14
    // Return IP type.  4 for IPv4, 6 for IPv6, 0 for bad IP.
15
16
    /**
17
     * @param $ipValue
18
     */
19
    public function addressType($ipValue)
20
    {
21
        $this->ipin  = $ipValue;
22
        $this->ipver = 0;
23
24
        // IPv4 addresses are easy-peasy
25
        if (filter_var($this->ipin, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
26
            $this->ipver = 4;
27
            $this->ipout = $this->ipin;
28
        }
29
30
        // IPv6 is at least a little more complex.
31
        if (filter_var($this->ipin, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
32
33
            // Look for embedded IPv4 in an embedded IPv6 address, where FFFF is appended.
34
            if (strpos($this->ipin, '::FFFF:') === 0) {
35
                $ipv4addr = substr($this->ipin, 7);
36 View Code Duplication
                if (filter_var($ipv4addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
                    $this->ipver = 4;
38
                    $this->ipout = $ipv4addr;
39
                }
40
41
                // Look for an IPv4 address embedded as ::x.x.x.x
42
            } elseif (strpos($this->ipin, '::') === 0) {
43
                $ipv4addr = substr($this->ipin, 2);
44 View Code Duplication
                if (filter_var($ipv4addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
                    $this->ipver = 4;
46
                    $this->ipout = $ipv4addr;
47
                }
48
49
                // Otherwise, assume this an IPv6 address.
50
            } else {
51
                $this->ipver = 6;
52
                $this->ipout = $this->ipin;
53
            }
54
        }
55
    }
56
57
    /** Check whether the given address is an IP address
58
     *
59
     * @param string $ip Given IP address
60
     *
61
     * @return string A if IPv4, AAAA if IPv6 or 0 if invalid
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
62
     */
63
    public function isValidIpAddress($ip)
64
    {
65
        $value = 0;
66
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
67
            $value = 'A';
68
        } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
69
            $value = 'AAAA';
70
        }
71
72
        return $value;
73
    }
74
}
75