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.

GeocoderResult   C
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 343
Duplicated Lines 4.66 %

Coupling/Cohesion

Components 7
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 45
lcom 7
cbo 1
dl 16
loc 343
ccs 109
cts 109
cp 1
rs 6.7213
c 0
b 0
f 0

33 Methods

Rating   Name   Duplication   Size   Complexity  
A hasPlaceId() 0 4 1
A getPlaceId() 0 4 1
A setPlaceId() 0 4 1
A hasAddressComponents() 0 6 1
A getAddressComponents() 16 16 4
A setAddressComponents() 0 5 1
A addAddressComponents() 0 6 2
A hasAddressComponent() 0 4 1
A addAddressComponent() 0 6 2
A hasFormattedAddress() 0 4 1
A getFormattedAddress() 0 4 1
A setFormattedAddress() 0 4 1
A hasPostcodeLocalities() 0 4 1
A getPostcodeLocalities() 0 4 1
A setPostcodeLocalities() 0 5 1
A addPostcodeLocalities() 0 6 2
A hasPostcodeLocality() 0 4 1
A addPostcodeLocality() 0 6 2
A hasGeometry() 0 4 1
A getGeometry() 0 4 1
A setGeometry() 0 4 1
A hasPartialMatch() 0 4 1
A isPartialMatch() 0 4 1
A setPartialMatch() 0 4 1
A hasTypes() 0 4 1
A getTypes() 0 4 1
A setTypes() 0 5 1
A addTypes() 0 6 2
A hasType() 0 4 1
A addType() 0 6 2
A removeAddressComponent() 0 5 2
A removePostcodeLocality() 0 5 2
A removeType() 0 5 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like GeocoderResult often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GeocoderResult, and based on these observations, apply Extract Interface, too.

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\Response;
13
14
use Ivory\GoogleMap\Service\Base\AddressComponent;
15
use Ivory\GoogleMap\Service\Base\Geometry;
16
17
/**
18
 * @see http://code.google.com/apis/maps/documentation/javascript/reference.html#GeocoderResult
19
 *
20
 * @author GeLo <[email protected]>
21
 */
