Passed
Pull Request — master (#31)
by Anatoly
39:30
created

IpAddress::isVersion4()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Entity;
13
14
/**
15
 * Import functions
16
 */
17
use function filter_var;
18
19
/**
20
 * Import constants
21
 */
22
use const FILTER_FLAG_IPV4;
23
use const FILTER_FLAG_IPV6;
24
use const FILTER_FLAG_NO_PRIV_RANGE;
25
use const FILTER_FLAG_NO_RES_RANGE;
26
use const FILTER_VALIDATE_IP;
27
28
/**
29
 * IP address
30
 */
31
final class IpAddress
32
{
33
34
    /**
35
     * The IP address value
36
     *
37
     * @var string
38
     */
39
    private string $value;
40
41
    /**
42
     * The list of proxies in front of this IP address
43
     *
44
     * @var list<string>
0 ignored issues
show
Bug introduced by
The type Sunrise\Http\Message\Entity\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
     */
46
    private array $proxies;
47
48
    /**
49
     * Constructor of the class
50
     *
51
     * @param string $value
52
     * @param list<string> $proxies
53
     */
54
    public function __construct(string $value, array $proxies = [])
55
    {
56
        $this->value = $value;
57
        $this->proxies = $proxies;
0 ignored issues
show
Documentation Bug introduced by
It seems like $proxies of type array is incompatible with the declared type Sunrise\Http\Message\Entity\list of property $proxies.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
    }
59
60
    /**
61
     * Gets the IP address value
62
     *
63
     * @return string
64
     */
65
    public function getValue(): string
66
    {
67
        return $this->value;
68
    }
69
70
    /**
71
     * Gets the list of proxies in front of this IP address
72
     *
73
     * @return list<string>
74
     */
75
    public function getProxies(): array
76
    {
77
        return $this->proxies;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxies returns the type array which is incompatible with the documented return type Sunrise\Http\Message\Entity\list.
Loading history...
78
    }
79
80
    /**
81
     * Checks if the IP address is valid
82
     *
83
     * @return bool
84
     */
85
    public function isValid(): bool
86
    {
87
        return false !== filter_var($this->value, FILTER_VALIDATE_IP);
88
    }
89
90
    /**
91
     * Checks if the IP address is IPv4
92
     *
93
     * @return bool
94
     */
95
    public function isVersion4(): bool
96
    {
97
        return false !== filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
98
    }
99
100
    /**
101
     * Checks if the IP address is IPv6
102
     *
103
     * @return bool
104
     */
105
    public function isVersion6(): bool
106
    {
107
        return false !== filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
108
    }
109
110
    /**
111
     * Checks if the IP address is in the private range
112
     *
113
     * @return bool
114
     */
115
    public function isInPrivateRange(): bool
116
    {
117
        if (!$this->isValid()) {
118
            return false;
119
        }
120
121
        return false === filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE);
122
    }
123
124
    /**
125
     * Checks if the IP address is in the reserved range
126
     *
127
     * @return bool
128
     */
129
    public function isInReservedRange(): bool
130
    {
131
        if (!$this->isValid()) {
132
            return false;
133
        }
134
135
        return false === filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE);
136
    }
137
}
138