Skip to content

Commit

Permalink
Merge pull request #83 from franmomu/add_more_generics
Browse files Browse the repository at this point in the history
Add more generics
  • Loading branch information
greg0ire committed Nov 11, 2020
2 parents f756b96 + 49c9405 commit d3dbc8a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/Doctrine/Instantiator/Instantiator.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T> $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])) {
Expand Down
2 changes: 2 additions & 0 deletions src/Doctrine/Instantiator/InstantiatorInterface.php
Expand Up @@ -18,6 +18,8 @@ interface InstantiatorInterface
*
* @template T of object
* @phpstan-param class-string<T> $className
*
* @phpstan-return T
*/
public function instantiate($className);
}
Expand Up @@ -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());
}

Expand Down
Expand Up @@ -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 "'
Expand All @@ -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" '
Expand Down

0 comments on commit d3dbc8a

Please sign in to comment.