Completed
Pull Request — master (#69)
by Michele
06:19
created

Single   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 221
Duplicated Lines 9.95 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 7
dl 22
loc 221
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAddressType() 0 4 1
A getRangeType() 0 4 1
A contains() 11 11 3
A containsRange() 11 11 3
A getStartAddress() 0 4 1
A getEndAddress() 0 4 1
A getComparableStartString() 0 4 1
A getComparableEndString() 0 4 1
A __toString() 0 4 1
A fromString() 0 10 2
A fromAddress() 0 4 1
A toString() 0 4 1
A asSubnet() 0 9 1
A asPattern() 0 4 1
A getSubnetMask() 0 8 2
A getReverseDNSLookupName() 0 4 1
A getSize() 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
namespace IPLib\Range;
4
5
use IPLib\Address\AddressInterface;
6
use IPLib\Address\IPv4;
7
use IPLib\Address\Type as AddressType;
8
use IPLib\Factory;
9
10
/**
11
 * Represents a single address (eg a range that contains just one address).
12
 *
13
 * @example 127.0.0.1
14
 * @example ::1
15
 */
16
class Single extends AbstractRange
17
{
18
    /**
19
     * @var \IPLib\Address\AddressInterface
20
     */
21
    protected $address;
22
23
    /**
24
     * Initializes the instance.
25
     *
26
     * @param \IPLib\Address\AddressInterface $address
27
     */
28 102
    protected function __construct(AddressInterface $address)
29
    {
30 102
        $this->address = $address;
31 102
    }
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @see \IPLib\Range\RangeInterface::__toString()
37
     */
38 11
    public function __toString()
39
    {
40 11
        return $this->address->__toString();
41
    }
42
43
    /**
44
     * Try get the range instance starting from its string representation.
45
     *
46
     * @param string|mixed $range
47
     * @param bool $supportNonDecimalIPv4 set to true to support parsing non decimal (that is, octal and hexadecimal) IPv4 addresses
48
     *
49
     * @return static|null
50
     */
51 47
    public static function fromString($range, $supportNonDecimalIPv4 = false)
52
    {
53 47
        $result = null;
54 47
        $address = Factory::addressFromString($range, true, true, $supportNonDecimalIPv4);
55 47
        if ($address !== null) {
56 33
            $result = new static($address);
57
        }
58
59 47
        return $result;
60
    }
61
62
    /**
63
     * Create the range instance starting from an address instance.
64
     *
65
     * @param \IPLib\Address\AddressInterface $address
66
     *
67
     * @return static
68
     */
69 69
    public static function fromAddress(AddressInterface $address)
70
    {
71 69
        return new static($address);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @see \IPLib\Range\RangeInterface::toString()
78
     */
79 19
    public function toString($long = false)
80
    {
81 19
        return $this->address->toString($long);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * @see \IPLib\Range\RangeInterface::getAddressType()
88
     */
89 19
    public function getAddressType()
90
    {
91 19
        return $this->address->getAddressType();
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     *
97
     * @see \IPLib\Range\RangeInterface::getRangeType()
98
     */
99 9
    public function getRangeType()
100
    {
101 9
        return $this->address->getRangeType();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     *
107
     * @see \IPLib\Range\RangeInterface::contains()
108
     */
109 7 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...
110
    {
111 7
        $result = false;
112 7
        if ($address->getAddressType() === $this->getAddressType()) {
113 5
            if ($address->toString(false) === $this->address->toString(false)) {
114 2
                $result = true;
115
            }
116
        }
117
118 7
        return $result;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     *
124
     * @see \IPLib\Range\RangeInterface::containsRange()
125
     */
126 9 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...
127
    {
128 9
        $result = false;
129 9
        if ($range->getAddressType() === $this->getAddressType()) {
130 9
            if ($range->toString(false) === $this->toString(false)) {
131 6
                $result = true;
132
            }
133
        }
134
135 9
        return $result;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     *
141
     * @see \IPLib\Range\RangeInterface::getStartAddress()
142
     */
143 60
    public function getStartAddress()
144
    {
145 60
        return $this->address;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     *
151
     * @see \IPLib\Range\RangeInterface::getEndAddress()
152
     */
153 1
    public function getEndAddress()
154
    {
155 1
        return $this->address;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     *
161
     * @see \IPLib\Range\RangeInterface::getComparableStartString()
162
     */
163 8
    public function getComparableStartString()
164
    {
165 8
        return $this->address->getComparableString();
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     *
171
     * @see \IPLib\Range\RangeInterface::getComparableEndString()
172
     */
173 8
    public function getComparableEndString()
174
    {
175 8
        return $this->address->getComparableString();
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     *
181
     * @see \IPLib\Range\RangeInterface::asSubnet()
182
     */
183 56
    public function asSubnet()
184
    {
185
        $networkPrefixes = array(
186 56
            AddressType::T_IPv4 => 32,
187
            AddressType::T_IPv6 => 128,
188
        );
189
190 56
        return new Subnet($this->address, $this->address, $networkPrefixes[$this->address->getAddressType()]);
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     *
196
     * @see \IPLib\Range\RangeInterface::asPattern()
197
     */
198 56
    public function asPattern()
199
    {
200 56
        return new Pattern($this->address, $this->address, 0);
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     *
206
     * @see \IPLib\Range\RangeInterface::getSubnetMask()
207
     */
208 2
    public function getSubnetMask()
209
    {
210 2
        if ($this->getAddressType() !== AddressType::T_IPv4) {
211 1
            return null;
212
        }
213
214 1
        return IPv4::fromBytes(array(255, 255, 255, 255));
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     *
220
     * @see \IPLib\Range\RangeInterface::getReverseDNSLookupName()
221
     */
222 3
    public function getReverseDNSLookupName()
223
    {
224 3
        return array($this->getStartAddress()->getReverseDNSLookupName());
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     *
230
     * @see \IPLib\Range\RangeInterface::getSize()
231
     */
232
    public function getSize()
233
    {
234
        return 1;
235
    }
236
}
237