Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for enum arguments to setValue() #343

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}

/**
* @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;
}

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

View check run for this annotation

Codecov / codecov/patch

src/Persistence/Reflection/EnumReflectionProperty.php#L98

Added line #L98 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/Persistence/Reflection/EnumReflectionProperty.php#L104

Added line #L104 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
Loading