Helper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 52
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getParts() 0 6 2
A getOctalParts() 0 7 2
A getHexParts() 0 7 2
1
<?php
2
/**
3
 *
4
 * Part of the IPFuscator package.
5
 * Author: Kashyap Merai <[email protected]>
6
 *
7
 */
8
9
10
namespace kamerk22\IPFuscator\Helper;
11
12
13
use kamerk22\IPFuscator\Exception\InvalidArgument;
14
15
class Helper
16
{
17
18
    /**
19
     * getParts
20
     *
21
     * @param string $ip
22
     *
23
     * @return array
24
     * @throws InvalidArgument
25
     */
26 14
    public static function getParts(string $ip): array
27
    {
28 14
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
29 13
            return explode('.', $ip);
30
        }
31 1
        throw InvalidArgument::invalidIp('IP Adress format is invalid.');
32
33
    }
34
35
    /**
36
     * getHexParts
37
     *
38
     * @param string $ip
39
     *
40
     * @return array
41
     * @throws InvalidArgument
42
     */
43 5
    public static function getHexParts(string $ip): array
44
    {
45 5
        $hexParts = [];
46 5
        foreach (self::getParts($ip) as $part) {
47 5
            $hexParts[] = '0x' . dechex((int)$part);
48
        }
49 5
        return $hexParts;
50
    }
51
52
    /**
53
     * getOctalParts
54
     *
55
     * @param string $ip
56
     *
57
     * @return array
58
     * @throws InvalidArgument
59
     */
60 5
    public static function getOctalParts(string $ip): array
61
    {
62 5
        $octParts = [];
63 5
        foreach (self::getParts($ip) as $part) {
64 5
            $octParts[] = '0' . decoct((int)$part);
65
        }
66 5
        return $octParts;
67
    }
68
}