diff --git a/composer.json b/composer.json index bed11a5..37376cd 100755 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "description" : "Reflection", "keywords": ["module", "xp"], "require" : { - "xp-framework/core": "^10.6", + "xp-framework/core": "^10.8", "xp-framework/ast": "^7.0", "php" : ">=7.0.0" }, diff --git a/src/main/php/lang/reflection/Type.class.php b/src/main/php/lang/reflection/Type.class.php index ec18677..01dc823 100755 --- a/src/main/php/lang/reflection/Type.class.php +++ b/src/main/php/lang/reflection/Type.class.php @@ -53,7 +53,7 @@ public function kind(): Kind { return Kind::$INTERFACE; } else if ($this->reflect->isTrait()) { return Kind::$TRAIT; - } else if ($this->reflect->isSubclassOf(Enum::class)) { + } else if ($this->reflect->isSubclassOf(Enum::class) || $this->reflect->isSubclassOf(\UnitEnum::class)) { return Kind::$ENUM; } else { return Kind::$CLASS; diff --git a/src/test/php/lang/reflection/unittest/TypeTest.class.php b/src/test/php/lang/reflection/unittest/TypeTest.class.php index 03baac5..f254e60 100755 --- a/src/test/php/lang/reflection/unittest/TypeTest.class.php +++ b/src/test/php/lang/reflection/unittest/TypeTest.class.php @@ -2,10 +2,16 @@ use lang\reflection\{Kind, Modifiers, Annotations, Constants, Properties, Methods, Package}; use lang\{ElementNotFoundException, Reflection, Enum, Runnable, XPClass, ClassLoader}; -use unittest\{Assert, Before, Test}; +use unittest\actions\VerifyThat; +use unittest\{Action, Assert, Before, Test}; class TypeTest { private $fixture; + private static $ENUMS; + + static function __static() { + self::$ENUMS= class_exists(\ReflectionEnum::class, false); + } /** * Declares a type and returns its reflection instance @@ -91,8 +97,20 @@ public function trait_kind() { } #[Test] - public function enum_kind() { - $t= $this->declare('K_E', ['kind' => 'class', 'extends' => [Enum::class]], '{ public static $M; }'); + public function enum_kind_for_xpenums() { + $t= $this->declare('K_XE', ['kind' => 'class', 'extends' => [Enum::class]], '{ public static $M; }'); + Assert::equals(Kind::$ENUM, $t->kind()); + } + + #[Test, Action(eval: 'new VerifyThat(fn() => !self::$ENUMS)')] + public function enum_kind_for_enum_lookalikes() { + $t= $this->declare('K_LE', ['kind' => 'class', 'implements' => [\UnitEnum::class]], '{ public static $M; }'); + Assert::equals(Kind::$ENUM, $t->kind()); + } + + #[Test, Action(eval: 'new VerifyThat(fn() => self::$ENUMS)')] + public function enum_kind_for_native_enums() { + $t= $this->declare('K_NE', ['kind' => 'enum'], '{ case M; }'); Assert::equals(Kind::$ENUM, $t->kind()); } @@ -177,6 +195,18 @@ public function annotation() { Assert::equals('annotated', $this->fixture->annotation(Annotated::class)->name()); } + #[Test, Action(eval: 'new VerifyThat(fn() => self::$ENUMS)')] + public function enum_annotation() { + $t= $this->declare('A_E', ['kind' => 'enum'], '{ case M; }'); + Assert::equals('annotated', $t->annotation(Annotated::class)->name()); + } + + #[Test, Action(eval: 'new VerifyThat(fn() => self::$ENUMS)')] + public function enum_case_annotation() { + $t= $this->declare('A_C', ['kind' => 'enum'], '{ #[Annotated] case M; }'); + Assert::equals('annotated', $t->constant('M')->annotation(Annotated::class)->name()); + } + #[Test] public function non_existant_annotation() { Assert::null($this->fixture->annotation('does-not-exist'));