Skip to content

Commit

Permalink
feat(excaption): make clearer excaption message
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
  • Loading branch information
feryardiant committed Mar 31, 2021
1 parent 40e80d6 commit a0fc0b2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
20 changes: 15 additions & 5 deletions src/Container/Exception/UnresolvableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct($toResolve, ?\Throwable $prev = null)
$prev = $toResolve;
}

parent::__construct(sprintf('Couldn\'t resolve %s.', $this->getTypeString($toResolve)), $prev);
parent::__construct($this->getTypeString($toResolve), $prev);
}

/**
Expand All @@ -29,22 +29,32 @@ public function __construct($toResolve, ?\Throwable $prev = null)
*/
private function getTypeString($toResolve): string
{
$message = 'Cannot resolve %s';

if (is_string($toResolve)) {
return 'string: ' . $toResolve;
return sprintf($message, 'string: ' . $toResolve);
}

if (is_array($toResolve)) {
if (! is_string($toResolve[0])) {
$toResolve[0] = get_class($toResolve[0]);
}

return 'array: [' . join(', ', $toResolve) . ']';
return sprintf($message, 'array: [' . join(', ', $toResolve) . ']');
}

if ($toResolve instanceof NotFoundException) {
return 'container: ' . $toResolve->getName();
return sprintf($message, 'container: ' . $toResolve->getName());
}

if ($toResolve instanceof \ReflectionException) {
return sprintf($message, 'instance: ' . $toResolve->getMessage());
}

if ($toResolve instanceof \Throwable) {
return $toResolve->getMessage();
}

return 'type: ' . gettype($toResolve);
return 'type: ' . (is_object($toResolve) ? get_class($toResolve) : gettype($toResolve));
}
}
14 changes: 5 additions & 9 deletions src/Container/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,13 @@ private function createInstance(string $className)

try {
$reflector = new ReflectionClass($className);
} catch (ReflectionException $err) {
throw new Exception\UnresolvableException($className, $err);
}

if (! $reflector->isInstantiable()) {
throw new Exception(sprintf('Target "%s" is not instantiable.', $className));
}

$args = ($constructor = $reflector->getConstructor()) ? $this->resolveArgs($constructor) : [];
$args = ($constructor = $reflector->getConstructor()) ? $this->resolveArgs($constructor) : [];

return $this->injectContainer($reflector->newInstanceArgs($args));
return $this->injectContainer($reflector->newInstanceArgs($args));
} catch (\Throwable $err) {
throw new Exception\UnresolvableException($err);
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion test/spec/ArrayContainer.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
it('should throw exception when setting incorrect param', function () {
expect(function () {
$this->c['foo'] = Stubs\AbstractFoo::class;
})->toThrow(new Exception(sprintf('Target "%s" is not instantiable.', Stubs\AbstractFoo::class)));
})->toThrow(new Exception\UnresolvableException(
new \Error('Cannot instantiate abstract class Stubs\\AbstractFoo')
));

expect(function () {
$this->c['foo'] = ['foo', 'bar'];
Expand Down
19 changes: 11 additions & 8 deletions test/spec/Container.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,25 +210,28 @@ public function barMethod() {
});

it('should throw exception when setting incorrect param', function () {
$notoInstantible = new Exception(sprintf('Target "%s" is not instantiable.', Stubs\AbstractFoo::class));
$refException = function (string $class) {
return new \ReflectionException(sprintf('Class "%s" does not exist', $class));
};

expect(function () {
$this->c->make(Stubs\AbstractFoo::class);
})->toThrow($notoInstantible);
})->toThrow(new Exception\UnresolvableException(
new \Error('Cannot instantiate abstract class Stubs\\AbstractFoo')
));

expect(function () {
$this->c->set('foo', Stubs\AbstractFoo::class);
})->toThrow($notoInstantible);
})->toThrow(new Exception\UnresolvableException(
new \Error('Cannot instantiate abstract class Stubs\\AbstractFoo')
));

expect(function () {
$this->c->set('foo', 'NotExistsClass');
})->toThrow(new Exception\UnresolvableException(
'NotExistsClass',
new \ReflectionException()
));
})->toThrow(new Exception\UnresolvableException($refException('NotExistsClass')));
expect(function () {
$this->c->set('foo', 'bar');
})->toThrow(new Exception\UnresolvableException('bar'));
})->toThrow(new Exception\UnresolvableException($refException('bar')));

expect(function () {
$this->c->set('foo', ['foo', 'bar']);
Expand Down
4 changes: 3 additions & 1 deletion test/spec/PropertyContainer.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
it('should throw exception when setting incorrect param', function () {
expect(function () {
$this->c->foo = Stubs\AbstractFoo::class;
})->toThrow(new Exception(sprintf('Target "%s" is not instantiable.', Stubs\AbstractFoo::class)));
})->toThrow(new Exception\UnresolvableException(
new \Error('Cannot instantiate abstract class Stubs\\AbstractFoo')
));

expect(function () {
$this->c->foo = ['foo', 'bar'];
Expand Down

0 comments on commit a0fc0b2

Please sign in to comment.