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.
Completed
Push — geocoder ( aa3a17 )
by Eric
02:27
created

GeocoderProvider::geocode()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 19
rs 8.8571
cc 5
eloc 11
nc 6
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Google Map package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\GoogleMap\Service\Geocoder;
13
14
use Geocoder\Provider\LocaleAwareProvider;
15
use Geocoder\Provider\LocaleTrait;
16
use Http\Client\HttpClient;
17
use Http\Message\MessageFactory;
18
use Ivory\GoogleMap\Base\Bound;
19
use Ivory\GoogleMap\Base\Coordinate;
20
use Ivory\GoogleMap\Service\AbstractService;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class GeocoderProvider extends AbstractService implements LocaleAwareProvider
26
{
27
    use LocaleTrait;
28
29
    /**
30
     * @var int|null
31
     */
32
    private $limit;
33
34
    /**
35
     * @param HttpClient     $client
36
     * @param MessageFactory $messageFactory
37
     */
38
    public function __construct(HttpClient $client, MessageFactory $messageFactory)
39
    {
40
        parent::__construct($client, $messageFactory, 'http://maps.googleapis.com/maps/api/geocode');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getLimit()
47
    {
48
        return $this->limit;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function limit($limit)
55
    {
56
        $this->limit = $limit;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param AbstractGeocoderRequest|Coordinate|string $request
63
     *
64
     * @return GeocoderResponse
65
     */
66
    public function geocode($request)
67
    {
68
        if (!$request instanceof AbstractGeocoderRequest) {
69
            if ($request instanceof Coordinate) {
70
                $request = new GeocoderCoordinateRequest($request);
71
            } else {
72
                $request = new GeocoderAddressRequest((string) $request);
73
            }
74
        }
75
76
        if ($this->locale !== null && !$request->hasLanguage()) {
77
            $request->setLanguage($this->locale);
78
        }
79
80
        $response = $this->getClient()->sendRequest($this->createRequest($request->buildQuery()));
81
        $data = $this->parse((string) $response->getBody());
82
83
        return $this->buildResponse($data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->buildResponse($data); (Ivory\GoogleMap\Service\Geocoder\GeocoderResponse) is incompatible with the return type declared by the interface Geocoder\Geocoder::geocode of type Geocoder\Model\AddressCollection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
84
    }
85
86
    /**
87
     * @param float $latitude
88
     * @param float $longitude
89
     *
90
     * @return GeocoderResponse
91
     */
92
    public function reverse($latitude, $longitude)
93
    {
94
        return $this->geocode(new Coordinate($latitude, $longitude));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->geocode(ne...latitude, $longitude)); (Ivory\GoogleMap\Service\Geocoder\GeocoderResponse) is incompatible with the return type declared by the interface Geocoder\Geocoder::reverse of type Geocoder\Model\AddressCollection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getName()
101
    {
102
        return 'ivory_google_map';
103
    }
104
105
    /**
106
     * @param string $data
107
     *
108
     * @return mixed[]
109
     */
110
    private function parse($data)
111
    {
112
        if ($this->getFormat() === self::FORMAT_JSON) {
113
            return json_decode($data, true);
114
        }
115
116
        return $this->getXmlParser()->parse($data, [
117
            'address_component' => 'address_components',
118
            'type'              => 'types',
119
            'result'            => 'results',
120
        ]);
121
    }
122
123
    /**
124
     * @param mixed[] $data
125
     *
126
     * @return GeocoderResponse
127
     */
128
    private function buildResponse(array $data)
129
    {
130
        $results = [];
131
        foreach ($data['results'] as $index => $response) {
132
            $results[] = $this->buildResult($response);
133
134
            if ($this->limit !== null && ++$index >= $this->limit) {
135
                break;
136
            }
137
        }
138
139
        $response = new GeocoderResponse();
140
        $response->setStatus($data['status']);
141
        $response->setResults($results);
142
143
        return $response;
144
    }
145
146
    /**
147
     * @param mixed[] $data
148
     *
149
     * @return GeocoderResult
150
     */
151
    private function buildResult(array $data)
152
    {
153
        $result = new GeocoderResult();
154
        $result->setPlaceId($data['place_id']);
155
        $result->setAddresses($this->buildAddressComponents($data['address_components']));
156
        $result->setFormattedAddress($data['formatted_address']);
157
        $result->setGeometry($this->buildGeometry($data['geometry']));
158
        $result->setTypes(isset($data['types']) ? $data['types'] : []);
159
        $result->setPartialMatch(isset($data['partial_match']) ? $data['partial_match'] : null);
160
161
        return $result;
162
    }
163
164
    /**
165
     * @param mixed[] $data
166
     *
167
     * @return GeocoderAddress[]
168
     */
169
    private function buildAddressComponents(array $data)
170
    {
171
        $addressComponents = [];
172
173
        foreach ($data as $item) {
174
            $addressComponents[] = $this->buildAddressComponent($item);
175
        }
176
177
        return $addressComponents;
178
    }
179
180
    /**
181
     * @param mixed[] $data
182
     *
183
     * @return GeocoderAddress
184
     */
185
    private function buildAddressComponent(array $data)
186
    {
187
        $addressComponent = new GeocoderAddress();
188
        $addressComponent->setLongName($data['long_name']);
189
        $addressComponent->setShortName($data['short_name']);
190
        $addressComponent->setTypes($data['types']);
191
192
        return $addressComponent;
193
    }
194
195
    /**
196
     * @param mixed[] $data
197
     *
198
     * @return GeocoderGeometry
199
     */
200
    private function buildGeometry(array $data)
201
    {
202
        $geometry = new GeocoderGeometry();
203
        $geometry->setBound(isset($data['bounds']) ? $this->buildBound($data['bounds']) : null);
204
        $geometry->setLocation($this->buildCoordinate($data['location']));
205
        $geometry->setLocationType($data['location_type']);
206
        $geometry->setViewport($this->buildBound($data['viewport']));
207
208
        return $geometry;
209
    }
210
211
    /**
212
     * @param mixed[] $data
213
     *
214
     * @return Bound
215
     */
216
    private function buildBound(array $data)
217
    {
218
        return new Bound(
219
            $this->buildCoordinate($data['southwest']),
220
            $this->buildCoordinate($data['northeast'])
221
        );
222
    }
223
224
    /**
225
     * @param mixed[] $data
226
     *
227
     * @return Coordinate
228
     */
229
    private function buildCoordinate(array $data)
230
    {
231
        return new Coordinate($data['lat'], $data['lng']);
232
    }
233
}
234