Skip to content

Commit

Permalink
Add check for PHP version for backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyRatFans committed Apr 1, 2022
1 parent e1de222 commit 21f2a6b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/DoctrineObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use function get_class_methods;
use function gettype;
use function in_array;
use function interface_exists;
use function is_array;
use function is_callable;
use function is_int;
Expand Down Expand Up @@ -320,7 +319,7 @@ public function hydrateValue(string $name, $value, ?array $data = null)
return null;
}

if (interface_exists('BackedEnum') && $value instanceof BackedEnum) {
if (PHP_VERSION_ID >= 80100 && $value instanceof BackedEnum) {
return $value;
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Assets/SimpleEntityWithEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

class SimpleEntityWithEnum
{
protected int $id;
/** @var int */
protected $id;

protected ?SimpleEnum $enum;
/** @var SimpleEnum|null */
protected $enum;

public function setId(int $id): void
{
Expand Down
12 changes: 8 additions & 4 deletions tests/Assets/SimpleEnumStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class SimpleEnumStrategy implements StrategyInterface
{
/**
* @param mixed $value
*
* @return int|null
*/
public function extract($value, ?object $object = null): int|null
public function extract($value, ?object $object = null)
{
if ($value === null) {
return null;
Expand All @@ -21,10 +23,12 @@ public function extract($value, ?object $object = null): int|null
}

/**
* @param mixed $value
* @param array<array-key, mixed>|null $data
* @param mixed $value
* @param array<array-key, mixed>|null $data
*
* @return int|null
*/
public function hydrate($value, ?array $data): SimpleEnum|null
public function hydrate($value, ?array $data)
{
if ($value === null) {
return null;
Expand Down
14 changes: 14 additions & 0 deletions tests/DoctrineObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use function implode;
use function time;

use const PHP_VERSION_ID;

class DoctrineObjectTest extends TestCase
{
use ProphecyTrait;
Expand Down Expand Up @@ -2927,6 +2929,10 @@ public function testNestedHydrationByReference()

public function testHandleEnumConversionUsingByValue(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('PHP 8.1 required for enum compatibility');
}

// When using hydration by value, it will use the public API of the entity to set values (setters)
$entity = new Assets\SimpleEntityWithEnum();
$this->configureObjectManagerForSimpleEntityWithEnum();
Expand All @@ -2943,6 +2949,10 @@ public function testHandleEnumConversionUsingByValue(): void

public function testNullValueIsNotConvertedToEnum(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('PHP 8.1 required for enum compatibility');
}

$entity = new Assets\SimpleEntityWithEnum();
$this->configureObjectManagerForSimpleEntityWithEnum();

Expand All @@ -2956,6 +2966,10 @@ public function testNullValueIsNotConvertedToEnum(): void

public function testWrongEnumBackedValueThrowsException(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('PHP 8.1 required for enum compatibility');
}

$entity = new Assets\SimpleEntityWithEnum();
$this->configureObjectManagerForSimpleEntityWithEnum();

Expand Down

0 comments on commit 21f2a6b

Please sign in to comment.