Test Failed
Push — master ( 6a40b6...6f5f8a )
by Angel
03:02
created

removeDotNotationToAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Acamposm\IPv4AddressConverter\Traits;
4
5
trait MutableIPv4AddressTrait
6
{
7
    /**
8
     * Apply dot notation to IP Address.
9
     *
10
     * @param string $address
11
     * @param int $length
12
     * @return string
13
     */
14
    private function applyDotNotationToAddress(string $address, int $length): string
15
    {
16
        return implode('.', str_split($address, $length));
0 ignored issues
show
Bug introduced by
It seems like str_split($address, $length) can also be of type true; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

16
        return implode('.', /** @scrutinizer ignore-type */ str_split($address, $length));
Loading history...
17
    }
18
19
    /**
20
     * Remove dot notation from binary string IP Address.
21
     *
22
     * @param string $address
23
     *
24
     * @return string
25
     */
26
    private function removeDotNotationToAddress(string $address): string
27
    {
28
        if (str_contains($address, '.')) {
29
            return str_replace('.', '', $address);
30
        }
31
32
        return $address;
33
    }
34
35
    /**
36
     * Converts a binary IP Address to dotted decimal IP Address.
37
     *
38
     * @param string $address
39
     * @return string
40
     */
41
    private function fromBinaryToDecimal(string $address): string
42
    {
43
        $address = $this->removeDotNotationToAddress($address);
44
        $octets = [];
45
46
        foreach (str_split($address, 8) as $octet) {
47
            $octets[] = bindec($octet);
48
        }
49
50
        return implode('.', $octets);
51
    }
52
53
    /**
54
     * Converts a binary IP Address to a hexadecimal IP Address.
55
     *
56
     * @param string $address
57
     * @return string
58
     */
59
    private function fromBinaryToHex(string $address): string
60
    {
61
        $address = $this->removeDotNotationToAddress($address);
62
        $octets = '';
63
64
        foreach (str_split($address, 8) as $octet) {
65
            $octets .= str_pad(dechex(bindec($octet)), 2, '0', STR_PAD_LEFT);
0 ignored issues
show
Bug introduced by
It seems like bindec($octet) can also be of type double; however, parameter $num of dechex() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            $octets .= str_pad(dechex(/** @scrutinizer ignore-type */ bindec($octet)), 2, '0', STR_PAD_LEFT);
Loading history...
66
        }
67
68
        return strtoupper($octets);
69
    }
70
71
    /**
72
     * Converts a binary IP Address to dotted hexadecimal IP Address.
73
     *
74
     * @param string $address
75
     * @return string
76
     */
77
    private function fromBinaryToDottedHex(string $address): string
78
    {
79
        return $this->applyDotNotationToAddress($this->fromBinaryToHex($address), 2);
80
    }
81
82
    /**
83
     * Converts a binary IP Address to long integer IP Address.
84
     *
85
     * @param string $address
86
     * @return int
87
     */
88
    private function fromBinaryToLong(string $address): int
89
    {
90
        return $this->fromDecimalToLong($this->fromBinaryToDecimal($address));
91
    }
92
93
    /**
94
     * Converts a dotted decimal IP Address to binary IP Address.
95
     *
96
     * @param string $address
97
     * @return string
98
     */
99
    private function fromDecimalToBinary(string $address): string
100
    {
101
        $octets = '';
102
103
        foreach (explode('.', $address) as $octet) {
104
            $octets .= str_pad(decbin($octet), 8, '0', STR_PAD_LEFT);
0 ignored issues
show
Bug introduced by
$octet of type string is incompatible with the type integer expected by parameter $num of decbin(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
            $octets .= str_pad(decbin(/** @scrutinizer ignore-type */ $octet), 8, '0', STR_PAD_LEFT);
Loading history...
105
        }
106
107
        return $octets;
108
    }
109
110
    /**
111
     * Converts a dotted decimal IP Address to doted binary IP Address.
112
     *
113
     * @param string $address
114
     * @return string
115
     */
116
    private function fromDecimalToDottedBinary(string $address): string
117
    {
118
        return $this->applyDotNotationToAddress($this->fromDecimalToBinary($address), 8);
119
    }
120
121
    /**
122
     * Converts a dotted decimal IP Address to dotted hex IP Address.
123
     *
124
     * @param string $address
125
     * @return string
126
     */
127
    private function fromDecimalToDottedHex(string $address): string
128
    {
129
        return $this->applyDotNotationToAddress($this->fromDecimalToHex($address), 2);
130
    }
131
132
    /**
133
     * Converts a dotted decimal IP Address to a hex IP Address.
134
     *
135
     * @param string $address
136
     * @return string
137
     */
138
    private function fromDecimalToHex(string $address): string
139
    {
140
        $octets = '';
141
142
        foreach (explode('.', $address) as $octet) {
143
            $octets .= str_pad(dechex($octet), 2, '0', STR_PAD_LEFT);
0 ignored issues
show
Bug introduced by
$octet of type string is incompatible with the type integer expected by parameter $num of dechex(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
            $octets .= str_pad(dechex(/** @scrutinizer ignore-type */ $octet), 2, '0', STR_PAD_LEFT);
Loading history...
144
        }
145
146
        return strtoupper($octets);
147
    }
148
149
    /**
150
     * Converts a doted decimal IP Address to long integer IP Address.
151
     *
152
     * @param string $address
153
     * @return int
154
     */
155
    private function fromDecimalToLong(string $address): int
156
    {
157
        return ip2long($address);
158
    }
159
160
161
    /**
162
     * Converts a hexadecimal IP address to binary IP Address.
163
     *
164
     * @param string $address
165
     * @return string
166
     */
167
    private function fromHexadecimalToBinary(string $address): string
168
    {
169
        $octets = [];
170
171
        foreach (str_split($address, 2) as $octet) {
172
            $octets[] = str_pad(decbin(hexdec($octet)), 8, '0', STR_PAD_LEFT);
0 ignored issues
show
Bug introduced by
It seems like hexdec($octet) can also be of type double; however, parameter $num of decbin() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

172
            $octets[] = str_pad(decbin(/** @scrutinizer ignore-type */ hexdec($octet)), 8, '0', STR_PAD_LEFT);
Loading history...
173
        }
174
175
        return implode('', $octets);
176
    }
177
178
    /**
179
     * Converts a hexadecimal IP address to binary IP Address with dot notation.
180
     *
181
     * @param string $address
182
     * @return string
183
     */
184
    private function fromHexadecimalToDottedBinary(string $address): string
185
    {
186
        return $this->applyDotNotationToAddress($this->fromHexadecimalToBinary($address), 8);
187
    }
188
189
    /**
190
     * Converts a hexadecimal IP address to decimal IP Address.
191
     *
192
     * @param string $address
193
     * @return string
194
     */
195
    private function fromHexadecimalToDecimal(string $address): string
196
    {
197
        $octets = [];
198
199
        foreach (str_split($address, 2) as $octet) {
200
            $octets[] = hexdec($octet);
201
        }
202
203
        return implode('.', $octets);
204
    }
205
206
    /**
207
     * Converts a hexadecimal IP address to Long integer IP Address.
208
     *
209
     * @param string $address
210
     * @return int
211
     */
212
    private function fromHexadecimalToLong(string $address): int
213
    {
214
        return ip2long($this->fromHexadecimalToDecimal($address));
215
    }
216
217
    /**
218
     * Converts a dotted binary IP Address to doted decimal IP Address.
219
     *
220
     * @param string $address
221
     * @return string
222
     */
223
    private function fromDottedBinaryToDecimal(string $address): string
224
    {
225
        $octets = [];
226
227
        foreach (explode('.', $address) as $octet) {
228
            $octets[] = bindec($octet);
229
        }
230
231
        return implode('.', $octets);
232
    }
233
234
    /**
235
     * Converts a long integer IP Address to decimal IP Address.
236
     *
237
     * @param string $address
238
     * @return string
239
     */
240
    private function fromLongToDecimal(string $address): string
241
    {
242
        return long2ip($address);
0 ignored issues
show
Bug introduced by
$address of type string is incompatible with the type integer expected by parameter $ip of long2ip(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

242
        return long2ip(/** @scrutinizer ignore-type */ $address);
Loading history...
243
    }
244
245
    /**
246
     * Converts a long integer IP Address to a dotted hex IP Address.
247
     *
248
     * @param string $address
249
     * @return string
250
     */
251
    private function fromLongToDottedHex(string $address): string
252
    {
253
        return $this->fromDecimalToDottedHex(long2ip($address));
0 ignored issues
show
Bug introduced by
$address of type string is incompatible with the type integer expected by parameter $ip of long2ip(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

253
        return $this->fromDecimalToDottedHex(long2ip(/** @scrutinizer ignore-type */ $address));
Loading history...
254
    }
255
256
    /**
257
     * Converts a long integer IP Address to a hex IP Address.
258
     *
259
     * @param int $address
260
     * @return string
261
     */
262
    private function fromLongToHex(int $address): string
263
    {
264
        return $this->fromDecimalToHex(long2ip($address));
265
    }
266
267
    /**
268
     * Converts a long integer IP Address to binary IP Address.
269
     *
270
     * @param string $address
271
     * @return string
272
     */
273
    private function fromLongToBinary(string $address): string
274
    {
275
        return $this->fromDecimalToBinary($this->fromLongToDecimal($address));
276
    }
277
278
    /**
279
     * Converts a long integer IP Address to dotted binary IP Address.
280
     * @param string $address
281
     * @return string
282
     */
283
    private function fromLongToDottedBinary(string $address): string
284
    {
285
        return $this->fromDecimalToDottedBinary($this->fromLongToDecimal($address));
286
    }
287
}
288