diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 532bcde2..c838cdd0 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -10,6 +10,16 @@ parameters: count: 1 path: src/Persistence/Reflection/EnumReflectionProperty.php + - + message: "#^Method Doctrine\\\\Persistence\\\\Reflection\\\\EnumReflectionProperty\\:\\:getDeclaringClass\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#" + count: 1 + path: src/Persistence/Reflection/EnumReflectionProperty.php + + - + message: "#^Method Doctrine\\\\Persistence\\\\Reflection\\\\EnumReflectionProperty\\:\\:getType\\(\\) should return ReflectionNamedType\\|ReflectionUnionType\\|null but returns ReflectionType\\|null\\.$#" + count: 1 + path: src/Persistence/Reflection/EnumReflectionProperty.php + - message: "#^Method Doctrine\\\\Persistence\\\\Reflection\\\\EnumReflectionProperty\\:\\:toEnum\\(\\) should return array\\\\|BackedEnum but returns array\\\\.$#" count: 1 diff --git a/src/Persistence/Reflection/EnumReflectionProperty.php b/src/Persistence/Reflection/EnumReflectionProperty.php index 023e542a..fd994f47 100644 --- a/src/Persistence/Reflection/EnumReflectionProperty.php +++ b/src/Persistence/Reflection/EnumReflectionProperty.php @@ -5,7 +5,9 @@ namespace Doctrine\Persistence\Reflection; use BackedEnum; +use ReflectionClass; use ReflectionProperty; +use ReflectionType; use ReturnTypeWillChange; use function array_map; @@ -30,6 +32,44 @@ public function __construct(ReflectionProperty $originalReflectionProperty, stri $this->enumType = $enumType; } + /** + * {@inheritDoc} + * + * @psalm-external-mutation-free + */ + public function getDeclaringClass(): ReflectionClass + { + return $this->originalReflectionProperty->getDeclaringClass(); + } + + /** + * {@inheritDoc} + * + * @psalm-external-mutation-free + */ + public function getName(): string + { + return $this->originalReflectionProperty->getName(); + } + + /** + * {@inheritDoc} + * + * @psalm-external-mutation-free + */ + public function getType(): ?ReflectionType + { + return $this->originalReflectionProperty->getType(); + } + + /** + * {@inheritDoc} + */ + public function getAttributes(?string $name = null, int $flags = 0): array + { + return $this->originalReflectionProperty->getAttributes($name, $flags); + } + /** * {@inheritDoc} * diff --git a/tests_php81/Persistence/Reflection/EnumReflectionPropertyTest.php b/tests_php81/Persistence/Reflection/EnumReflectionPropertyTest.php index bf5fed7a..a5f2b6f6 100644 --- a/tests_php81/Persistence/Reflection/EnumReflectionPropertyTest.php +++ b/tests_php81/Persistence/Reflection/EnumReflectionPropertyTest.php @@ -1,9 +1,13 @@ getValue($object)); } + public function testGetDeclaringClass(): void + { + $reflProperty = new EnumReflectionProperty(new ReflectionProperty(TypedEnumClass::class, 'suit'), Suit::class); + self::assertSame(TypedEnumClass::class, $reflProperty->getDeclaringClass()->getName()); + } + + public function testGetName(): void + { + $reflProperty = new EnumReflectionProperty(new ReflectionProperty(TypedEnumClass::class, 'suit'), Suit::class); + self::assertSame('suit', $reflProperty->getName()); + } + + public function testGetType(): void + { + $reflProperty = new EnumReflectionProperty(new ReflectionProperty(TypedEnumClass::class, 'suit'), Suit::class); + $type = $reflProperty->getType(); + self::assertInstanceOf(ReflectionNamedType::class, $type); + self::assertSame(Suit::class, $type->getName()); + } + + public function testGetAttributes(): void + { + $reflProperty = new EnumReflectionProperty(new ReflectionProperty(TypedEnumClass::class, 'suit'), Suit::class); + self::assertCount(1, $reflProperty->getAttributes()); + self::assertSame(MyAttribute::class, $reflProperty->getAttributes()[0]->getName()); + } + public function testSetValidValue(): void { $object = new TypedEnumClass(); @@ -84,8 +115,14 @@ public function testSetEnumArray(): void } } +#[Attribute(Attribute::TARGET_PROPERTY)] +class MyAttribute +{ +} + class TypedEnumClass { + #[MyAttribute] public ?Suit $suit = null; public ?array $suits = null; @@ -93,8 +130,8 @@ class TypedEnumClass enum Suit: string { - case Hearts = 'H'; + case Hearts = 'H'; case Diamonds = 'D'; - case Clubs = 'C'; - case Spades = 'S'; + case Clubs = 'C'; + case Spades = 'S'; }