Skip to content

Commit

Permalink
All exceptions should be ours (#705)
Browse files Browse the repository at this point in the history
* Throw our invalid argument exception

* Use factory function for exception

* Fixed the remaining exceptions

* Applied changes from StyleCI
  • Loading branch information
Nyholm committed Jun 17, 2017
1 parent e530af1 commit b4bd6dc
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 23 deletions.
12 changes: 7 additions & 5 deletions Assert.php
Expand Up @@ -12,6 +12,8 @@

namespace Geocoder;

use Geocoder\Exception\InvalidArgument;

class Assert
{
/**
Expand All @@ -21,13 +23,13 @@ class Assert
public static function latitude($value, $message = '')
{
if (!is_float($value)) {
throw new \InvalidArgumentException(
throw new InvalidArgument(
sprintf($message ?: 'Expected a double. Got: %s', self::typeToString($value))
);
}

if ($value < -90 || $value > 90) {
throw new \InvalidArgumentException(
throw new InvalidArgument(
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value)
);
}
Expand All @@ -40,13 +42,13 @@ public static function latitude($value, $message = '')
public static function longitude($value, $message = '')
{
if (!is_float($value)) {
throw new \InvalidArgumentException(
throw new InvalidArgument(
sprintf($message ?: 'Expected a doable. Got: %s', self::typeToString($value))
);
}

if ($value < -180 || $value > 180) {
throw new \InvalidArgumentException(
throw new InvalidArgument(
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value)
);
}
Expand All @@ -59,7 +61,7 @@ public static function longitude($value, $message = '')
public static function notNull($value, $message = '')
{
if (null === $value) {
throw new \InvalidArgumentException(sprintf($message ?: 'Value cannot be null'));
throw new InvalidArgument(sprintf($message ?: 'Value cannot be null'));
}
}

Expand Down
18 changes: 18 additions & 0 deletions Exception/LogicException.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Exception;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class LogicException extends \LogicException implements Exception
{
}
18 changes: 18 additions & 0 deletions Exception/OutOfBoundsException.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Exception;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class OutOfBoundsException extends \OutOfBoundsException implements Exception
{
}
9 changes: 7 additions & 2 deletions Exception/ProviderNotRegistered.php
Expand Up @@ -21,12 +21,17 @@ class ProviderNotRegistered extends \RuntimeException implements Exception
* @param string $providerName
* @param array $registeredProviders
*/
public function __construct(string $providerName, array $registeredProviders = [])
public static function create(string $providerName, array $registeredProviders = [])
{
parent::__construct(sprintf(
return new self(sprintf(
'Provider "%s" is not registered, so you cannot use it. Did you forget to register it or made a typo?%s',
$providerName,
0 == count($registeredProviders) ? '' : sprintf(' Registered providers are: %s.', implode(', ', $registeredProviders))
));
}

public static function noProviderRegistered()
{
return new self('No provider registered.');
}
}
4 changes: 3 additions & 1 deletion Model/AddressBuilder.php
Expand Up @@ -12,6 +12,8 @@

namespace Geocoder\Model;

use Geocoder\Exception\LogicException;

/**
* A class that builds a Location or any of its subclasses.
*
Expand Down Expand Up @@ -102,7 +104,7 @@ public function __construct(string $providedBy)
public function build(string $class = Address::class)
{
if (!is_a($class, Address::class, true)) {
throw new \LogicException('First parameter to LocationBuilder::build must be a class name extending Geocoder\Model\Address');
throw new LogicException('First parameter to LocationBuilder::build must be a class name extending Geocoder\Model\Address');
}

$country = null;
Expand Down
3 changes: 2 additions & 1 deletion Model/AddressCollection.php
Expand Up @@ -14,6 +14,7 @@

use Geocoder\Collection;
use Geocoder\Exception\CollectionIsEmpty;
use Geocoder\Exception\OutOfBoundsException;
use Geocoder\Location;

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

return $this->locations[$index];
Expand Down
29 changes: 17 additions & 12 deletions Model/AdminLevelCollection.php
Expand Up @@ -12,7 +12,9 @@

namespace Geocoder\Model;

use Geocoder\Exception\CollectionIsEmpty;
use Geocoder\Exception\InvalidArgument;
use Geocoder\Exception\OutOfBoundsException;

/**
* @author Giorgio Premi <giosh94mhz@gmail.com>
Expand Down Expand Up @@ -65,29 +67,34 @@ public function count()
}

/**
* @return AdminLevel|null
* @return AdminLevel
*
* @throws CollectionIsEmpty
*/
public function first()
public function first(): AdminLevel
{
if (empty($this->adminLevels)) {
return null;
throw new CollectionIsEmpty();
}

return reset($this->adminLevels);
}

/**
* @param int $offset
* @param int|null $length
*
* @return AdminLevel[]
*/
public function slice($offset, $length = null)
public function slice(int $offset, int $length = null): array
{
return array_slice($this->adminLevels, $offset, $length, true);
}

/**
* @return bool
*/
public function has($level): bool
public function has(int $level): bool
{
return isset($this->adminLevels[$level]);
}
Expand All @@ -98,7 +105,7 @@ public function has($level): bool
* @throws \OutOfBoundsException
* @throws InvalidArgument
*/
public function get($level): AdminLevel
public function get(int $level): AdminLevel
{
$this->checkLevel($level);

Expand All @@ -122,14 +129,12 @@ public function all()
*
* @throws \OutOfBoundsException
*/
private function checkLevel($level)
private function checkLevel(int $level)
{
if ($level <= 0 || $level > self::MAX_LEVEL_DEPTH) {
throw new \OutOfBoundsException(sprintf(
'Administrative level should be an integer in [1,%d], %d given',
self::MAX_LEVEL_DEPTH,
$level
));
throw new OutOfBoundsException(
sprintf('Administrative level should be an integer in [1,%d], %d given', self::MAX_LEVEL_DEPTH, $level)
);
}
}
}
4 changes: 2 additions & 2 deletions ProviderAggregator.php
Expand Up @@ -148,7 +148,7 @@ public function registerProviders(array $providers = []): self
public function using(string $name): self
{
if (!isset($this->providers[$name])) {
throw new ProviderNotRegistered($name ?? '');
throw ProviderNotRegistered::create($name ?? '', $this->providers);
}

$this->provider = $this->providers[$name];
Expand Down Expand Up @@ -177,7 +177,7 @@ protected function getProvider(): Provider
{
if (null === $this->provider) {
if (0 === count($this->providers)) {
throw new \RuntimeException('No provider registered.');
throw ProviderNotRegistered::noProviderRegistered();
}

$this->using(key($this->providers));
Expand Down

0 comments on commit b4bd6dc

Please sign in to comment.