diff --git a/docs/api/classes/Ngmy-Enum-Enum.html b/docs/api/classes/Ngmy-Enum-Enum.html
index 229d5f0..22473d8 100644
--- a/docs/api/classes/Ngmy-Enum-Enum.html
+++ b/docs/api/classes/Ngmy-Enum-Enum.html
@@ -118,7 +118,7 @@
$names
- : list<string|int, string>
+ : array<string, list<string|int, string>>
@@ -188,7 +188,7 @@
values()
- : list<string|int, self>
+ : list<string|int, static>
Returns all constants of this enum type.
@@ -241,7 +241,7 @@
@@ -272,19 +272,34 @@
private
- static list<string|int, string>
+ static array<string, list<string|int, string>>
$names
-
+ = []
+
+
+ -
+ phpstan-var
+
+ -
+
+
array<class-string, list>
+
+
+
+
@@ -307,7 +322,7 @@
Returns the enum constant of the specified name.
@@ -359,7 +374,7 @@
@@ -410,7 +425,7 @@
Returns the name of this enum constant, as contained in the declaration.
@@ -444,7 +459,7 @@
@@ -476,7 +491,7 @@
Returns true if the specified object is equal to this enum constant.
@@ -520,7 +535,7 @@ Returns the hash code for this enum constant
@@ -554,7 +569,7 @@ Returns the name of this enum constant, exactly as declared in its enum declaration.
@@ -588,7 +603,7 @@ Returns the ordinal of this enum constant.
@@ -623,7 +638,7 @@ Returns the enum constant of the specified name.
@@ -667,14 +682,14 @@ Returns all constants of this enum type.
public
- final static values() : list<string|int, self>
+ final static values() : list<string|int, static>
@@ -682,7 +697,7 @@
Return values
- list<string|int, self>
+ list<string|int, static>
—
@@ -701,7 +716,7 @@
@@ -734,7 +749,7 @@
@@ -776,7 +791,7 @@
diff --git a/src/Enum.php b/src/Enum.php
index a66b579..de5a49e 100644
--- a/src/Enum.php
+++ b/src/Enum.php
@@ -11,8 +11,11 @@
abstract class Enum
{
- /** @var list */
- private static $names;
+ /**
+ * @var array>
+ * @phpstan-var array>
+ */
+ private static $names = [];
/** @var string */
private $name;
@@ -41,7 +44,7 @@ final public static function valueOf(string $name): self
/**
* Returns all constants of this enum type.
*
- * @return list
+ * @return list
*/
final public static function values(): array
{
@@ -59,11 +62,12 @@ final public static function values(): array
final public static function names(): array
{
self::validateInheritance();
- if (isset(self::$names)) {
- return self::$names;
+ $class = \get_called_class();
+ if (isset(self::$names[$class])) {
+ return self::$names[$class];
}
- self::$names = [];
- $reflectionClass = new ReflectionClass(\get_called_class());
+ self::$names[$class] = [];
+ $reflectionClass = new ReflectionClass($class);
$staticProperties = $reflectionClass->getStaticProperties();
foreach (\array_keys($staticProperties) as $propertyName) {
$reflectionProperty = $reflectionClass->getProperty($propertyName);
@@ -76,10 +80,10 @@ final public static function names(): array
if (!\preg_match('/@[^\s]+/', $line, $mathces) || $mathces[0] != '@enum') {
continue;
}
- self::$names[] = $propertyName;
+ self::$names[$class][] = $propertyName;
}
}
- return self::$names;
+ return self::$names[$class];
}
/**
diff --git a/tests/Data/Enum7.php b/tests/Data/Enum7.php
new file mode 100644
index 0000000..c27f3d3
--- /dev/null
+++ b/tests/Data/Enum7.php
@@ -0,0 +1,47 @@
+getValue() == $value) {
+ return $enum;
+ }
+ }
+ throw new InvalidArgumentException('The value "%s" is invalid.');
+ }
+
+ public function getValue(): int
+ {
+ return self::${$this->name()};
+ }
+}
diff --git a/tests/Data/Enum8.php b/tests/Data/Enum8.php
new file mode 100644
index 0000000..e7e0bb8
--- /dev/null
+++ b/tests/Data/Enum8.php
@@ -0,0 +1,25 @@
+assertSame($expected, $fromCache);
}
+ /**
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ */
+ public function testNamesSameProcessAnotherClass(): void
+ {
+ $this->assertSame(['FOO', 'BAR', 'BAZ'], Data\Enum1::names());
+ $this->assertSame(['HOGE', 'FUGA'], Data\Enum8::names());
+ }
+
/**
* @return array>
*/
@@ -257,6 +267,34 @@ public function testHashCode(Enum $one, Enum $other, bool $expected): void
$this->assertSame($expected, $one->hashCode() === $other->hashCode());
}
+ /**
+ * @return array>
+ */
+ public function getInstanceProvider(): array
+ {
+ return [
+ [Data\Enum7::class, 1, Data\Enum7::valueOf('FOO')],
+ [Data\Enum7::class, 2, Data\Enum7::valueOf('BAR')],
+ [Data\Enum7::class, 3, Data\Enum7::valueOf('BAZ')],
+ [Data\Enum7::class, 4, new InvalidArgumentException()]
+ ];
+ }
+
+ /**
+ * @param mixed $value
+ * @param Enum|Exception $expected
+ * @dataProvider getInstanceProvider
+ *
+ * @phpstan-param class-string $class
+ */
+ public function testGetInstance(string $class, $value, $expected): void
+ {
+ if ($expected instanceof Exception) {
+ $this->expectException(\get_class($expected));
+ }
+ $this->assertEquals($expected, $class::getInstance($value));
+ }
+
/**
* @return array>
*/