Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion src/main/php/lang/reflection/Type.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
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 && 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() => 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