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...c0e5a5 )
by Eric
03:24 queued 35s
created

GeocoderProvider::buildAddresses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
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
        $response = new GeocoderResponse();
131
        $response->setStatus($data['status']);
132
        $response->setResults($this->buildResults($data['results']));
133
134
        return $response;
135
    }
136
137
    /**
138
     * @param mixed[] $data
139
     *
140
     * @return GeocoderResult[]
141
     */
142
    private function buildResults(array $data)
143
    {
144
        $results = [];
145
146
        foreach ($data as $index => $response) {
147
            $results[] = $this->buildResult($response);
148
149
            if ($this->limit !== null && ++$index >= $this->limit) {
150
                break;
151
            }
152
        }
153
154
        return $results;
155
    }
156
157
    /**
158
     * @param mixed[] $data
159
     *
160
     * @return GeocoderResult
161
     */
162
    private function buildResult(array $data)
163
    {
164
        $result = new GeocoderResult();
165
        $result->setPlaceId($data['place_id']);
166
        $result->setAddresses($this->buildAddresses($data['address_components']));
167
        $result->setFormattedAddress($data['formatted_address']);
168
        $result->setGeometry($this->buildGeometry($data['geometry']));
169
        $result->setTypes(isset($data['types']) ? $data['types'] : []);
170
        $result->setPartialMatch(isset($data['partial_match']) ? $data['partial_match'] : null);
171
172
        return $result;
173
    }
174
175
    /**
176
     * @param mixed[] $data
177
     *
178
     * @return GeocoderAddress[]
179
     */
180
    private function buildAddresses(array $data)
181
    {
182
        $addresses = [];
183
184
        foreach ($data as $item) {
185
            $addresses[] = $this->buildAddress($item);
186
        }
187
188
        return $addresses;
189
    }
190
191
    /**
192
     * @param mixed[] $data
193
     *
194
     * @return GeocoderAddress
195
     */
196
    private function buildAddress(array $data)
197
    {
198
        $address = new GeocoderAddress();
199
        $address->setLongName($data['long_name']);
200
        $address->setShortName($data['short_name']);
201
        $address->setTypes($data['types']);
202
203
        return $address;
204
    }
205
206
    /**
207
     * @param mixed[] $data
208
     *
209
     * @return GeocoderGeometry
210
     */
211
    private function buildGeometry(array $data)
212
    {
213
        $geometry = new GeocoderGeometry();
214
        $geometry->setBound(isset($data['bounds']) ? $this->buildBound($data['bounds']) : null);
215
        $geometry->setLocation($this->buildCoordinate($data['location']));
216
        $geometry->setLocationType($data['location_type']);
217
        $geometry->setViewport($this->buildBound($data['viewport']));
218
219
        return $geometry;
220
    }
221
222
    /**
223
     * @param mixed[] $data
224
     *
225
     * @return Bound
226
     */
227
    private function buildBound(array $data)
228
    {
229
        return new Bound(
230
            $this->buildCoordinate($data['southwest']),
231
            $this->buildCoordinate($data['northeast'])
232
        );
233
    }
234
235
    /**
236
     * @param mixed[] $data
237
     *
238
     * @return Coordinate
239
     */
240
    private function buildCoordinate(array $data)
241
    {
242
        return new Coordinate($data['lat'], $data['lng']);
243
    }
244
}
245