Skip to content

Commit b6d7979

Browse files
authored
All exceptions should be ours (#705)
* Throw our invalid argument exception * Use factory function for exception * Fixed the remaining exceptions * Applied changes from StyleCI
1 parent 8dd8cf1 commit b6d7979

File tree

9 files changed

+77
-26
lines changed

9 files changed

+77
-26
lines changed

src/Common/Assert.php

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

1313
namespace Geocoder;
1414

15+
use Geocoder\Exception\InvalidArgument;
16+
1517
class Assert
1618
{
1719
/**
@@ -21,13 +23,13 @@ class Assert
2123
public static function latitude($value, $message = '')
2224
{
2325
if (!is_float($value)) {
24-
throw new \InvalidArgumentException(
26+
throw new InvalidArgument(
2527
sprintf($message ?: 'Expected a double. Got: %s', self::typeToString($value))
2628
);
2729
}
2830

2931
if ($value < -90 || $value > 90) {
30-
throw new \InvalidArgumentException(
32+
throw new InvalidArgument(
3133
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value)
3234
);
3335
}
@@ -40,13 +42,13 @@ public static function latitude($value, $message = '')
4042
public static function longitude($value, $message = '')
4143
{
4244
if (!is_float($value)) {
43-
throw new \InvalidArgumentException(
45+
throw new InvalidArgument(
4446
sprintf($message ?: 'Expected a doable. Got: %s', self::typeToString($value))
4547
);
4648
}
4749

4850
if ($value < -180 || $value > 180) {
49-
throw new \InvalidArgumentException(
51+
throw new InvalidArgument(
5052
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value)
5153
);
5254
}
@@ -59,7 +61,7 @@ public static function longitude($value, $message = '')
5961
public static function notNull($value, $message = '')
6062
{
6163
if (null === $value) {
62-
throw new \InvalidArgumentException(sprintf($message ?: 'Value cannot be null'));
64+
throw new InvalidArgument(sprintf($message ?: 'Value cannot be null'));
6365
}
6466
}
6567

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Exception;
12+
13+
/**
14+
* @author Tobias Nyholm <[email protected]>
15+
*/
16+
class LogicException extends \LogicException implements Exception
17+
{
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Exception;
12+
13+
/**
14+
* @author Tobias Nyholm <[email protected]>
15+
*/
16+
class OutOfBoundsException extends \OutOfBoundsException implements Exception
17+
{
18+
}

src/Common/Exception/ProviderNotRegistered.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ class ProviderNotRegistered extends \RuntimeException implements Exception
2121
* @param string $providerName
2222
* @param array $registeredProviders
2323
*/
24-
public function __construct(string $providerName, array $registeredProviders = [])
24+
public static function create(string $providerName, array $registeredProviders = [])
2525
{
26-
parent::__construct(sprintf(
26+
return new self(sprintf(
2727
'Provider "%s" is not registered, so you cannot use it. Did you forget to register it or made a typo?%s',
2828
$providerName,
2929
0 == count($registeredProviders) ? '' : sprintf(' Registered providers are: %s.', implode(', ', $registeredProviders))
3030
));
3131
}
32+
33+
public static function noProviderRegistered()
34+
{
35+
return new self('No provider registered.');
36+
}
3237
}

src/Common/Model/AddressBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Geocoder\Model;
1414

15+
use Geocoder\Exception\LogicException;
16+
1517
/**
1618
* A class that builds a Location or any of its subclasses.
1719
*
@@ -102,7 +104,7 @@ public function __construct(string $providedBy)
102104
public function build(string $class = Address::class)
103105
{
104106
if (!is_a($class, Address::class, true)) {
105-
throw new \LogicException('First parameter to LocationBuilder::build must be a class name extending Geocoder\Model\Address');
107+
throw new LogicException('First parameter to LocationBuilder::build must be a class name extending Geocoder\Model\Address');
106108
}
107109

108110
$country = null;

src/Common/Model/AddressCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Geocoder\Collection;
1616
use Geocoder\Exception\CollectionIsEmpty;
17+
use Geocoder\Exception\OutOfBoundsException;
1718
use Geocoder\Location;
1819

1920
class AddressCollection implements Collection
@@ -89,7 +90,7 @@ public function has($index): bool
8990
public function get($index): Location
9091
{
9192
if (!isset($this->locations[$index])) {
92-
throw new \OutOfBoundsException(sprintf('The index "%s" does not exist in this collection.', $index));
93+
throw new OutOfBoundsException(sprintf('The index "%s" does not exist in this collection.', $index));
9394
}
9495

9596
return $this->locations[$index];

src/Common/Model/AdminLevelCollection.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
namespace Geocoder\Model;
1414

15+
use Geocoder\Exception\CollectionIsEmpty;
1516
use Geocoder\Exception\InvalidArgument;
17+
use Geocoder\Exception\OutOfBoundsException;
1618

1719
/**
1820
* @author Giorgio Premi <[email protected]>
@@ -65,29 +67,34 @@ public function count()
6567
}
6668

6769
/**
68-
* @return AdminLevel|null
70+
* @return AdminLevel
71+
*
72+
* @throws CollectionIsEmpty
6973
*/
70-
public function first()
74+
public function first(): AdminLevel
7175
{
7276
if (empty($this->adminLevels)) {
73-
return null;
77+
throw new CollectionIsEmpty();
7478
}
7579

7680
return reset($this->adminLevels);
7781
}
7882

7983
/**
84+
* @param int $offset
85+
* @param int|null $length
86+
*
8087
* @return AdminLevel[]
8188
*/
82-
public function slice($offset, $length = null)
89+
public function slice(int $offset, int $length = null): array
8390
{
8491
return array_slice($this->adminLevels, $offset, $length, true);
8592
}
8693

8794
/**
8895
* @return bool
8996
*/
90-
public function has($level): bool
97+
public function has(int $level): bool
9198
{
9299
return isset($this->adminLevels[$level]);
93100
}
@@ -98,7 +105,7 @@ public function has($level): bool
98105
* @throws \OutOfBoundsException
99106
* @throws InvalidArgument
100107
*/
101-
public function get($level): AdminLevel
108+
public function get(int $level): AdminLevel
102109
{
103110
$this->checkLevel($level);
104111

@@ -122,14 +129,12 @@ public function all()
122129
*
123130
* @throws \OutOfBoundsException
124131
*/
125-
private function checkLevel($level)
132+
private function checkLevel(int $level)
126133
{
127134
if ($level <= 0 || $level > self::MAX_LEVEL_DEPTH) {
128-
throw new \OutOfBoundsException(sprintf(
129-
'Administrative level should be an integer in [1,%d], %d given',
130-
self::MAX_LEVEL_DEPTH,
131-
$level
132-
));
135+
throw new OutOfBoundsException(
136+
sprintf('Administrative level should be an integer in [1,%d], %d given', self::MAX_LEVEL_DEPTH, $level)
137+
);
133138
}
134139
}
135140
}

src/Common/ProviderAggregator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function registerProviders(array $providers = []): self
148148
public function using(string $name): self
149149
{
150150
if (!isset($this->providers[$name])) {
151-
throw new ProviderNotRegistered($name ?? '');
151+
throw ProviderNotRegistered::create($name ?? '', $this->providers);
152152
}
153153

154154
$this->provider = $this->providers[$name];
@@ -177,7 +177,7 @@ protected function getProvider(): Provider
177177
{
178178
if (null === $this->provider) {
179179
if (0 === count($this->providers)) {
180-
throw new \RuntimeException('No provider registered.');
180+
throw ProviderNotRegistered::noProviderRegistered();
181181
}
182182

183183
$this->using(key($this->providers));

src/Provider/GoogleMaps/GoogleMaps.php

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

1313
namespace Geocoder\Provider\GoogleMaps;
1414

15-
use Exception;
1615
use Geocoder\Collection;
1716
use Geocoder\Exception\InvalidCredentials;
1817
use Geocoder\Exception\InvalidServerResponse;
@@ -201,8 +200,9 @@ private function fetchUrl(string $url, string $locale = null, int $limit, string
201200
}
202201

203202
if ('REQUEST_DENIED' === $json->status) {
204-
throw new Exception(sprintf('API access denied. Request: %s - Message: %s',
205-
$url, $json->error_message));
203+
throw new InvalidServerResponse(
204+
sprintf('API access denied. Request: %s - Message: %s', $url, $json->error_message)
205+
);
206206
}
207207

208208
// you are over your quota

0 commit comments

Comments
 (0)