Passed
Push — master ( eb7e43...5163e1 )
by Simon
33:47 queued 29:53
created

smarty_modifier_cidr()   C

Complexity

Conditions 13
Paths 25

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 45
rs 6.6166
c 1
b 0
f 0
cc 13
nc 25
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
/**
11
 * A function which takes an IP address (either IPv4 or IPv6) and a CIDR prefix
12
 * and calculates the first IP in the range.
13
 *
14
 * @param string $ipAddress
15
 * @param ?int    $cidr
16
 *
17
 * @return string
18
 */
19
function smarty_modifier_cidr(string $ipAddress, ?int $cidr): string {
20
    if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
21
        if ($cidr === null) {
22
            $cidr = 32;
23
        }
24
25
        if ($cidr < 0 || $cidr > 32) {
26
            throw new InvalidArgumentException("Invalid CIDR prefix for IPv4: $cidr");
27
        }
28
29
        $ipLong = ip2long($ipAddress);
30
        $mask = -1 << (32 - $cidr);
31
        $network = $ipLong & $mask;
32
33
        return long2ip($network);
34
    }
35
    elseif (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
36
        if ($cidr === null) {
37
            $cidr = 64;
38
        }
39
40
        if ($cidr < 0 || $cidr > 128) {
41
            throw new InvalidArgumentException("Invalid CIDR prefix for IPv6: $cidr");
42
        }
43
44
        $ipBin = inet_pton($ipAddress);
45
        $prefix = $cidr;
46
        $bin = '';
47
        for ($i = 0; $i < strlen($ipBin); $i++) {
48
            $bits = 8;
49
            if ($prefix < 8) {
50
                $bits = $prefix;
51
            }
52
            $mask = $bits === 0 ? 0 : (0xFF << (8 - $bits)) & 0xFF;
53
            $bin .= chr(ord($ipBin[$i]) & $mask);
54
            $prefix -= $bits;
55
            if ($prefix <= 0) {
56
                $prefix = 0;
57
            }
58
        }
59
60
        return inet_ntop($bin);
61
    }
62
63
    throw new InvalidArgumentException("Invalid IP address: $ipAddress");
64
}