Skip to content

Commit 29f041f

Browse files
authored
Merge pull request #10 from xp-framework/feature/php-enum-support
Support PHP 8.1 native enums
2 parents 7bd48a1 + 8c06bdf commit 29f041f

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"description" : "Reflection",
77
"keywords": ["module", "xp"],
88
"require" : {
9-
"xp-framework/core": "^10.6",
9+
"xp-framework/core": "^10.8",
1010
"xp-framework/ast": "^7.0",
1111
"php" : ">=7.0.0"
1212
},

src/main/php/lang/reflection/Type.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function kind(): Kind {
5353
return Kind::$INTERFACE;
5454
} else if ($this->reflect->isTrait()) {
5555
return Kind::$TRAIT;
56-
} else if ($this->reflect->isSubclassOf(Enum::class)) {
56+
} else if ($this->reflect->isSubclassOf(Enum::class) || $this->reflect->isSubclassOf(\UnitEnum::class)) {
5757
return Kind::$ENUM;
5858
} else {
5959
return Kind::$CLASS;

src/test/php/lang/reflection/unittest/TypeTest.class.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
use lang\reflection\{Kind, Modifiers, Annotations, Constants, Properties, Methods, Package};
44
use lang\{ElementNotFoundException, Reflection, Enum, Runnable, XPClass, ClassLoader};
5-
use unittest\{Assert, Before, Test};
5+
use unittest\actions\VerifyThat;
6+
use unittest\{Action, Assert, Before, Test};
67

78
class TypeTest {
89
private $fixture;
10+
private static $ENUMS;
11+
12+
static function __static() {
13+
self::$ENUMS= class_exists(\ReflectionEnum::class, false);
14+
}
915

1016
/**
1117
* Declares a type and returns its reflection instance
@@ -91,8 +97,20 @@ public function trait_kind() {
9197
}
9298

9399
#[Test]
94-
public function enum_kind() {
95-
$t= $this->declare('K_E', ['kind' => 'class', 'extends' => [Enum::class]], '{ public static $M; }');
100+
public function enum_kind_for_xpenums() {
101+
$t= $this->declare('K_XE', ['kind' => 'class', 'extends' => [Enum::class]], '{ public static $M; }');
102+
Assert::equals(Kind::$ENUM, $t->kind());
103+
}
104+
105+
#[Test, Action(eval: 'new VerifyThat(fn() => !self::$ENUMS)')]
106+
public function enum_kind_for_enum_lookalikes() {
107+
$t= $this->declare('K_LE', ['kind' => 'class', 'implements' => [\UnitEnum::class]], '{ public static $M; }');
108+
Assert::equals(Kind::$ENUM, $t->kind());
109+
}
110+
111+
#[Test, Action(eval: 'new VerifyThat(fn() => self::$ENUMS)')]
112+
public function enum_kind_for_native_enums() {
113+
$t= $this->declare('K_NE', ['kind' => 'enum'], '{ case M; }');
96114
Assert::equals(Kind::$ENUM, $t->kind());
97115
}
98116

@@ -177,6 +195,18 @@ public function annotation() {
177195
Assert::equals('annotated', $this->fixture->annotation(Annotated::class)->name());
178196
}
179197

198+
#[Test, Action(eval: 'new VerifyThat(fn() => self::$ENUMS)')]
199+
public function enum_annotation() {
200+
$t= $this->declare('A_E', ['kind' => 'enum'], '{ case M; }');
201+
Assert::equals('annotated', $t->annotation(Annotated::class)->name());
202+
}
203+
204+
#[Test, Action(eval: 'new VerifyThat(fn() => self::$ENUMS)')]
205+
public function enum_case_annotation() {
206+
$t= $this->declare('A_C', ['kind' => 'enum'], '{ #[Annotated] case M; }');
207+
Assert::equals('annotated', $t->constant('M')->annotation(Annotated::class)->name());
208+
}
209+
180210
#[Test]
181211
public function non_existant_annotation() {
182212
Assert::null($this->fixture->annotation('does-not-exist'));

0 commit comments

Comments
 (0)