Completed
Push — master ( 918974...334de5 )
by Michele
15s queued 10s
created

Single::getReverseDNSLookupName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 43
    protected function __construct(AddressInterface $address)
29
    {
30 43
        $this->address = $address;
31 43
    }
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 44
    public static function fromString($range, $supportNonDecimalIPv4 = false)
52
    {
53 44
        $result = null;
54 44
        $address = Factory::addressFromString($range, true, true, $supportNonDecimalIPv4);
55 44
        if ($address !== null) {
56 30
            $result = new static($address);
57
        }
58
59 44
        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 13
    public static function fromAddress(AddressInterface $address)
70
    {
71 13
        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 1
    public function getStartAddress()
144
    {
145 1
        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 2
    public function asSubnet()
184
    {
185 2
        $networkPrefixes = array(
186 1
            AddressType::T_IPv4 => 32,
187
            AddressType::T_IPv6 => 128,
188
        );
189 1
190
        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
    public function asPattern()
199
    {
200
        return new Pattern($this->address, $this->address, 0);
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     *
206
     * @see \IPLib\Range\RangeInterface::getSubnetMask()
207
     */
208
    public function getSubnetMask()
209
    {
210
        if ($this->getAddressType() !== AddressType::T_IPv4) {
211
            return null;
212
        }
213
214
        return IPv4::fromBytes(array(255, 255, 255, 255));
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     *
220
     * @see \IPLib\Range\RangeInterface::getReverseDNSLookupName()
221
     */
222
    public function getReverseDNSLookupName()
223
    {
224
        return array($this->getStartAddress()->getReverseDNSLookupName());
225
    }
226
}
227