fromHexadecimalToDottedBinary()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
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
     *
13
     * @return string
14
     */
15
    private function applyDotNotationToAddress(string $address, int $length): string
16
    {
17
        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

17
        return implode('.', /** @scrutinizer ignore-type */ str_split($address, $length));
Loading history...
18
    }
19
20
    /**
21
     * Remove dot notation from binary string IP Address.
22
     *
23
     * @param string $address
24
     *
25
     * @return string
26
     */
27
    private function removeDotNotationToAddress(string $address): string
28
    {
29
        if (str_contains($address, '.')) {
30
            return str_replace('.', '', $address);
31
        }
32
33
        return $address;
34
    }
35
36
    /**
37
     * Converts a binary IP Address to dotted decimal IP Address.
38
     *
39
     * @param string $address
40
     *
41
     * @return string
42
     */
43
    private function fromBinaryToDecimal(string $address): string
44
    {
45
        $address = $this->removeDotNotationToAddress($address);
46
        $octets = [];
47
48
        foreach (str_split($address, 8) as $octet) {
49
            $octets[] = bindec($octet);
50
        }
51
52
        return implode('.', $octets);
53
    }
54
55
    /**
56
     * Converts a binary IP Address to a hexadecimal IP Address.
57
     *
58
     * @param string $address
59
     *
60
     * @return string
61
     */
62
    private function fromBinaryToHex(string $address): string
63
    {
64
        $address = $this->removeDotNotationToAddress($address);
65
        $octets = '';
66
67
        foreach (str_split($address, 8) as $octet) {
68
            $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

68
            $octets .= str_pad(dechex(/** @scrutinizer ignore-type */ bindec($octet)), 2, '0', STR_PAD_LEFT);
Loading history...
69
        }
70
71
        return strtoupper($octets);
72
    }
73
74
    /**
75
     * Converts a binary IP Address to dotted hexadecimal IP Address.
76
     *
77
     * @param string $address
78
     *
79
     * @return string
80
     */
81
    private function fromBinaryToDottedHex(string $address): string
82
    {
83
        return $this->applyDotNotationToAddress($this->fromBinaryToHex($address), 2);
84
    }
85
86
    /**
87
     * Converts a binary IP Address to long integer IP Address.
88
     *
89
     * @param string $address
90
     *
91
     * @return int
92
     */
93
    private function fromBinaryToLong(string $address): int
94
    {
95
        return $this->fromDecimalToLong($this->fromBinaryToDecimal($address));
96
    }
97
98
    /**
99
     * Converts a dotted decimal IP Address to binary IP Address.
100
     *
101
     * @param string $address
102
     *
103
     * @return string
104
     */
105
    private function fromDecimalToBinary(string $address): string
106
    {
107
        $octets = '';
108
109
        foreach (explode('.', $address) as $octet) {
110
            $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

110
            $octets .= str_pad(decbin(/** @scrutinizer ignore-type */ $octet), 8, '0', STR_PAD_LEFT);
Loading history...
111
        }
112
113
        return $octets;
114
    }
115
116
    /**
117
     * Converts a dotted decimal IP Address to doted binary IP Address.
118
     *
119
     * @param string $address
120
     *
121
     * @return string
122
     */
123
    private function fromDecimalToDottedBinary(string $address): string
124
    {
125
        return $this->applyDotNotationToAddress($this->fromDecimalToBinary($address), 8);
126
    }
127
128
    /**
129
     * Converts a dotted decimal IP Address to dotted hex IP Address.
130
     *
131
     * @param string $address
132
     *
133
     * @return string
134
     */
135
    private function fromDecimalToDottedHex(string $address): string
136
    {
137
        return $this->applyDotNotationToAddress($this->fromDecimalToHex($address), 2);
138
    }
139
140
    /**
141
     * Converts a dotted decimal IP Address to a hex IP Address.
142
     *
143
     * @param string $address
144
     *
145
     * @return string
146
     */
147
    private function fromDecimalToHex(string $address): string
