Completed
Pull Request — master (#60)
by Michele
14:12 queued 04:12
created

AbstractRange   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 112
Duplicated Lines 29.46 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 7
dl 33
loc 112
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getRangeType() 0 32 10
A getAddressAtOffset() 0 21 5
A contains() 16 16 4
A containsRange() 17 17 4

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
namespace IPLib\Range;
4
5
use IPLib\Address\AddressInterface;
6
use IPLib\Address\IPv4;
7
use IPLib\Address\IPv6;
8
use IPLib\Address\Type as AddressType;
9
use IPLib\Factory;
10
11
abstract class AbstractRange implements RangeInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     *
16
     * @see \IPLib\Range\RangeInterface::getRangeType()
17
     */
18 242
    public function getRangeType()
19
    {
20 242
        if ($this->rangeType === null) {
21 242
            $addressType = $this->getAddressType();
22 242
            if ($addressType === AddressType::T_IPv6 && Subnet::get6to4()->containsRange($this)) {
23 9
                $this->rangeType = Factory::rangeFromBoundaries($this->fromAddress->toIPv4(), $this->toAddress->toIPv4())->getRangeType();
0 ignored issues
show
Bug introduced by
The property rangeType does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property fromAddress does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property toAddress does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
            } else {
25
                switch ($addressType) {
26 233
                    case AddressType::T_IPv4:
27 170
                        $defaultType = IPv4::getDefaultReservedRangeType();
28 170
                        $reservedRanges = IPv4::getReservedRanges();
29 170
                        break;
30 63
                    case AddressType::T_IPv6:
31 63
                        $defaultType = IPv6::getDefaultReservedRangeType();
32 63
                        $reservedRanges = IPv6::getReservedRanges();
33 63
                        break;
34
                    default:
35
                        throw new \Exception('@todo'); // @codeCoverageIgnore
36
                }
37 233
                $rangeType = null;
38 233
                foreach ($reservedRanges as $reservedRange) {
39 233
                    $rangeType = $reservedRange->getRangeType($this);
40 233
                    if ($rangeType !== null) {
41 233
                        break;
42
                    }
43
                }
44 233
                $this->rangeType = $rangeType === null ? $defaultType : $rangeType;
45
            }
46
        }
47
48 242
        return $this->rangeType === false ? null : $this->rangeType;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @see \IPLib\Range\RangeInterface::getAddressAtOffset()
55
     */
56 241
    public function getAddressAtOffset($n)
57
    {
58 241
        if (!is_int($n)) {
59 241
            return null;
60 241
        }
61 241
62 241
        $address = null;
0 ignored issues
show
Unused Code introduced by
$address is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63 237
        if ($n >= 0) {
64 237
            $start = Factory::addressFromString($this->getComparableStartString());
65 195
            $address = $start->getAddressAtOffset($n);
66
        } else {
67
            $end = Factory::addressFromString($this->getComparableEndString());
68
            $address = $end->getAddressAtOffset($n + 1);
69
        }
70 241
71
        if ($address === null) {
72
            return null;
73
        }
74
75
        return $this->contains($address) ? $address : null;
76
    }
77
78 79
    /**
79
     * {@inheritdoc}
80 79
     *
81 79
     * @see \IPLib\Range\RangeInterface::contains()
82 79
     */
83 79 View Code Duplication
    public function contains(AddressInterface $address)
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...
84 79
    {
85 42
        $result = false;
86 42
        if ($address->getAddressType() === $this->getAddressType()) {
87 42
            $cmp = $address->getComparableString();
88 14
            $from = $this->getComparableStartString();
89
            if ($cmp >= $from) {
90
                $to = $this->getComparableEndString();
91
                if ($cmp <= $to) {
92
                    $result = true;
93 79
                }
94
            }
95
        }
96
97
        return $result;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     *
103
     * @see \IPLib\Range\RangeInterface::containsRange()
104
     */
105 View Code Duplication
    public function containsRange(RangeInterface $range)
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...
106
    {
107
        $result = false;
108
        if ($range->getAddressType() === $this->getAddressType()) {
109
            $myStart = $this->getComparableStartString();
110
            $itsStart = $range->getComparableStartString();
111
            if ($itsStart >= $myStart) {
112
                $myEnd = $this->getComparableEndString();
113
                $itsEnd = $range->getComparableEndString();
114
                if ($itsEnd <= $myEnd) {
115
                    $result = true;
116
                }
117
            }
118
        }
119
120
        return $result;
121
    }
122
}
123