Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more generics #83

Merged
merged 2 commits into from Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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