Passed
Branch main (86ec0c)
by Michele
08:09
created

AbstractRange::getRangeType()   B

Complexity

Conditions 10
Paths 29

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 23
nc 29
nop 0
dl 0
loc 31
ccs 19
cts 19
cp 1
crap 10
rs 7.6666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use OutOfBoundsException;
11
use OverflowException;
12
13
/**
14
 * Base class for range classes.
15
 */
16
abstract class AbstractRange implements RangeInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     *
21
     * @see \IPLib\Range\RangeInterface::getRangeType()
22
     */
23 242
    public function getRangeType()
24
    {
25 242
        if ($this->rangeType === null) {
26 242
            $addressType = $this->getAddressType();
27 242
            if ($addressType === AddressType::T_IPv6 && Subnet::get6to4()->containsRange($this)) {
28 9
                $this->rangeType = Factory::getRangeFromBoundaries($this->fromAddress->toIPv4(), $this->toAddress->toIPv4())->getRangeType();
0 ignored issues
show
Bug Best Practice introduced by
The property rangeType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
            } else {
30 233
                switch ($addressType) {
31
                    case AddressType::T_IPv4:
32 170
                        $defaultType = IPv4::getDefaultReservedRangeType();
33 170
                        $reservedRanges = IPv4::getReservedRanges();
34 170
                        break;
35
                    case AddressType::T_IPv6:
36 63
                        $defaultType = IPv6::getDefaultReservedRangeType();
37 63
                        $reservedRanges = IPv6::getReservedRanges();
38 63
                        break;
39
                    default:
40
                        throw new \Exception('@todo'); // @codeCoverageIgnore
41
                }
42 233
                $rangeType = null;
43 233
                foreach ($reservedRanges as $reservedRange) {
44 233
                    $rangeType = $reservedRange->getRangeType($this);
45 233
                    if ($rangeType !== null) {
46 198
                        break;
47
                    }
48
                }
49 233
                $this->rangeType = $rangeType === null ? $defaultType : $rangeType;
50
            }
51
        }
52
53 242
        return $this->rangeType === false ? null : $this->rangeType;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     *
59
     * @see \IPLib\Range\RangeInterface::getAddressAtOffset()
60
     */
61 15
    public function getAddressAtOffset($n)
62
    {
63 15
        if (!is_int($n)) {
64 2
            return null;
65
        }
66
67 13
        $address = null;
68 13
        if ($n >= 0) {
69 8
            $start = Factory::parseAddressString($this->getComparableStartString());
70 8
            $address = $start->getAddressAtOffset($n);
71
        } else {
72 5
            $end = Factory::parseAddressString($this->getComparableEndString());
73 5
            $address = $end->getAddressAtOffset($n + 1);
74
        }
75
76 13
        if ($address === null) {
77 1
            return null;
78
        }
79
80 12
        return $this->contains($address) ? $address : null;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     *
86
     * @see \IPLib\Range\RangeInterface::contains()
87
     */
88 253
    public function contains(AddressInterface $address)
89
    {
90 253
        $result = false;
91 253
        if ($address->getAddressType() === $this->getAddressType()) {
92 253
            $cmp = $address->getComparableString();
93 253
            $from = $this->getComparableStartString();
94 253
            if ($cmp >= $from) {
95 248
                $to = $this->getComparableEndString();
96 248
                if ($cmp <= $to) {
97 204
                    $result = true;
98
                }
99
            }
100
        }
101
102 253
        return $result;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @see \IPLib\Range\RangeInterface::containsRange()
109
     */
110 89
    public function containsRange(RangeInterface $range)
111
    {
112 89
        $result = false;
113 89
        if ($range->getAddressType() === $this->getAddressType()) {
114 89
            $myStart = $this->getComparableStartString();
115 89
            $itsStart = $range->getComparableStartString();
116 89
            if ($itsStart >= $myStart) {
117 51
                $myEnd = $this->getComparableEndString();
118 51
                $itsEnd = $range->getComparableEndString();
119 51
                if ($itsEnd <= $myEnd) {
120 21
                    $result = true;
121
                }
122
            }
123
        }
124
125 89
        return $result;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     *
131
     * @see \IPLib\Range\RangeInterface::split()
132
     */
133 65
    public function split($networkPrefix, $forceSubnet = false)
134
    {
135 65
        $networkPrefix = (int) $networkPrefix;
136 65
        $myNetworkPrefix = $this->getNetworkPrefix();
137 65
        if ($networkPrefix === $myNetworkPrefix) {
138
            return array(
139 18
                $forceSubnet ? $this->asSubnet() : $this,
140
            );
141
        }
142 47
        if ($networkPrefix < $myNetworkPrefix) {
143 16
            throw new OutOfBoundsException("The value of the \$networkPrefix parameter can't be smaller than the network prefix of the range ({$myNetworkPrefix})");
144
        }
145 31
        $startIp = $this->getStartAddress();
146 31
        $maxPrefix = $startIp::getNumberOfBits();
147 31
        if ($networkPrefix > $maxPrefix) {
148 13
            throw new OutOfBoundsException("The value of the \$networkPrefix parameter can't be larger than the maximum network prefix of the range ({$maxPrefix})");
149
        }
150
151 18
        $systemBitness = PHP_INT_SIZE * 8;
152 18
        $minPrefixByBitness = $maxPrefix - $systemBitness + 2;
153 18
        if ($networkPrefix < $minPrefixByBitness) {
154 1
            throw new OverflowException("The value of \$networkPrefix leads to too large ranges for the current machine bitness (you can use a value of at least {$minPrefixByBitness})");
155
        }
156
157 17
        $chunkSize = pow(2, $maxPrefix - $networkPrefix);
158 17
        $maxIndex = $this->getSize() / $chunkSize;
159 17
        $data = array();
160 17
        for ($i = 0; $i < $maxIndex; $i++) {
161 17
            $range = Subnet::parseString(sprintf('%s/%d', $startIp, $networkPrefix));
162 17
            if (!$forceSubnet && $this instanceof Pattern) {
163 6
                $range = $range->asPattern() ?: $range;
164
            }
165 17
            $data[] = $range;
166 17
            $startIp = $startIp->getAddressAtOffset($chunkSize);
167
        }
168
169 17
        return $data;
170
    }
171
}
172