Skip to content

Commit f60028e

Browse files
committed
Keep PHP 8.1 enums as is
1 parent 8263fc3 commit f60028e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

fixtures/f012/Suit.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DeepCopy\f012;
4+
5+
enum Suit: string
6+
{
7+
case Hearts = 'Hearts';
8+
case Diamonds = 'Diamonds';
9+
case Clubs = 'Clubs';
10+
case Spades = 'Spades';
11+
}

src/DeepCopy/DeepCopy.php

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ private function recursiveCopy($var)
140140
return $var;
141141
}
142142

143+
// PHP 8.1 Enum
144+
if (function_exists('enum_exists') && enum_exists($var::class)) {
145+
return $var;
146+
}
147+
143148
// Object
144149
return $this->copyObject($var);
145150
}

tests/DeepCopyTest/DeepCopyTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use DeepCopy\f008;
2020
use DeepCopy\f009;
2121
use DeepCopy\f011;
22+
use DeepCopy\f012\Suit;
2223
use DeepCopy\Filter\KeepFilter;
2324
use DeepCopy\Filter\SetNullFilter;
2425
use DeepCopy\Matcher\PropertyNameMatcher;
@@ -495,6 +496,18 @@ public function test_it_ignores_uninitialized_typed_properties()
495496
$this->assertFalse(isset($copy->foo));
496497
}
497498

499+
/**
500+
* @requires PHP 8.1
501+
*/
502+
public function test_it_keeps_enums()
503+
{
504+
$enum = Suit::Clubs;
505+
506+
$copy = (new DeepCopy())->copy($enum);
507+
508+
$this->assertSame($enum, $copy);
509+
}
510+
498511
private function assertEqualButNotSame($expected, $val)
499512
{
500513
$this->assertEquals($expected, $val);

0 commit comments

Comments
 (0)