From dace345ed54a1c86f2d9a6a39316a3ced4cd920f Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Mon, 8 Mar 2021 15:10:01 +0900 Subject: [PATCH 1/3] Fix the type --- src/Enum.php | 2 +- tests/Data/Enum7.php | 47 ++++++++++++++++++++++++++++++++++++++++++++ tests/EnumTest.php | 28 ++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/Data/Enum7.php diff --git a/src/Enum.php b/src/Enum.php index a66b579..828de23 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -41,7 +41,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 { 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/EnumTest.php b/tests/EnumTest.php index 77e7ce1..52bba94 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -257,6 +257,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> */ From 5b77a085ea2b6f73db62ee1b97ba3e0d8ec3bbf6 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Mon, 8 Mar 2021 15:34:22 +0900 Subject: [PATCH 2/3] Fix the bug --- src/Enum.php | 20 ++++++++++++-------- tests/Data/Enum8.php | 25 +++++++++++++++++++++++++ tests/EnumTest.php | 10 ++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 tests/Data/Enum8.php diff --git a/src/Enum.php b/src/Enum.php index 828de23..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; @@ -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/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> */ From 829a2def7669c95f45050f608824dd6649142ff6 Mon Sep 17 00:00:00 2001 From: Yuta Nagamiya Date: Mon, 8 Mar 2021 16:38:44 +0900 Subject: [PATCH 3/3] Generate the API documentation --- docs/api/classes/Ngmy-Enum-Enum.html | 57 ++++++++++++++++++---------- 1 file changed, 36 insertions(+), 21 deletions(-) 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 - + = []
+

+ Tags + +
+
+
+ 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 @@