GeocoderResult   B
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 332
Duplicated Lines 4.82 %

Coupling/Cohesion

Components 7
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 45
lcom 7
cbo 1
dl 16
loc 332
ccs 101
cts 101
cp 1
rs 8.8
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 removeAddressComponent() 0 5 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 removePostcodeLocality() 0 5 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 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 null !== $this->placeId;
65
    }
66
67
    /**
68
     * @return string|null
69
     */
70 8
    public function getPlaceId()
71
    {
72 8
        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 24 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 24
        if (null === $type) {
103 20
            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
            }
112
        }
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
        }
134 12
    }
135
136
    /**
137
     * @return bool
138
     */
139 20
    public function hasAddressComponent(AddressComponent $addressComponent)
140
    {
141 20
        return in_array($addressComponent, $this->addressComponents, true);
142
    }
143
144 20
    public function addAddressComponent(AddressComponent $addressComponent)
145
    {
146 20
        if (!$this->hasAddressComponent($addressComponent)) {
147 20
            $this->addressComponents[] = $addressComponent;
148
        }
149 20
    }
150
151 4
    public function removeAddressComponent(AddressComponent $addressComponent)
152
    {
153 4
        unset($this->addressComponents[array_search($addressComponent, $this->addressComponents, true)]);
154 4
        $this->addressComponents = empty($this->addressComponents) ? [] : array_values($this->addressComponents);
155 4
    }
156
157
    /**
158
     * @return bool
159
     */
160 12
    public function hasFormattedAddress()
161
    {
162 12
        return !empty($this->formattedAddress);
163
    }
164
165
    /**
166
     * @return string|null
167
     */
168 12
    public function getFormattedAddress()
169
    {
170 12
        return $this->formattedAddress;
171
    }
172
173
    /**
174
     * @param string|null $formattedAddress
175
     */
176 8
    public function setFormattedAddress($formattedAddress = null)
177
    {
178 8
        $this->formattedAddress = $formattedAddress;
179 8
    }
180
181
    /**
182
     * @return bool
183
     */
184 20
    public function hasPostcodeLocalities()
185
    {
186 20
        return !empty($this->postcodeLocalities);
187
    }
188
189
    /**
190
     * @return string[]
191
     */
192 20
    public function getPostcodeLocalities()
193
    {
194 20
        return $this->postcodeLocalities;
195
    }
196
197
    /**
198
     * @param string[] $postcodeLocalities
199
     */
200 8
    public function setPostcodeLocalities(array $postcodeLocalities)
201
    {
202 8
        $this->postcodeLocalities = [];
203 8
        $this->addPostcodeLocalities($postcodeLocalities);
204 8
    }
205
206
    /**
207
     * @param string[] $postcodeLocalities
208
     */
209 8
    public function addPostcodeLocalities(array $postcodeLocalities)
210
    {
211 8
        foreach ($postcodeLocalities as $postcodeLocality) {
212 8
            $this->addPostcodeLocality($postcodeLocality);
213
        }
214 8
    }
215
216
    /**
217
     * @param string $postcodeLocality
218
     *
219
     * @return bool
220
     */
221 16
    public function hasPostcodeLocality($postcodeLocality)
222
    {
223 16
        return in_array($postcodeLocality, $this->postcodeLocalities, true);
224
    }
225
226
    /**
227
     * @param string $postcodeLocality
228
     */
229 16
    public function addPostcodeLocality($postcodeLocality)
230
    {
231 16
        if (!$this->hasPostcodeLocality($postcodeLocality)) {
232 16
            $this->postcodeLocalities[] = $postcodeLocality;
233
        }
234 16
    }
235
236
    /**
237
     * @param string $postcodeLocality
238
     */
239 4
    public function removePostcodeLocality($postcodeLocality)
240
    {
241 4
        unset($this->postcodeLocalities[array_search($postcodeLocality, $this->postcodeLocalities, true)]);
242 4
        $this->postcodeLocalities = empty($this->postcodeLocalities) ? [] : array_values($this->postcodeLocalities);
243 4
    }
244
245
    /**
246
     * @return bool
247
     */
248 12
    public function hasGeometry()
249
    {
250 12
        return null !== $this->geometry;
251
    }
252
253
    /**
254
     * @return Geometry|null
255
     */
256 12
    public function getGeometry()
257
    {
258 12
        return $this->geometry;
259
    }
260
261 8
    public function setGeometry(Geometry $geometry = null)
262
    {
263 8
        $this->geometry = $geometry;
264 8
    }
265
266
    /**
267
     * @return bool
268
     */
269 12
    public function hasPartialMatch()
270
    {
271 12
        return null !== $this->partialMatch;
272
    }
273
274
    /**
275
     * @return bool
276
     */
277 12
    public function isPartialMatch()
278
    {
279 12
        return $this->partialMatch;
280
    }
281
282
    /**
283
     * @param bool|null $partialMatch
284
     */
285 8
    public function setPartialMatch($partialMatch = null)
286
    {
287 8
        $this->partialMatch = $partialMatch;
288 8
    }
289
290
    /**
291
     * @return bool
292
     */
293 20
    public function hasTypes()
294
    {
295 20
        return !empty($this->types);
296
    }
297
298
    /**
299
     * @return string[]
300
     */
301 20
    public function getTypes()
302
    {
303 20
        return $this->types;
304
    }
305
306
    /**
307
     * @param string[] $types
308
     */
309 8
    public function setTypes(array $types)
310
    {
311 8
        $this->types = [];
312 8
        $this->addTypes($types);
313 8
    }
314
315
    /**
316
     * @param string[] $types
317
     */
318 8
    public function addTypes(array $types)
319
    {
320 8
        foreach ($types as $type) {
321 8
            $this->addType($type);
322
        }
323 8
    }
324
325
    /**
326
     * @param string $type
327
     *
328
     * @return bool
329
     */
330 16
    public function hasType($type)
331
    {
332 16
        return in_array($type, $this->types, true);
333
    }
334
335
    /**
336
     * @param string $type
337
     */
338 16
    public function addType($type)
339
    {
340 16
        if (!$this->hasType($type)) {
341 16
            $this->types[] = $type;
342
        }
343 16
    }
344
345
    /**
346
     * @param string $type
347
     */
348 4
    public function removeType($type)
349
    {
350 4
        unset($this->types[array_search($type, $this->types, true)]);
351 4
        $this->types = empty($this->types) ? [] : array_values($this->types);
352 4
    }
353
}
354