Skip to content

Keep PHP 8.1 enums as is #171

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

Merged
merged 6 commits into from
Mar 3, 2022
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
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