IpValidation   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 40
ccs 17
cts 18
cp 0.9444
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A toJson() 0 33 4
1
<?php
2
namespace Anax\Model;
3
4
class IpValidation
5
{
6
    /**
7
     *
8
     * Returns Array[valid, ip, ipv, domain], get used at view.
9
     *
10
     */
11 6
    public function toJson($ip) : array
12
    {
13 6
            if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
14 2
                $ipv = "Ipv4";
15 4
            } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
16
                $ipv = "Ipv6";
17
            } else {
18 4
                $ipv = null;
19
            }
20
21 6
            if ($ipv != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $ipv of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
22 2
                $details = json_decode(file_get_contents("http://api.ipstack.com/{$ip}?access_key=9e9f05f2e74673444b90a999a579ab3f"));
23 2
                $domain = gethostbyaddr($ip);
24
            return [
25 2
                "valid" => "valid IP",
26 2
                "ip" => $ip,
27 2
                "ipv" => $ipv,
28 2
                "domain" => $domain,
29 2
                "lat" => $details->latitude,
30 2
                "lon" => $details->longitude,
31 2
                "country" => $details->country_name,
32 2
                "city" => $details->city
33
            ];
34
            } else {
35
                return [
36 4
                    "valid" => "Not Valid IP",
37
                    "ip" => null,
38
                    "ipv" => null,
39
                    "domain" => null,
40
                    "lat" => null,
41
                    "lon" => null,
42
                    "country" => null,
43
                    "city" => null
44
                ];
45
            }
46
47
    }
48
}
49