22
class GeocoderResult
23
{
24
    /**
25
     * @var string|null
26
     */
27
    private $placeId;
28
29
    /**
30
     * @var AddressComponent[]
31
     */
32
    private $addressComponents = [];
33
34
    /**
35
     * @var string|null
36
     */
37
    private $formattedAddress;
38
39
    /**
40
     * @var string[]
41
     */
42
    private $postcodeLocalities = [];
43
44
    /**
45
     * @var Geometry|null
46
     */
47
    private $geometry;
48
49
    /**
50
     * @var bool|null
51
     */
52
    private $partialMatch;
53
54
    /**
55
     * @var string[]
56
     */
57
    private $types = [];
58
59
    /**
60
     * @return bool
61
     */
62 8
    public function hasPlaceId()
63
    {
64 8
        return $this->placeId !== null;
65
    }
66
67
    /**
68
     * @return string|null
69
     */
70 136
    public function getPlaceId()
71
    {
72 136
        return $this->placeId;
73
    }
74
75
    /**
76
     * @param string|null $placeId
77
     */
78 4
    public function setPlaceId($placeId)
79
    {
80 4
        $this->placeId = $placeId;
81 4
    }
82
83
    /**
84
     * @param string|null $type
85
     *
86
     * @return bool
87
     */
88 24
    public function hasAddressComponents($type = null)
89
    {
90 24
        $addresses = $this->getAddressComponents($type);
91
92 24
        return !empty($addresses);
93
    }
94
95
    /**
96
     * @param string|null $type
97
     *
98
     * @return AddressComponent[]
99
     */
100 152 View Code Duplication
    public function getAddressComponents($type = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102 152
        if ($type === null) {
103 148
            return $this->addressComponents;
104
        }
105
106 4
        $addressComponents = [];
107
108 4
        foreach ($this->addressComponents as $addressComponent) {
109 4
            if (in_array($type, $addressComponent->getTypes(), true)) {
110 4
                $addressComponents[] = $addressComponent;
111 2
            }
112 2
        }
113
114 4
        return $addressComponents;
115
    }
116
117
    /**
118
     * @param AddressComponent[] $addressComponents
119
     */
120 12
    public function setAddressComponents(array $addressComponents)
121
    {
122 12
        $this->addressComponents = [];
123 12
        $this->addAddressComponents($addressComponents);
124 12
    }
125
126
    /**
127
     * @param AddressComponent[] $addressComponents
128
     */
129 12
    public function addAddressComponents(array $addressComponents)
130
    {
131 12
        foreach ($addressComponents as $addressComponent) {
132 12
            $this->addAddressComponent($addressComponent);
133 6
        }
134 12
    }
135
136
    /**
137
     * @param AddressComponent $addressComponent
138
     *
139
     * @return bool
140
     */
141 20
    public function hasAddressComponent(AddressComponent $addressComponent)
142
    {
143 20
        return in_array($addressComponent, $this->addressComponents, true);
144
    }
145
146
    /**
147
     * @param AddressComponent $addressComponent
148
     */
149 20
    public function addAddressComponent(AddressComponent $addressComponent)
150
    {
151 20
        if (!$this->hasAddressComponent($addressComponent)) {
152 20
            $this->addressComponents[] = $addressComponent;
153 10
        }
154 20
    }
155
156
    /**
157
     * @param AddressComponent $addressComponent
158
     */
159 4
    public function removeAddressComponent(AddressComponent $addressComponent)
160
    {
161 4
        unset($this->addressComponents[array_search($addressComponent, $this->addressComponents, true)]);
162 4
        $this->addressComponents = empty($this->addressComponents) ? [] : array_values($this->addressComponents);
163 4
    }
164
165
    /**
166
     * @return bool
167
     */
168 12
    public function hasFormattedAddress()
169
    {
170 12
        return !empty($this->formattedAddress);
171
    }
172
173
    /**
174
     * @return string|null
175
     */
176 140
    public function getFormattedAddress()
177
    {
178 140
        return $this->formattedAddress;
179
    }
180
181
    /**
182
     * @param string|null $formattedAddress
183
     */
184 8
    public function setFormattedAddress($formattedAddress = null)
185
    {
186 8
        $this->formattedAddress = $formattedAddress;
187 8
    }
188
189
    /**
190
     * @return bool
191
     */
192 20
    public function hasPostcodeLocalities()
193
    {
194 20
        return !empty($this->postcodeLocalities);
195
    }
196
197
    /**
198
     * @return string[]
199
     */
200 20
    public function getPostcodeLocalities()
201
    {
202 20
        return $this->postcodeLocalities;
203
    }
204
205
    /**
206
     * @param string[] $postcodeLocalities
207
     */
208 8
    public function setPostcodeLocalities(array $postcodeLocalities)
209
    {
210 8
        $this->postcodeLocalities = [];
211 8
        $this->addPostcodeLocalities($postcodeLocalities);
212 8
    }
213
214
    /**
215
     * @param string[] $postcodeLocalities
216
     */
217 8
    public function addPostcodeLocalities(array $postcodeLocalities)
218
    {
219 8
        foreach ($postcodeLocalities as $postcodeLocality) {
220 8
            $this->addPostcodeLocality($postcodeLocality);
221 4
        }
222 8
    }
223
224
    /**
225
     * @param string $postcodeLocality
226
     *
227
     * @return bool
228
     */
229 16
    public function hasPostcodeLocality($postcodeLocality)
230
    {
231 16
        return in_array($postcodeLocality, $this->postcodeLocalities, true);
232
    }
233
234
    /**
235
     * @param string $postcodeLocality
236
     */
237 16
    public function addPostcodeLocality($postcodeLocality)
238
    {
239 16
        if (!$this->hasPostcodeLocality($postcodeLocality)) {
240 16
            $this->postcodeLocalities[] = $postcodeLocality;
241 8
        }
242 16
    }
243
244
    /**
245
     * @param string $postcodeLocality
246
     */
247 4
    public function removePostcodeLocality($postcodeLocality)
248
    {
249 4
        unset($this->postcodeLocalities[array_search($postcodeLocality, $this->postcodeLocalities, true)]);
250 4
        $this->postcodeLocalities = empty($this->postcodeLocalities) ? [] : array_values($this->postcodeLocalities);
251 4
    }
252
253
    /**
254
     * @return bool
255
     */
256 12
    public function hasGeometry()
257
    {
258 12
        return $this->geometry !== null;
259
    }
260
261
    /**
262
     * @return Geometry|null
263
     */
264 140
    public function getGeometry()
265
    {
266 140
        return $this->geometry;
267
    }
268
269
    /**
270
     * @param Geometry|null $geometry
271
     */
272 8
    public function setGeometry(Geometry $geometry = null)
273
    {
274 8
        $this->geometry = $geometry;
275 8
    }
276
277
    /**
278
     * @return bool
279
     */
280 12
    public function hasPartialMatch()
281
    {
282 12
        return $this->partialMatch !== null;
283
    }
284
285
    /**
286
     * @return bool
287
     */
288 140
    public function isPartialMatch()
289
    {
290 140
        return $this->partialMatch;
291
    }
292
293
    /**
294
     * @param bool|null $partialMatch
295
     */
296 8
    public function setPartialMatch($partialMatch = null)
297
    {
298 8
        $this->partialMatch = $partialMatch;
299 8
    }
300
301
    /**
302
     * @return bool
303
     */
304 20
    public function hasTypes()
305
    {
306 20
        return !empty($this->types);
307
    }
308
309
    /**
310
     * @return string[]
311
     */
312 148
    public function getTypes()
313
    {
314 148
        return $this->types;
315
    }
316
317
    /**
318
     * @param string[] $types
319
     */
320 8
    public function setTypes(array $types)
321
    {
322 8
        $this->types = [];
323 8
        $this->addTypes($types);
324 8
    }
325
326
    /**
327
     * @param string[] $types
328
     */
329 8
    public function addTypes(array $types)
330
    {
331 8
        foreach ($types as $type) {
332 8
            $this->addType($type);
333 4
        }
334 8
    }
335
336
    /**
337
     * @param string $type
338
     *
339
     * @return bool
340
     */
341 16
    public function hasType($type)
342
    {
343 16
        return in_array($type, $this->types, true);
344
    }
345
346
    /**
347
     * @param string $type
348
     */
349 16
    public function addType($type)
350
    {
351 16
        if (!$this->hasType($type)) {
352 16
            $this->types[] = $type;
353 8
        }
354 16
    }
355
356
    /**
357
     * @param string $type
358
     */
359 4
    public function removeType($type)
360
    {
361 4
        unset($this->types[array_search($type, $this->types, true)]);
362 4
        $this->types = empty($this->types) ? [] : array_values($this->types);
363 4
    }
364
}
365