Skip to content

Support PHP 8.1 native enums #10

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 4 commits into from
Mar 13, 2021
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/reflection/Type.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 33 additions & 3 deletions src/test/php/lang/reflection/unittest/TypeTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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'));
Expand Down