148
    {
149
        $octets = '';
150
151
        foreach (explode('.', $address) as $octet) {
152
            $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

152
            $octets .= str_pad(dechex(/** @scrutinizer ignore-type */ $octet), 2, '0', STR_PAD_LEFT);
Loading history...
153
        }
154
155
        return strtoupper($octets);
156
    }
157
158
    /**
159
     * Converts a doted decimal IP Address to long integer IP Address.
160
     *
161
     * @param string $address
162
     *
163
     * @return int
164
     */
165
    private function fromDecimalToLong(string $address): int
166
    {
167
        return ip2long($address);
168
    }
169
170
    /**
171
     * Converts a hexadecimal IP address to binary IP Address.
172
     *
173
     * @param string $address
174
     *
175
     * @return string
176
     */
177
    private function fromHexadecimalToBinary(string $address): string
178
    {
179
        $octets = [];
180
181
        foreach (str_split($address, 2) as $octet) {
182
            $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

182
            $octets[] = str_pad(decbin(/** @scrutinizer ignore-type */ hexdec($octet)), 8, '0', STR_PAD_LEFT);
Loading history...
183
        }
184
185
        return implode('', $octets);
186
    }
187
188
    /**
189
     * Converts a hexadecimal IP address to binary IP Address with dot notation.
190
     *
191
     * @param string $address
192
     *
193
     * @return string
194
     */
195
    private function fromHexadecimalToDottedBinary(string $address): string
196
    {
197
        return $this->applyDotNotationToAddress($this->fromHexadecimalToBinary($address), 8);
198
    }
199
200
    /**
201
     * Converts a hexadecimal IP address to decimal IP Address.
202
     *
203
     * @param string $address
204
     *
205
     * @return string
206
     */
207
    private function fromHexadecimalToDecimal(string $address): string
208
    {
209
        $octets = [];
210
211
        foreach (str_split($address, 2) as $octet) {
212
            $octets[] = hexdec($octet);
213
        }
214
215
        return implode('.', $octets);
216
    }
217
218
    /**
219
     * Converts a hexadecimal IP address to Long integer IP Address.
220
     *
221
     * @param string $address
222
     *
223
     * @return int
224
     */
225
    private function fromHexadecimalToLong(string $address): int
226
    {
227
        return ip2long($this->fromHexadecimalToDecimal($address));
228
    }
229
230
    /**
231
     * Converts a long integer IP Address to decimal IP Address.
232
     *
233
     * @param int $address
234
     *
235
     * @return string
236
     */
237
    private function fromLongToDecimal(int $address): string
238
    {
239
        return long2ip($address);
240
    }
241
242
    /**
243
     * Converts a long integer IP Address to a dotted hex IP Address.
244
     *
245
     * @param int $address
246
     *
247
     * @return string
248
     */
249
    private function fromLongToDottedHex(int $address): string
250
    {
251
        return $this->fromDecimalToDottedHex(long2ip($address));
252
    }
253
254
    /**
255
     * Converts a long integer IP Address to a hex IP Address.
256
     *
257
     * @param int $address
258
     *
259
     * @return string
260
     */
261
    private function fromLongToHex(int $address): string
262
    {
263
        return $this->fromDecimalToHex(long2ip($address));
264
    }
265
266
    /**
267
     * Converts a long integer IP Address to binary IP Address.
268
     *
269
     * @param int $address
270
     *
271
     * @return string
272
     */
273
    private function fromLongToBinary(int $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
     *
281
     * @param string $address
282
     *
283
     * @return string
284
     */
285
    private function fromLongToDottedBinary(string $address): string
286
    {
287
        return $this->fromDecimalToDottedBinary($this->fromLongToDecimal($address));
0 ignored issues
show
Bug introduced by
$address of type string is incompatible with the type integer expected by parameter $address of Acamposm\IPv4AddressConv...it::fromLongToDecimal(). ( Ignorable by Annotation )

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

287
        return $this->fromDecimalToDottedBinary($this->fromLongToDecimal(/** @scrutinizer ignore-type */ $address));
Loading history...
288
    }
289
}
290