Skip to content

Commit 87b7849

Browse files
authored
Php7 type hints (#708)
* Added some PSR-7 typehints * Added some PSR-7 typehints * Adding PHP7 stuff * Adding more typehints * Use Throwable instead of Exception * Applied changes from StyleCI
1 parent b6d7979 commit 87b7849

14 files changed

+64
-53
lines changed

src/Common/Collection.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Geocoder;
1414

1515
use Geocoder\Exception\CollectionIsEmpty;
16+
use Geocoder\Exception\OutOfBoundsException;
1617

1718
/**
1819
* This is the interface that is always return from a Geocoder.
@@ -37,22 +38,22 @@ public function isEmpty(): bool;
3738
/**
3839
* @return Location[]
3940
*/
40-
public function slice($offset, $length = null);
41+
public function slice(int $offset, int $length = null);
4142

4243
/**
4344
* @return bool
4445
*/
45-
public function has($index): bool;
46+
public function has(int $index): bool;
4647

4748
/**
4849
* @return Location
4950
*
50-
* @throws \OutOfBoundsException
51+
* @throws OutOfBoundsException
5152
*/
52-
public function get($index): Location;
53+
public function get(int $index): Location;
5354

5455
/**
5556
* @return Location[]
5657
*/
57-
public function all();
58+
public function all(): array;
5859
}

src/Common/Geocoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ public function geocode(string $value): Collection;
5151
*
5252
* @throws \Geocoder\Exception\Exception
5353
*/
54-
public function reverse($latitude, $longitude): Collection;
54+
public function reverse(float $latitude, float $longitude): Collection;
5555
}

src/Common/GeocoderTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function geocode(string $value): Collection
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function reverse($latitude, $longitude): Collection
40+
public function reverse(float $latitude, float $longitude): Collection
4141
{
4242
return $this->reverseQuery(ReverseQuery::fromCoordinates($latitude, $longitude));
4343
}

src/Common/Model/Address.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,18 @@ public static function createFromArray(array $data)
235235

236236
$adminLevels = [];
237237
foreach ($data['adminLevels'] as $adminLevel) {
238+
if (empty($adminLevel['level'])) {
239+
continue;
240+
}
241+
242+
$name = $adminLevel['name'] ?? $adminLevel['code'] ?? null;
243+
if (empty($name)) {
244+
continue;
245+
}
246+
238247
$adminLevels[] = new AdminLevel(
239-
$adminLevel['level'] ?? null,
240-
$adminLevel['name'] ?? null,
248+
$adminLevel['level'],
249+
$name,
241250
$adminLevel['code'] ?? null
242251
);
243252
}

src/Common/Model/AddressBuilder.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Geocoder\Model;
1414

15+
use Geocoder\Exception\InvalidArgument;
1516
use Geocoder\Exception\LogicException;
1617

