Skip to content

Commit

Permalink
Merge pull request myclabs#171 from Slamdunk/enum
Browse files Browse the repository at this point in the history
Keep PHP 8.1 enums as is
  • Loading branch information
mnapoli authored Mar 3, 2022
2 parents c6a951b + 0736438 commit 14daed4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions fixtures/f012/Suit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace DeepCopy\f012;

enum Suit: string
{
case Hearts = 'Hearts';
case Diamonds = 'Diamonds';
case Clubs = 'Clubs';
case Spades = 'Spades';
}
5 changes: 5 additions & 0 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ private function recursiveCopy($var)
return $var;
}

// Enum
if (PHP_VERSION_ID >= 80100 && enum_exists(get_class($var))) {
return $var;
}

// Object
return $this->copyObject($var);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use DeepCopy\f008;
use DeepCopy\f009;
use DeepCopy\f011;
use DeepCopy\f012\Suit;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Filter\SetNullFilter;
use DeepCopy\Matcher\PropertyNameMatcher;
Expand Down Expand Up @@ -495,6 +496,18 @@ public function test_it_ignores_uninitialized_typed_properties()
$this->assertFalse(isset($copy->foo));
}

/**
* @requires PHP 8.1
*/
public function test_it_keeps_enums()
{
$enum = Suit::Clubs;

$copy = (new DeepCopy())->copy($enum);

$this->assertSame($enum, $copy);
}

private function assertEqualButNotSame($expected, $val)
{
$this->assertEquals($expected, $val);
Expand Down

0 comments on commit 14daed4

Please sign in to comment.