Skip to content

Commit

Permalink
Add support for enum arguments to setValue()
Browse files Browse the repository at this point in the history
The ORM is relying on that feature.
  • Loading branch information
greg0ire committed Feb 27, 2024
1 parent 5ce8785 commit a102634
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
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: 13 additions & 1 deletion src/Persistence/Reflection/EnumReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,25 @@ 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[])
*/
private function toEnum($value)
{
if ($value instanceof BackedEnum) {
return $value;
}

Check warning on line 97 in src/Persistence/Reflection/EnumReflectionProperty.php

View check run for this annotation

Codecov / codecov/patch

src/Persistence/Reflection/EnumReflectionProperty.php#L97

Added line #L97 was not covered by tests

if (is_array($value)) {
foreach ($value as $v) {
if ($v instanceof BackedEnum) {
return $value;
}

Check warning on line 103 in src/Persistence/Reflection/EnumReflectionProperty.php

View check run for this annotation

Codecov / codecov/patch

src/Persistence/Reflection/EnumReflectionProperty.php#L103

Added line #L103 was not covered by tests

break;
}

Check warning on line 106 in src/Persistence/Reflection/EnumReflectionProperty.php

View check run for this annotation

Codecov / codecov/patch

src/Persistence/Reflection/EnumReflectionProperty.php#L106

Added line #L106 was not covered by tests

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 a102634

Please # to comment.