Skip to content

Commit

Permalink
Merge pull request #343 from greg0ire/add-support-for-set-value-with-…
Browse files Browse the repository at this point in the history
…enum

Add support for enum arguments to setValue()
  • Loading branch information
greg0ire authored Feb 29, 2024
2 parents 5ce8785 + e70bc39 commit 8961eec
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 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\\:\\:toEnum\\(\\) should return array\\<BackedEnum\\>\\|BackedEnum but returns array\\<BackedEnum\\|int\\|string\\>\\.$#"
count: 1
path: src/Persistence/Reflection/EnumReflectionProperty.php

-
message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(BackedEnum\\|int\\|string\\)\\: mixed\\)\\|null, array\\{class\\-string\\<BackedEnum\\>, 'from'\\} given\\.$#"
count: 1
path: src/Persistence/Reflection/EnumReflectionProperty.php

-
message: "#^Variable property access on \\$this\\(Doctrine\\\\Persistence\\\\Reflection\\\\TypedNoDefaultRuntimePublicReflectionProperty\\)\\.$#"
count: 1
Expand Down
8 changes: 8 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@
<code>$parentClass === false</code>
</TypeDoesNotContainType>
</file>
<file src="src/Persistence/Reflection/EnumReflectionProperty.php">
<InvalidReturnStatement occurrences="1">
<code>$value</code>
</InvalidReturnStatement>
<PossiblyInvalidArgument occurrences="1">
<code>$value</code>
</PossiblyInvalidArgument>
</file>
</files>
14 changes: 12 additions & 2 deletions src/Persistence/Reflection/EnumReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use function array_map;
use function is_array;
use function reset;

/**
* PHP Enum Reflection Property - special override for backed enums.
Expand Down Expand Up @@ -86,13 +87,22 @@ private function fromEnum($enum)
}

/**
* @param int|string|int[]|string[] $value
* @param int|string|int[]|string[]|BackedEnum|BackedEnum[] $value
*
* @return ($value is int|string ? BackedEnum : BackedEnum[])
* @return ($value is int|string|BackedEnum ? BackedEnum : BackedEnum[])
*/
private function toEnum($value)
{
if ($value instanceof BackedEnum) {
return $value;
}

if (is_array($value)) {
$v = reset($value);
if ($v instanceof BackedEnum) {
return $value;
}

return array_map([$this->enumType, 'from'], $value);
}

Expand Down
18 changes: 18 additions & 0 deletions tests_php81/Persistence/Reflection/EnumReflectionPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ public function testSetValidArrayValue(): void
self::assertSame(['H', 'D'], $reflProperty->getValue($object));
self::assertSame([Suit::Hearts, Suit::Diamonds], $object->suits);
}

public function testSetEnum(): void
{
$object = new TypedEnumClass();
$reflProperty = new EnumReflectionProperty(new ReflectionProperty(TypedEnumClass::class, 'suit'), Suit::class);
$reflProperty->setValue($object, Suit::Hearts);

self::assertSame(Suit::Hearts, $object->suit);
}

public function testSetEnumArray(): void
{
$object = new TypedEnumClass();
$reflProperty = new EnumReflectionProperty(new ReflectionProperty(TypedEnumClass::class, 'suits'), Suit::class);
$reflProperty->setValue($object, [Suit::Hearts, Suit::Diamonds]);

self::assertSame([Suit::Hearts, Suit::Diamonds], $object->suits);
}
}

class TypedEnumClass
Expand Down

0 comments on commit 8961eec

Please sign in to comment.