1718
/**
@@ -101,7 +102,7 @@ public function __construct(string $providedBy)
101102
*
102103
* @return Address
103104
*/
104-
public function build(string $class = Address::class)
105+
public function build(string $class = Address::class): Address
105106
{
106107
if (!is_a($class, Address::class, true)) {
107108
throw new LogicException('First parameter to LocationBuilder::build must be a class name extending Geocoder\Model\Address');
@@ -135,11 +136,11 @@ public function build(string $class = Address::class)
135136
*
136137
* @return AddressBuilder
137138
*/
138-
public function setBounds($south, $west, $north, $east)
139+
public function setBounds($south, $west, $north, $east): self
139140
{
140141
try {
141142
$this->bounds = new Bounds($south, $west, $north, $east);
142-
} catch (\InvalidArgumentException $e) {
143+
} catch (InvalidArgument $e) {
143144
$this->bounds = null;
144145
}
145146

@@ -152,25 +153,25 @@ public function setBounds($south, $west, $north, $east)
152153
*
153154
* @return AddressBuilder
154155
*/
155-
public function setCoordinates($latitude, $longitude)
156+
public function setCoordinates($latitude, $longitude): self
156157
{
157158
try {
158159
$this->coordinates = new Coordinates($latitude, $longitude);
159-
} catch (\InvalidArgumentException $e) {
160+
} catch (InvalidArgument $e) {
160161
$this->coordinates = null;
161162
}
162163

163164
return $this;
164165
}
165166

166167
/**
167-
* @param int $level
168-
* @param string $name
169-
* @param string $code
168+
* @param int $level
169+
* @param string $name
170+
* @param string|null $code
170171
*
171172
* @return AddressBuilder
172173
*/
173-
public function addAdminLevel($level, $name, $code)
174+
public function addAdminLevel(int $level, string $name, string $code = null): self
174175
{
175176
$this->adminLevels[] = new AdminLevel($level, $name, $code);
176177

@@ -182,7 +183,7 @@ public function addAdminLevel($level, $name, $code)
182183
*
183184
* @return AddressBuilder
184185
*/
185-
public function setStreetNumber($streetNumber)
186+
public function setStreetNumber($streetNumber): self
186187
{
187188
$this->streetNumber = $streetNumber;
188189

@@ -194,7 +195,7 @@ public function setStreetNumber($streetNumber)
194195
*
195196
* @return AddressBuilder
196197
*/
197-
public function setStreetName($streetName)
198+
public function setStreetName($streetName): self
198199
{
199200
$this->streetName = $streetName;
200201

@@ -206,7 +207,7 @@ public function setStreetName($streetName)
206207
*
207208
* @return AddressBuilder
208209
*/
209-
public function setLocality($locality)
210+
public function setLocality($locality): self
210211
{
211212
$this->locality = $locality;
212213

@@ -218,7 +219,7 @@ public function setLocality($locality)
218219
*
219220
* @return AddressBuilder
220221
*/
221-
public function setPostalCode($postalCode)
222+
public function setPostalCode($postalCode): self
222223
{
223224
$this->postalCode = $postalCode;
224225

@@ -230,7 +231,7 @@ public function setPostalCode($postalCode)
230231
*
231232
* @return AddressBuilder
232233
*/
233-
public function setSubLocality($subLocality)
234+
public function setSubLocality($subLocality): self
234235
{
235236
$this->subLocality = $subLocality;
236237

@@ -242,7 +243,7 @@ public function setSubLocality($subLocality)
242243
*
243244
* @return AddressBuilder
244245
*/
245-
public function setAdminLevels($adminLevels)
246+
public function setAdminLevels($adminLevels): self
246247
{
247248
$this->adminLevels = $adminLevels;
248249

@@ -254,7 +255,7 @@ public function setAdminLevels($adminLevels)
254255
*
255256
* @return AddressBuilder
256257
*/
257-
public function setCountry($country)
258+
public function setCountry($country): self
258259
{
259260
$this->country = $country;
260261

@@ -266,7 +267,7 @@ public function setCountry($country)
266267
*
267268
* @return AddressBuilder
268269
*/
269-
public function setCountryCode($countryCode)
270+
public function setCountryCode($countryCode): self
270271
{
271272
$this->countryCode = $countryCode;
272273

@@ -278,7 +279,7 @@ public function setCountryCode($countryCode)
278279
*
279280
* @return AddressBuilder
280281
*/
281-
public function setTimezone($timezone)
282+
public function setTimezone($timezone): self
282283
{
283284
$this->timezone = $timezone;
284285

@@ -289,9 +290,9 @@ public function setTimezone($timezone)
289290
* @param string $name
290291
* @param mixed $value
291292
*
292-
* @return $this
293+
* @return AddressBuilder
293294
*/
294-
public function setValue(string $name, $value)
295+
public function setValue(string $name, $value): self
295296
{
296297
$this->data[$name] = $value;
297298

@@ -301,6 +302,8 @@ public function setValue(string $name, $value)
301302
/**
302303
* @param string $name
303304
* @param mixed|null $default
305+
*
306+
* @return mixed
304307
*/
305308
public function getValue(string $name, $default = null)
306309
{

src/Common/Model/AddressCollection.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,23 @@ public function isEmpty(): bool
7171
/**
7272
* @return Location[]
7373
*/
74-
public function slice($offset, $length = null)
74+
public function slice(int $offset, int $length = null)
7575
{
7676
return array_slice($this->locations, $offset, $length);
7777
}
7878

7979
/**
8080
* @return bool
8181
*/
82-
public function has($index): bool
82+
public function has(int $index): bool
8383
{
8484
return isset($this->locations[$index]);
8585
}
8686

8787
/**
8888
* {@inheritdoc}
8989
*/
90-
public function get($index): Location
90+
public function get(int $index): Location
9191
{
9292
if (!isset($this->locations[$index])) {
9393
throw new OutOfBoundsException(sprintf('The index "%s" does not exist in this collection.', $index));
@@ -99,7 +99,7 @@ public function get($index): Location
9999
/**
100100
* {@inheritdoc}
101101
*/
102-
public function all()
102+
public function all(): array
103103
{
104104
return $this->locations;
105105
}

src/Common/Model/AdminLevel.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class AdminLevel
3737
* @param string $name
3838
* @param string|null $code
3939
*/
40-
public function __construct($level, $name, $code)
40+
public function __construct(int $level, string $name, string $code = null)
4141
{
4242
$this->level = $level;
4343
$this->name = $name;
@@ -49,7 +49,7 @@ public function __construct($level, $name, $code)
4949
*
5050
* @return int Level number [1,5]
5151
*/
52-
public function getLevel()
52+
public function getLevel(): int
5353
{
5454
return $this->level;
5555
}
@@ -59,15 +59,15 @@ public function getLevel()
5959
*
6060
* @return string
6161
*/
62-
public function getName()
62+
public function getName(): string
6363
{
6464
return $this->name;
6565
}
6666

6767
/**
6868
* Returns the administrative level short name.
6969
*
70-
* @return string
70+
* @return string|null
7171
*/
7272
public function getCode()
7373
{
@@ -81,6 +81,6 @@ public function getCode()
8181
*/
8282
public function __toString(): string
8383
{
84-
return $this->getName() ?: '';
84+
return $this->getName();
8585
}
8686
}

src/Common/Model/AdminLevelCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function get(int $level): AdminLevel
119119
/**
120120
* @return AdminLevel[]
121121
*/
122-
public function all()
122+
public function all(): array
123123
{
124124
return $this->adminLevels;
125125
}

src/Common/Model/Country.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
final class Country
1919
{
2020
/**
21-
* @var string
21+
* @var string|null
2222
*/
2323
private $name;
2424

2525
/**
26-
* @var string
26+
* @var string|null
2727
*/
2828
private $code;
2929

3030
/**
3131
* @param string $name
3232
* @param string $code
3333
*/
34-
public function __construct($name, $code)
34+
public function __construct(string $name = null, string $code = null)
3535
{
3636
$this->name = $name;
3737
$this->code = $code;
@@ -40,7 +40,7 @@ public function __construct($name, $code)
4040
/**
4141
* Returns the country name.
4242
*
43-
* @return string
43+
* @return string|null
4444
*/
4545
public function getName()
4646
{
@@ -50,7 +50,7 @@ public function getName()
5050
/**
5151
* Returns the country ISO code.
5252
*
53-
* @return string
53+
* @return string|null
5454
*/
5555
public function getCode()
5656
{

src/Common/ProviderAggregator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function geocode(string $value): Collection
8282
/**
8383
* {@inheritdoc}
8484
*/
85-
public function reverse($latitude, $longitude): Collection
85+
public function reverse(float $latitude, float $longitude): Collection
8686
{
8787
return $this->reverseQuery(ReverseQuery::create(new Coordinates($latitude, $longitude))
8888
->withLimit($this->limit));

0 commit comments

Comments
 (0)