diff --git a/src/Doctrine/Instantiator/Instantiator.php b/src/Doctrine/Instantiator/Instantiator.php index ee4803c..330d331 100644 --- a/src/Doctrine/Instantiator/Instantiator.php +++ b/src/Doctrine/Instantiator/Instantiator.php @@ -3,6 +3,7 @@ namespace Doctrine\Instantiator; use ArrayIterator; +use Doctrine\Instantiator\Exception\ExceptionInterface; use Doctrine\Instantiator\Exception\InvalidArgumentException; use Doctrine\Instantiator\Exception\UnexpectedValueException; use Exception; @@ -43,12 +44,26 @@ final class Instantiator implements InstantiatorInterface private static $cachedCloneables = []; /** - * {@inheritDoc} + * @param string $className + * + * @return object + * + * @throws ExceptionInterface + * + * @template T of object + * @phpstan-param class-string $className + * + * @phpstan-return T */ public function instantiate($className) { if (isset(self::$cachedCloneables[$className])) { - return clone self::$cachedCloneables[$className]; + /** + * @phpstan-var T + */ + $cachedCloneable = self::$cachedCloneables[$className]; + + return clone $cachedCloneable; } if (isset(self::$cachedInstantiators[$className])) { diff --git a/src/Doctrine/Instantiator/InstantiatorInterface.php b/src/Doctrine/Instantiator/InstantiatorInterface.php index 3ffff82..39a4581 100644 --- a/src/Doctrine/Instantiator/InstantiatorInterface.php +++ b/src/Doctrine/Instantiator/InstantiatorInterface.php @@ -18,6 +18,8 @@ interface InstantiatorInterface * * @template T of object * @phpstan-param class-string $className + * + * @phpstan-return T */ public function instantiate($className); } diff --git a/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php b/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php index 518c4e2..5292074 100644 --- a/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php +++ b/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php @@ -25,7 +25,6 @@ public function testFromNonExistingTypeWithNonExistingClass(): void $className = self::class . str_replace('.', '', uniqid('', true)); $exception = InvalidArgumentException::fromNonExistingClass($className); - self::assertInstanceOf(InvalidArgumentException::class, $exception); self::assertSame('The provided class "' . $className . '" does not exist', $exception->getMessage()); } diff --git a/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php b/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php index 013b909..b64fdd2 100644 --- a/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php +++ b/tests/DoctrineTest/InstantiatorTest/Exception/UnexpectedValueExceptionTest.php @@ -23,7 +23,6 @@ public function testFromSerializationTriggeredException(): void $previous = new Exception(); $exception = UnexpectedValueException::fromSerializationTriggeredException($reflectionClass, $previous); - self::assertInstanceOf(UnexpectedValueException::class, $exception); self::assertSame($previous, $exception->getPrevious()); self::assertSame( 'An exception was raised while trying to instantiate an instance of "' @@ -37,7 +36,6 @@ public function testFromUncleanUnSerialization(): void $reflection = new ReflectionClass(AbstractClassAsset::class); $exception = UnexpectedValueException::fromUncleanUnSerialization($reflection, 'foo', 123, 'bar', 456); - self::assertInstanceOf(UnexpectedValueException::class, $exception); self::assertSame( sprintf( 'Could not produce an instance of "%s" '