Ip::isValid()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Kontrolio\Rules\Core;
4
5
use Kontrolio\Rules\AbstractRule;
6
7
/**
8
 * IP address validation rule.
9
 *
10
 * @package Kontrolio\Rules\Core
11
 */
12
class Ip extends AbstractRule
13
{
14
    const V4 = '4';
15
    const V6 = '6';
16
    const ALL = 'all';
17
18
    // adds FILTER_FLAG_NO_PRIV_RANGE flag (skip private ranges)
19
    const V4_NO_PRIV = '4_no_priv';
20
    const V6_NO_PRIV = '6_no_priv';
21
    const ALL_NO_PRIV = 'all_no_priv';
22
23
    // adds FILTER_FLAG_NO_RES_RANGE flag (skip reserved ranges)
24
    const V4_NO_RES = '4_no_res';
25
    const V6_NO_RES = '6_no_res';
26
    const ALL_NO_RES = 'all_no_res';
27
28
    // adds FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE flags (skip both)
29
    const V4_ONLY_PUBLIC = '4_public';
30
    const V6_ONLY_PUBLIC = '6_public';
31
    const ALL_ONLY_PUBLIC = 'all_public';
32
33
    /**
34
     * IP version flag.
35
     *
36
     * @var int
37
     */
38
    protected $version;
39
40
    /**
41
     * Ip constructor.
42
     *
43
     * @param string $version
44
     */
45
    public function __construct($version = self::V4)
46
    {
47
        $this->version = $version;
0 ignored issues
show
Documentation Bug introduced by
The property $version was declared of type integer, but $version is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
48
    }
49
50
    /**
51
     * Validates input.
52
     *
53
     * @param mixed $input
54
     *
55
     * @return bool
56
     */
57
    public function isValid($input = null)
58
    {
59
        if ($input === null || $input === '') {
60
            return false;
61
        }
62
63
        $input = (string) $input;
64
65
        if (!filter_var($input, FILTER_VALIDATE_IP, $this->getFlag())) {
0 ignored issues
show
Bug introduced by
It seems like $this->getFlag() can also be of type null; however, parameter $options of filter_var() does only seem to accept array|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
        if (!filter_var($input, FILTER_VALIDATE_IP, /** @scrutinizer ignore-type */ $this->getFlag())) {
Loading history...
66
            return false;
67
        }
68
69
        return true;
70
    }
71
72
    /**
73
     * Returns IP filter flag.
74
     *
75
     * @return int|null
76
     */
77
    protected function getFlag()
78
    {
79
        switch ($this->version) {
80
            case static::V4:
81
                return FILTER_FLAG_IPV4;
82
            case static::V6:
83
                return FILTER_FLAG_IPV6;
84
            case static::V4_NO_PRIV:
85
                return FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE;
86
            case static::V6_NO_PRIV:
87
                return FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE;
88
            case static::ALL_NO_PRIV:
89
                return FILTER_FLAG_NO_PRIV_RANGE;
90
            case static::V4_NO_RES:
91
                return FILTER_FLAG_IPV4 | FILTER_FLAG_NO_RES_RANGE;
92
            case static::V6_NO_RES:
93
                return FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE;
94
            case static::ALL_NO_RES:
95
                return FILTER_FLAG_NO_RES_RANGE;
96
            case static::V4_ONLY_PUBLIC:
97
                return FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
98
            case static::V6_ONLY_PUBLIC:
99
                return FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
100
            case static::ALL_ONLY_PUBLIC:
101
                return FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
102
            default:
103
                return null;
104
        }
105
    }
106
}
107