Skip to content

Commit

Permalink
Merge pull request #348 from greg0ire/missing-methods
Browse files Browse the repository at this point in the history
Add missing proxy methods
  • Loading branch information
greg0ire authored Mar 1, 2024
2 parents a55fc31 + 5bed91c commit b6fd1f1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\\>\\|BackedEnum but returns array\\<BackedEnum\\|int\\|string\\>\\.$#"
count: 1
Expand Down
40 changes: 40 additions & 0 deletions src/Persistence/Reflection/EnumReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Doctrine\Persistence\Reflection;

use BackedEnum;
use ReflectionClass;
use ReflectionProperty;
use ReflectionType;
use ReturnTypeWillChange;

use function array_map;
Expand All @@ -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}
*
Expand Down
43 changes: 40 additions & 3 deletions tests_php81/Persistence/Reflection/EnumReflectionPropertyTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests_PHP81\Persistence\Reflection;

use Attribute;
use Doctrine\Persistence\Reflection\EnumReflectionProperty;
use PHPUnit\Framework\TestCase;
use ReflectionNamedType;
use ReflectionProperty;
use ValueError;

Expand All @@ -20,6 +24,33 @@ public function testGetValue(): void
self::assertNull($reflProperty->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();
Expand Down Expand Up @@ -84,17 +115,23 @@ public function testSetEnumArray(): void
}
}

#[Attribute(Attribute::TARGET_PROPERTY)]
class MyAttribute
{
}

class TypedEnumClass
{
#[MyAttribute]
public ?Suit $suit = null;

public ?array $suits = null;
}

enum Suit: string
{
case Hearts = 'H';
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
case Clubs = 'C';
case Spades = 'S';
}

0 comments on commit b6fd1f1

Please sign in to comment.