diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 67dfcda..d735c3e 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -51,4 +51,13 @@ src/Doctrine/Instantiator/Exception/ExceptionInterface.php src/Doctrine/Instantiator/InstantiatorInterface.php + + + + tests/DoctrineTest/InstantiatorTestAsset/SimpleEnumAsset.php + + + + tests/DoctrineTest/InstantiatorTestAsset/SimpleEnumAsset.php + diff --git a/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php b/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php index bd4e3e6..33de31c 100644 --- a/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php +++ b/src/Doctrine/Instantiator/Exception/InvalidArgumentException.php @@ -17,11 +17,11 @@ class InvalidArgumentException extends BaseInvalidArgumentException implements E public static function fromNonExistingClass(string $className): self { if (interface_exists($className)) { - return new self(sprintf('The provided type "%s" is an interface, and can not be instantiated', $className)); + return new self(sprintf('The provided type "%s" is an interface, and cannot be instantiated', $className)); } if (trait_exists($className)) { - return new self(sprintf('The provided type "%s" is a trait, and can not be instantiated', $className)); + return new self(sprintf('The provided type "%s" is a trait, and cannot be instantiated', $className)); } return new self(sprintf('The provided class "%s" does not exist', $className)); @@ -35,8 +35,16 @@ public static function fromNonExistingClass(string $className): self public static function fromAbstractClass(ReflectionClass $reflectionClass): self { return new self(sprintf( - 'The provided class "%s" is abstract, and can not be instantiated', + 'The provided class "%s" is abstract, and cannot be instantiated', $reflectionClass->getName() )); } + + public static function fromEnum(string $className): self + { + return new self(sprintf( + 'The provided class "%s" is an enum, and cannot be instantiated', + $className + )); + } } diff --git a/src/Doctrine/Instantiator/Instantiator.php b/src/Doctrine/Instantiator/Instantiator.php index d5f591e..d616fa4 100644 --- a/src/Doctrine/Instantiator/Instantiator.php +++ b/src/Doctrine/Instantiator/Instantiator.php @@ -12,6 +12,7 @@ use Serializable; use function class_exists; +use function enum_exists; use function is_subclass_of; use function restore_error_handler; use function set_error_handler; @@ -19,6 +20,8 @@ use function strlen; use function unserialize; +use const PHP_VERSION_ID; + final class Instantiator implements InstantiatorInterface { /** @@ -148,6 +151,10 @@ private function getReflectionClass(string $className): ReflectionClass throw InvalidArgumentException::fromNonExistingClass($className); } + if (PHP_VERSION_ID >= 80100 && enum_exists($className, false)) { + throw InvalidArgumentException::fromEnum($className); + } + $reflection = new ReflectionClass($className); if ($reflection->isAbstract()) { diff --git a/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php b/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php index 5292074..5f32d92 100644 --- a/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php +++ b/tests/DoctrineTest/InstantiatorTest/Exception/InvalidArgumentExceptionTest.php @@ -33,7 +33,7 @@ public function testFromNonExistingTypeWithTrait(): void $exception = InvalidArgumentException::fromNonExistingClass(SimpleTraitAsset::class); self::assertSame( - sprintf('The provided type "%s" is a trait, and can not be instantiated', SimpleTraitAsset::class), + sprintf('The provided type "%s" is a trait, and cannot be instantiated', SimpleTraitAsset::class), $exception->getMessage() ); } @@ -44,7 +44,7 @@ public function testFromNonExistingTypeWithInterface(): void self::assertSame( sprintf( - 'The provided type "%s" is an interface, and can not be instantiated', + 'The provided type "%s" is an interface, and cannot be instantiated', InstantiatorInterface::class ), $exception->getMessage() @@ -58,7 +58,7 @@ public function testFromAbstractClass(): void self::assertSame( sprintf( - 'The provided class "%s" is abstract, and can not be instantiated', + 'The provided class "%s" is abstract, and cannot be instantiated', AbstractClassAsset::class ), $exception->getMessage() diff --git a/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php b/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php index e28a5c0..8054587 100644 --- a/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php +++ b/tests/DoctrineTest/InstantiatorTest/InstantiatorTest.php @@ -14,6 +14,7 @@ use DoctrineTest\InstantiatorTestAsset\PharExceptionAsset; use DoctrineTest\InstantiatorTestAsset\SerializableArrayObjectAsset; use DoctrineTest\InstantiatorTestAsset\SerializableFinalInternalChildAsset; +use DoctrineTest\InstantiatorTestAsset\SimpleEnumAsset; use DoctrineTest\InstantiatorTestAsset\SimpleSerializableAsset; use DoctrineTest\InstantiatorTestAsset\SimpleTraitAsset; use DoctrineTest\InstantiatorTestAsset\UnCloneableAsset; @@ -21,6 +22,7 @@ use DoctrineTest\InstantiatorTestAsset\WakeUpNoticesAsset; use DoctrineTest\InstantiatorTestAsset\XMLReaderAsset; use Exception; +use Generator; use PDORow; use PharException; use PHPUnit\Framework\TestCase; @@ -29,6 +31,8 @@ use function str_replace; use function uniqid; +use const PHP_VERSION_ID; + /** * Tests for {@see \Doctrine\Instantiator\Instantiator} * @@ -142,15 +146,19 @@ public function getInstantiableClasses(): array /** * Provides a list of instantiable classes (existing) * - * @return string[][] + * @psalm-return Generator */ - public function getInvalidClassNames(): array + public function getInvalidClassNames(): Generator { - return [ - [self::class . str_replace('.', '', uniqid('', true))], - [InstantiatorInterface::class], - [AbstractClassAsset::class], - [SimpleTraitAsset::class], - ]; + yield 'invalid string' => [self::class . str_replace('.', '', uniqid('', true))]; + yield 'interface' => [InstantiatorInterface::class]; + yield 'abstract class' => [AbstractClassAsset::class]; + yield 'trait' => [SimpleTraitAsset::class]; + + if (PHP_VERSION_ID < 80100) { + return; + } + + yield 'enum' => [SimpleEnumAsset::class]; } } diff --git a/tests/DoctrineTest/InstantiatorTestAsset/SimpleEnumAsset.php b/tests/DoctrineTest/InstantiatorTestAsset/SimpleEnumAsset.php new file mode 100644 index 0000000..9add64c --- /dev/null +++ b/tests/DoctrineTest/InstantiatorTestAsset/SimpleEnumAsset.php @@ -0,0 +1,9 @@ +