Test Failed
Push — master ( 6f5f8a...2f484e )
by Angel
03:52
created

fromDottedBinaryToDecimal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
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 long integer IP Address to decimal IP Address.
219
     *
220
     * @param int $address
221
     * @return string
222
     */
223
    private function fromLongToDecimal(int $address): string
224
    {
225
        return long2ip($address);
226
    }
227
228
    /**
229
     * Converts a long integer IP Address to a dotted hex IP Address.
230
     *
231
     * @param int $address
232
     * @return string
233
     */
234
    private function fromLongToDottedHex(int $address): string
235
    {
236
        return $this->fromDecimalToDottedHex(long2ip($address));
237
    }
238
239
    /**
240
     * Converts a long integer IP Address to a hex IP Address.
241
     *
242
     * @param int $address
243
     * @return string
244
     */
245
    private function fromLongToHex(int $address): string
246
    {
247
        return $this->fromDecimalToHex(long2ip($address));
248
    }
249
250
    /**
251
     * Converts a long integer IP Address to binary IP Address.
252
     *
253
     * @param int $address
254
     * @return string
255
     */
256
    private function fromLongToBinary(int $address): string
257
    {
258
        return $this->fromDecimalToBinary($this->fromLongToDecimal($address));
259
    }
260
261
    /**
262
     * Converts a long integer IP Address to dotted binary IP Address.
263
     * @param string $address
264
     * @return string
265
     */
266
    private function fromLongToDottedBinary(string $address): string
267
    {
268
        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

268
        return $this->fromDecimalToDottedBinary($this->fromLongToDecimal(/** @scrutinizer ignore-type */ $address));
Loading history...
269
    }
270
}
271