IPv6IntervalPoint   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 11.39 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 2
A value() 0 4 1
A __toString() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2016, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Interval\IPv6;
12
13
use GpsLab\Component\Interval\Exception\InvalidPointTypeException;
14
use GpsLab\Component\Interval\BaseIntervalPoint;
15
use GpsLab\Component\Interval\IPv4\IPv4IntervalPoint;
16
17
class IPv6IntervalPoint extends BaseIntervalPoint
18
{
19
    /**
20
     * @var string
21
     */
22
    const IPV6_SEG = '[0-9a-fA-F]{1,4}';
23
24
    /**
25
     * IPv6 address.
26
     *
27
     * Ignore:
28
     *     link-local IPv6 addresses with zone index
29
     *
30
     * @see http://vernon.mauery.com/content/projects/linux/ipv6_regex
31
     *
32
     * @var string
33
     */
34
    const IPV6_ADDR = '(
35
           ('.self::IPV6_SEG.':){7,7}'.self::IPV6_SEG.'|         # 1:2:3:4:5:6:7:8
36
           ('.self::IPV6_SEG.':){1,7}:|                          # 1::                                 1:2:3:4:5:6:7::
37
           ('.self::IPV6_SEG.':){1,6}:'.self::IPV6_SEG.'|        # 1::8               1:2:3:4:5:6::8   1:2:3:4:5:6::8
38
           ('.self::IPV6_SEG.':){1,5}(:'.self::IPV6_SEG.'){1,2}| # 1::7:8             1:2:3:4:5::7:8   1:2:3:4:5::8
39
           ('.self::IPV6_SEG.':){1,4}(:'.self::IPV6_SEG.'){1,3}| # 1::6:7:8           1:2:3:4::6:7:8   1:2:3:4::8
40
           ('.self::IPV6_SEG.':){1,3}(:'.self::IPV6_SEG.'){1,4}| # 1::5:6:7:8         1:2:3::5:6:7:8   1:2:3::8
41
           ('.self::IPV6_SEG.':){1,2}(:'.self::IPV6_SEG.'){1,5}| # 1::4:5:6:7:8       1:2::4:5:6:7:8   1:2::8
42
           '.self::IPV6_SEG.':((:'.self::IPV6_SEG.'){1,6})|      # 1::3:4:5:6:7:8     1::3:4:5:6:7:8   1::8
43
           :((:'.self::IPV6_SEG.'){1,7}|:)|                      # ::2:3:4:5:6:7:8    ::2:3:4:5:6:7:8  ::8       ::
44
45
           # link-local IPv6 addresses with zone index
46
           #fe80:(:'.self::IPV6_SEG.'){0,4}%[0-9a-zA-Z]{1,}|      # fe80::7:8%eth0     fe80::7:8%1
47
48
           # IPv4-mapped IPv6 addresses and IPv4-translated addresses
49
           # ::255.255.255.255  ::ffff:255.255.255.255  ::ffff:0:255.255.255.255
50
           ::(ffff(:0{1,4}){0,1}:){0,1}'.IPv4IntervalPoint::IPV4_ADDR.'|
51
52
           # IPv4-Embedded IPv6 Address
53
           # 2001:db8:3:4::192.0.2.33  64:ff9b::192.0.2.33
54
           ('.self::IPV6_SEG.':){1,4}:'.IPv4IntervalPoint::IPV4_ADDR.'
55
       )';
56
57
    /**
58
     * @var string
59
     */
60
    private $ip;
61
62
    /**
63
     * @var int
64
     */
65
    private $long;
66
67
    /**
68
     * @param string $ip
69
     */
70 40 View Code Duplication
    public function __construct($ip)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
71
    {
72 40
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
73 4
            throw InvalidPointTypeException::point('IPv6', $ip);
74
        }
75
76 36
        $this->ip = $ip;
77 36
        $this->long = inet_pton($ip);
0 ignored issues
show
Documentation Bug introduced by
The property $long was declared of type integer, but inet_pton($ip) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
78 36
    }
79
80
    /**
81
     * @return int
82
     */
83 36
    public function value()
84
    {
85 36
        return $this->long;
86
    }
87
88
    /**
89
     * @return string
90
     */
91 24
    public function __toString()
92
    {
93 24
        return $this->ip;
94
    }
95
}
96