Completed
Push — master ( f7df9a...d0ebd1 )
by Tobias
01:47
created

Address::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 11
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Model;
14
15
use Geocoder\Location;
16
17
/**
18
 * @author William Durand <[email protected]>
19
 */
20
class Address implements Location
21
{
22
    /**
23
     * @var Coordinates|null
24
     */
25
    private $coordinates;
26
27
    /**
28
     * @var Bounds|null
29
     */
30
    private $bounds;
31
32
    /**
33
     * @var string|int|null
34
     */
35
    private $streetNumber;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $streetName;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $subLocality;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $locality;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $postalCode;
56
57
    /**
58
     * @var AdminLevelCollection
59
     */
60
    private $adminLevels;
61
62
    /**
63
     * @var Country|null
64
     */
65
    private $country;
66
67
    /**
68
     * @var string|null
69
     */
70
    private $timezone;
71
72
    /**
73
     * @var string
74
     */
75
    private $providedBy;
76
77
    /**
78
     * @param string               $providedBy
79
     * @param AdminLevelCollection $adminLevels
80
     * @param Coordinates|null     $coordinates
81
     * @param Bounds|null          $bounds
82
     * @param string|null          $streetNumber
83
     * @param string|null          $streetName
84
     * @param string|null          $postalCode
85
     * @param string|null          $locality
86
     * @param string|null          $subLocality
87
     * @param Country|null         $country
88
     * @param string|null          $timezone
89
     */
90 37
    public function __construct(
91
        string $providedBy,
92
        AdminLevelCollection $adminLevels,
93
        Coordinates $coordinates = null,
94
        Bounds $bounds = null,
95
        string $streetNumber = null,
96
        string $streetName = null,
97
        string $postalCode = null,
98
        string $locality = null,
99
        string $subLocality = null,
100
        Country $country = null,
101
        string $timezone = null
102
    ) {
103 37
        $this->providedBy = $providedBy;
104 37
        $this->adminLevels = $adminLevels;
105 37
        $this->coordinates = $coordinates;
106 37
        $this->bounds = $bounds;
107 37
        $this->streetNumber = $streetNumber;
108 37
        $this->streetName = $streetName;
109 37
        $this->postalCode = $postalCode;
110 37
        $this->locality = $locality;
111 37
        $this->subLocality = $subLocality;
112 37
        $this->country = $country;
113 37
        $this->timezone = $timezone;
114 37
    }
115
116
    /**
117
     * @return string
118
     */
119 2
    public function getProvidedBy(): string
120
    {
121 2
        return $this->providedBy;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 20
    public function getCoordinates()
128
    {
129 20
        return $this->coordinates;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 12
    public function getBounds()
136
    {
137 12
        return $this->bounds;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 15
    public function getStreetNumber()
144
    {
145 15
        return $this->streetNumber;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 15
    public function getStreetName()
152
    {
153 15
        return $this->streetName;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 15
    public function getLocality()
160
    {
161 15
        return $this->locality;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 15
    public function getPostalCode()
168
    {
169 15
        return $this->postalCode;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 15
    public function getSubLocality()
176
    {
177 15
        return $this->subLocality;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 15
    public function getAdminLevels(): AdminLevelCollection
184
    {
185 15
        return $this->adminLevels;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 31
    public function getCountry()
192
    {
193 31
        return $this->country;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 15
    public function getTimezone()
200
    {
201 15
        return $this->timezone;
202
    }
203
204
    /**
205
     * Create an Address with an array. Useful for testing.
206
     *
207
     * @param array $data
208
     *
209
     * @return static
210
     */
211 36
    public static function createFromArray(array $data)
212
    {
213
        $defaults = [
214 36
            'providedBy' => 'n/a',
215
            'latitude' => null,
216
            'longitude' => null,
217
            'bounds' => [
218
                'south' => null,
219
                'west' => null,
220
                'north' => null,
221
                'east' => null,
222
            ],
223
            'streetNumber' => null,
224
            'streetName' => null,
225
            'locality' => null,
226
            'postalCode' => null,
227
            'subLocality' => null,
228
            'adminLevels' => [],
229
            'country' => null,
230
            'countryCode' => null,
231
            'timezone' => null,
232
        ];
233
234 36
        $data = array_merge($defaults, $data);
235
236 36
        $adminLevels = [];
237 36
        foreach ($data['adminLevels'] as $adminLevel) {
238 6
            if (empty($adminLevel['level'])) {
239
                continue;
240
            }
241
242 6
            $name = $adminLevel['name'] ?? $adminLevel['code'] ?? null;
243 6
            if (empty($name)) {
244
                continue;
245
            }
246
247 6
            $adminLevels[] = new AdminLevel($adminLevel['level'], $name, $adminLevel['code'] ?? null);
248
        }
249
250 36
        return new static(
251 36
            $data['providedBy'],
252 36
            new AdminLevelCollection($adminLevels),
253 36
            self::createCoordinates(
254 36
                $data['latitude'],
255 36
                $data['longitude']
256
            ),
257 36
            self::createBounds(
258 36
                $data['bounds']['south'],
259 36
                $data['bounds']['west'],
260 36
                $data['bounds']['north'],
261 36
                $data['bounds']['east']
262
            ),
263 36
            $data['streetNumber'],
264 36
            $data['streetName'],
265 36
            $data['postalCode'],
266 36
            $data['locality'],
267 36
            $data['subLocality'],
268 36
            new Country(
269 36
                $data['country'],
270 36
                $data['countryCode']
271
            ),
272 36
            $data['timezone']
273
        );
274
    }
275
276
    /**
277
     * @param float $latitude
278
     * @param float $longitude
279
     *
280
     * @return Coordinates|null
281
     */
282 36
    private static function createCoordinates($latitude, $longitude)
283
    {
284 36
        if (null === $latitude || null === $longitude) {
285 23
            return null;
286
        }
287
288 13
        return new Coordinates($latitude, $longitude);
289
    }
290
291
    /**
292
     * @param float $south
293
     * @param float $west
294
     * @param float $north
295
     *
296
     * @return Bounds|null
297
     */
298 36
    private static function createBounds($south, $west, $north, $east)
299
    {
300 36
        if (null === $south || null === $west || null === $north || null === $east) {
301 29
            return null;
302
        }
303
304 7
        return new Bounds($south, $west, $north, $east);
305
    }
306
307
    /**
308
     * {@inheritdoc}
309
     */
310 16
    public function toArray(): array
311
    {
312 16
        $adminLevels = [];
313 16
        foreach ($this->adminLevels as $adminLevel) {
314 2
            $adminLevels[$adminLevel->getLevel()] = [
315 2
                'name' => $adminLevel->getName(),
316 2
                'code' => $adminLevel->getCode(),
317 2
                'level' => $adminLevel->getLevel(),
318
            ];
319
        }
320
321 16
        $lat = null;
322 16
        $lon = null;
323 16
        if (null !== $coordinates = $this->getCoordinates()) {
324 11
            $lat = $coordinates->getLatitude();
325 11
            $lon = $coordinates->getLongitude();
326
        }
327
328 16
        $countryName = null;
329 16
        $countryCode = null;
330 16
        if (null !== $country = $this->getCountry()) {
331 15
            $countryName = $country->getName();
332 15
            $countryCode = $country->getCode();
333
        }
334
335
        $noBounds = [
336 16
            'south' => null,
337
            'west' => null,
338
            'north' => null,
339
            'east' => null,
340
        ];
341
342
        return [
343 16
            'providedBy' => $this->providedBy,
344 16
            'latitude' => $lat,
345 16
            'longitude' => $lon,
346 16
            'bounds' => null !== $this->bounds ? $this->bounds->toArray() : $noBounds,
347 16
            'streetNumber' => $this->streetNumber,
348 16
            'streetName' => $this->streetName,
349 16
            'postalCode' => $this->postalCode,
350 16
            'locality' => $this->locality,
351 16
            'subLocality' => $this->subLocality,
352 16
            'adminLevels' => $adminLevels,
353 16
            'country' => $countryName,
354 16
            'countryCode' => $countryCode,
355 16
            'timezone' => $this->timezone,
356
        ];
357
    }
358
}
359