GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Geoip::geocodeQuery()   D
last analyzed

Complexity

Conditions 10
Paths 9

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 28
nc 9
nop 1

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Provider\Geoip;
14
15
use Geocoder\Collection;
16
use Geocoder\Exception\ExtensionNotLoaded;
17
use Geocoder\Exception\UnsupportedOperation;
18
use Geocoder\Model\Address;
19
use Geocoder\Model\AddressCollection;
20
use Geocoder\Query\GeocodeQuery;
21
use Geocoder\Query\ReverseQuery;
22
use Geocoder\Provider\AbstractProvider;
23
use Geocoder\Provider\Provider;
24
25
/**
26
 * @author William Durand <[email protected]>
27
 *
28
 * @see http://php.net/manual/ref.geoip.php
29
 */
30
final class Geoip extends AbstractProvider implements Provider
31
{
32
    public function __construct()
33
    {
34
        if (!function_exists('geoip_record_by_name')) {
35
            throw new ExtensionNotLoaded('You must install the GeoIP extension, see: https://php.net/manual/book.geoip.php.');
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function geocodeQuery(GeocodeQuery $query): Collection
43
    {
44
        $address = $query->getText();
45
46
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
47
            throw new UnsupportedOperation('The Geoip provider does not support street addresses, only IPv4 addresses.');
48
        }
49
50
        // This API does not support IPv6
51
        if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
52
            throw new UnsupportedOperation('The Geoip provider does not support IPv6 addresses, only IPv4 addresses.');
53
        }
54
55
        if ('127.0.0.1' === $address) {
56
            return new AddressCollection([$this->getLocationForLocalhost()]);
57
        }
58
59
        $results = @geoip_record_by_name($address);
60
61
        if (!is_array($results)) {
62
            return new AddressCollection([]);
63
        }
64
65
        if (!empty($results['region']) && !empty($results['country_code'])) {
66
            $timezone = @geoip_time_zone_by_country_and_region($results['country_code'], $results['region']) ?: null;
67
            $region = @geoip_region_name_by_code($results['country_code'], $results['region']) ?: $results['region'];
68
        } else {
69
            $timezone = null;
70
            $region = $results['region'];
71
        }
72
73
        return new AddressCollection([
74
            Address::createFromArray([
75
                'providedBy' => $this->getName(),
76
                'latitude' => $results['latitude'],
77
                'longitude' => $results['longitude'],
78
                'locality' => $results['city'],
79
                'postalCode' => $results['postal_code'],
80
                'adminLevels' => $results['region'] ? [['name' => $region, 'code' => $results['region'], 'level' => 1]] : [],
81
                'country' => $results['country_name'],
82
                'countryCode' => $results['country_code'],
83
                'timezone' => $timezone,
84
            ]),
85
        ]);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function reverseQuery(ReverseQuery $query): Collection
92
    {
93
        throw new UnsupportedOperation('The Geoip provider is not able to do reverse geocoding.');
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getName(): string
100
    {
101
        return 'geoip';
102
    }
103
}
104