Code Duplication    Length = 76-79 lines in 2 locations

src/IPv4Network/IPv4NetworkComparator.php 1 location

@@ 16-91 (lines=76) @@
13
/**
14
 * The comparator must be used only in IPv4Network.
15
 */
16
class IPv4NetworkComparator
17
{
18
    /**
19
     * @var IPv4Network
20
     */
21
    private $network;
22
23
    /**
24
     * @param IPv4Network $network
25
     */
26
    public function __construct(IPv4Network $network)
27
    {
28
        $this->network = $network;
29
    }
30
31
    /**
32
     * Checks if this network is equal to the specified network.
33
     *
34
     * @param IPv4Network $interval
35
     *
36
     * @return bool
37
     */
38
    public function equal(IPv4Network $interval)
39
    {
40
        return
41
            $this->network->startPoint()->eq($interval->startPoint()) &&
42
            $this->network->mask()->equal($interval->mask())
43
        ;
44
    }
45
46
    /**
47
     * Does this network contain the specified IP.
48
     *
49
     * @param IPv4NetworkPoint $point
50
     *
51
     * @return bool
52
     */
53
    public function contains(IPv4NetworkPoint $point)
54
    {
55
        return ($point->value() & $this->network->mask()->ip()) == $this->network->startPoint()->value();
56
    }
57
58
    /**
59
     * Does this network intersect the specified network.
60
     *
61
     * @param IPv4Network $network
62
     *
63
     * @return bool
64
     */
65
    public function intersects(IPv4Network $network)
66
    {
67
        if (
68
            $this->network->startPoint()->gt($network->endPoint()) ||
69
            $this->network->endPoint()->lt($network->startPoint())
70
        ) {
71
            return false;
72
        }
73
74
        return true;
75
    }
76
77
    /**
78
     * Does this network abut with the network specified.
79
     *
80
     * @param IPv4Network $network
81
     *
82
     * @return bool
83
     */
84
    public function abuts(IPv4Network $network)
85
    {
86
        return
87
            $network->endPoint()->eq($this->network->startPoint()) ||
88
            $this->network->endPoint()->eq($network->startPoint())
89
        ;
90
    }
91
}
92

src/IPv6Network/IPv6NetworkComparator.php 1 location

@@ 16-94 (lines=79) @@
13
/**
14
 * The comparator must be used only in IPv6Network.
15
 */
16
class IPv6NetworkComparator
17
{
18
    /**
19
     * @var IPv6Network
20
     */
21
    private $network;
22
23
    /**
24
     * @param IPv6Network $network
25
     */
26
    public function __construct(IPv6Network $network)
27
    {
28
        $this->network = $network;
29
    }
30
31
    /**
32
     * Checks if this network is equal to the specified network.
33
     *
34
     * @param IPv6Network $interval
35
     *
36
     * @return bool
37
     */
38
    public function equal(IPv6Network $interval)
39
    {
40
        return
41
            $this->network->startPoint()->eq($interval->startPoint()) &&
42
            $this->network->mask() == $interval->mask()
43
        ;
44
    }
45
46
    /**
47
     * Does this network contain the specified IP.
48
     *
49
     * @param IPv6NetworkPoint $point
50
     *
51
     * @return bool
52
     */
53
    public function contains(IPv6NetworkPoint $point)
54
    {
55
        return
56
            $this->network->startPoint()->lte($point) &&
57
            $this->network->endPoint()->gte($point)
58
        ;
59
    }
60
61
    /**
62
     * Does this network intersect the specified network.
63
     *
64
     * @param IPv6Network $network
65
     *
66
     * @return bool
67
     */
68
    public function intersects(IPv6Network $network)
69
    {
70
        if (
71
            $this->network->startPoint()->gt($network->endPoint()) ||
72
            $this->network->endPoint()->lt($network->startPoint())
73
        ) {
74
            return false;
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * Does this network abut with the network specified.
82
     *
83
     * @param IPv6Network $network
84
     *
85
     * @return bool
86
     */
87
    public function abuts(IPv6Network $network)
88
    {
89
        return
90
            $network->endPoint()->eq($this->network->startPoint()) ||
91
            $this->network->endPoint()->eq($network->startPoint())
92
        ;
93
    }
94
}
95