diff --git a/CHANGELOG.md b/CHANGELOG.md index 24f9d7d..1004145 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,23 @@ All notable changes to `container` will be documented in this file. Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## 1.4.1 - 2020-05-06 + +### Added +- Added: GrumpPHP, thank to [drupol](https://github.com/drupol). + +### Deprecated +- Nothing + +### Fixed +- Some internal change of method visibility. + +### Removed +- Nothing + +### Security +- Nothing + ## 1.4.0 - 2020-05-06 ### Added diff --git a/src/Container.php b/src/Container.php index 1c19210..1ad2657 100644 --- a/src/Container.php +++ b/src/Container.php @@ -213,7 +213,7 @@ function (ReflectionParameter $param) use ($arguments) { return $this->get($param->getName()); } - throw ContainerException::findType($param->getClass()); + throw ContainerException::findType($type); } if (true === $type->isBuiltin()) { @@ -224,6 +224,8 @@ function (ReflectionParameter $param) use ($arguments) { if ($type->allowsNull()) { return null; } + + throw ContainerException::findType($type); } if (ContainerInterface::class === $type->getName()) { diff --git a/src/ContainerException.php b/src/ContainerException.php index 2a44e51..11e180b 100644 --- a/src/ContainerException.php +++ b/src/ContainerException.php @@ -6,18 +6,18 @@ use Exception; use Psr\Container\ContainerExceptionInterface; -use ReflectionClass; +use ReflectionNamedType; final class ContainerException extends Exception implements ContainerExceptionInterface { /** - * @param ReflectionClass|null $class + * @param ReflectionNamedType|null $class * * @return ContainerException */ - public static function findType(?ReflectionClass $class): ContainerException + public static function findType(?ReflectionNamedType $class): ContainerException { - return new self(sprintf('Unable to find type hint of %s', ($class ? $class->getName() : ''))); + return new self(sprintf('Unable to find type hint (%s)', ($class ? $class->getName() : ''))); } /** diff --git a/tests/ResolveConstructionTest.php b/tests/ResolveConstructionTest.php index 68bc65e..31c2da6 100644 --- a/tests/ResolveConstructionTest.php +++ b/tests/ResolveConstructionTest.php @@ -5,6 +5,7 @@ namespace Tests; use Gravatalonga\Container\Container; +use Gravatalonga\Container\ContainerException; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; use Tests\Stub\Bar; @@ -106,4 +107,26 @@ public function testIfParamIsNullableButWeHaveValueFromContainer() self::assertInstanceOf(FooBarWithNullClass::class, $class); self::assertEquals('my-var', $class->name); } + + /** + * @test + */ + public function testGotExceptionFromVariableNameAndBuiltTypeNotIntoContainer() + { + $this->expectException(ContainerException::class); + $this->expectExceptionMessage("Unable to find type hint (string)"); + $container = new Container(); + $class = $container->get(FooBarClass::class); + } + + /** + * @test + */ + public function testGotExceptionFromVariableNameNotIntoContainer() + { + $this->expectException(ContainerException::class); + $this->expectExceptionMessage("Unable to find type hint ()"); + $container = new Container(); + $class = $container->get(FooBarWithoutBuiltInTypeClass::class); + } }