From 6ba0d7f8295d637d9e4e19e8b0e711d4ace42777 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 13 Mar 2021 16:17:00 +0100 Subject: [PATCH 1/4] Support PHP 8.1 native enums --- src/main/php/lang/reflection/Type.class.php | 7 ++++++- .../reflection/unittest/TypeTest.class.php | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/php/lang/reflection/Type.class.php b/src/main/php/lang/reflection/Type.class.php index ec18677..fdf513e 100755 --- a/src/main/php/lang/reflection/Type.class.php +++ b/src/main/php/lang/reflection/Type.class.php @@ -10,6 +10,11 @@ class Type { private $reflect; private $annotations= null; + private static $ENUMS; + + static function __static() { + self::$ENUMS= interface_exists(\UnitEnum::class, false); + } /** @param ReflectionClass $reflect */ public function __construct($reflect) { @@ -53,7 +58,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) || (self::$ENUMS && $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..44ea448 100755 --- a/src/test/php/lang/reflection/unittest/TypeTest.class.php +++ b/src/test/php/lang/reflection/unittest/TypeTest.class.php @@ -2,7 +2,8 @@ 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; @@ -91,8 +92,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() => !class_exists(\ReflectionEnum::class, false) && interface_exists(\UnitEnum::class, false))')] + 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() => class_exists(\ReflectionEnum::class, false))')] + public function enum_kind_for_native_enums() { + $t= $this->declare('K_NE', ['kind' => 'enum'], '{ case M; }'); Assert::equals(Kind::$ENUM, $t->kind()); } From be8b1f9751bdb82ebaaa5401436e0abfe6407ba9 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 13 Mar 2021 16:26:38 +0100 Subject: [PATCH 2/4] Verify enum cases can be annotated --- .../reflection/unittest/TypeTest.class.php | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/test/php/lang/reflection/unittest/TypeTest.class.php b/src/test/php/lang/reflection/unittest/TypeTest.class.php index 44ea448..5b48330 100755 --- a/src/test/php/lang/reflection/unittest/TypeTest.class.php +++ b/src/test/php/lang/reflection/unittest/TypeTest.class.php @@ -7,6 +7,11 @@ 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 @@ -97,13 +102,13 @@ public function enum_kind_for_xpenums() { Assert::equals(Kind::$ENUM, $t->kind()); } - #[Test, Action(eval: 'new VerifyThat(fn() => !class_exists(\ReflectionEnum::class, false) && interface_exists(\UnitEnum::class, false))')] + #[Test, Action(eval: 'new VerifyThat(fn() => !self::$ENUMS && interface_exists(\UnitEnum::class, false))')] 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() => class_exists(\ReflectionEnum::class, false))')] + #[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()); @@ -190,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')); From 9ccadb670d015445d0bb72846b2a4e35c1937c89 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 13 Mar 2021 16:30:25 +0100 Subject: [PATCH 3/4] Bump minimum requirement on XP core to 10.8.0, remove compatibility code --- composer.json | 2 +- src/main/php/lang/reflection/Type.class.php | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) 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 fdf513e..01dc823 100755 --- a/src/main/php/lang/reflection/Type.class.php +++ b/src/main/php/lang/reflection/Type.class.php @@ -10,11 +10,6 @@ class Type { private $reflect; private $annotations= null; - private static $ENUMS; - - static function __static() { - self::$ENUMS= interface_exists(\UnitEnum::class, false); - } /** @param ReflectionClass $reflect */ public function __construct($reflect) { @@ -58,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) || (self::$ENUMS && $this->reflect->isSubclassOf(\UnitEnum::class))) { + } else if ($this->reflect->isSubclassOf(Enum::class) || $this->reflect->isSubclassOf(\UnitEnum::class)) { return Kind::$ENUM; } else { return Kind::$CLASS; From 8c06bdf8ac2f4d310ad8d0199b94ae4aa8bac4a9 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 13 Mar 2021 16:31:59 +0100 Subject: [PATCH 4/4] Remove superfluous check for \UnitEnum::class It will always be available now that we require XP core 10.8+ --- src/test/php/lang/reflection/unittest/TypeTest.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/php/lang/reflection/unittest/TypeTest.class.php b/src/test/php/lang/reflection/unittest/TypeTest.class.php index 5b48330..f254e60 100755 --- a/src/test/php/lang/reflection/unittest/TypeTest.class.php +++ b/src/test/php/lang/reflection/unittest/TypeTest.class.php @@ -102,7 +102,7 @@ public function enum_kind_for_xpenums() { Assert::equals(Kind::$ENUM, $t->kind()); } - #[Test, Action(eval: 'new VerifyThat(fn() => !self::$ENUMS && interface_exists(\UnitEnum::class, false))')] + #[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());