From 5cdcc30b105d3f1257668acf255baa73020a47e3 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 06:54:57 +0100 Subject: [PATCH 01/14] Add .idea folder to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a683006..e3d8217 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ composer.lock vendor/ .php-cs-fixer.cache .vscode/ +.idea/ coverage/ node_modules From 25b8f405d5b233479989c86f7342f82480abbee4 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 06:55:13 +0100 Subject: [PATCH 02/14] Remove useless concatenation --- src/From.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/From.php b/src/From.php index 2ac8853..07f7c94 100644 --- a/src/From.php +++ b/src/From.php @@ -37,7 +37,7 @@ public static function tryFrom(string $case): ?static */ public static function fromName(string $case): static { - return static::tryFromName($case) ?? throw new ValueError('"' . $case . '" is not a valid name for enum ' . static::class . ''); + return static::tryFromName($case) ?? throw new ValueError('"' . $case . '" is not a valid name for enum ' . static::class); } /** From e6f788fd9fcca62a84529f7438e3e631ee2444ae Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:07:05 +0100 Subject: [PATCH 03/14] Add phpunit cache directory to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e3d8217..f58ae8f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ vendor/ .php-cs-fixer.cache .vscode/ .idea/ +.phpunit.cache/ coverage/ node_modules From 050057a70811bc9f60ef31cfebac54bb84faa855 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:07:26 +0100 Subject: [PATCH 04/14] Use iterable type instead of unnecessary checks --- src/Comparable.php | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/Comparable.php b/src/Comparable.php index c655912..116178a 100644 --- a/src/Comparable.php +++ b/src/Comparable.php @@ -20,21 +20,9 @@ public function isNot(mixed $enum): bool return ! $this->is($enum); } - public function in(array|object $enums): bool + public function in(iterable $enums): bool { - $iterator = $enums; - - if (! is_array($enums)) { - if ($enums instanceof Iterator) { - $iterator = $enums; - } elseif ($enums instanceof IteratorAggregate) { - $iterator = $enums->getIterator(); - } else { - throw new Exception('in() expects an iterable value'); - } - } - - foreach ($iterator as $item) { + foreach ($enums as $item) { if ($item === $this) { return true; } @@ -43,7 +31,7 @@ public function in(array|object $enums): bool return false; } - public function notIn(array|object $enums): bool + public function notIn(iterable $enums): bool { return ! $this->in($enums); } From 6d6303d276e28c2fbd21f7ba7c3aad2ae66876e8 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:19:07 +0100 Subject: [PATCH 05/14] Remove unnecessary null coalescing If the return value is `null`, it will return `null` anyway. If the method doesn't exist, it will throw an `Error` because of the undefined method. --- src/Meta/Reflection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Meta/Reflection.php b/src/Meta/Reflection.php index 929ac61..82c3662 100644 --- a/src/Meta/Reflection.php +++ b/src/Meta/Reflection.php @@ -73,6 +73,6 @@ public static function metaValue(string $metaProperty, mixed $enum): mixed return $properties[0]->value; } - return $metaProperty::defaultValue() ?? null; + return $metaProperty::defaultValue(); } } From ca36e6dbe429bf73af469598e6ce59fa83af6ba6 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:19:16 +0100 Subject: [PATCH 06/14] Add typehint --- src/Meta/Reflection.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Meta/Reflection.php b/src/Meta/Reflection.php index 82c3662..2f8ae82 100644 --- a/src/Meta/Reflection.php +++ b/src/Meta/Reflection.php @@ -51,6 +51,7 @@ protected static function parseMetaProperties(ReflectionClass $reflection): arra /** * Get the value of a meta property on the provided enum. * + * @param class-string $metaProperty * @param \Enum $enum */ public static function metaValue(string $metaProperty, mixed $enum): mixed From 5db99971491d4fc8f3e838cef97ea72057221a8f Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:19:51 +0100 Subject: [PATCH 07/14] Merge meta properties in one go instead of in a loop --- src/Meta/Reflection.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Meta/Reflection.php b/src/Meta/Reflection.php index 2f8ae82..18fe88a 100644 --- a/src/Meta/Reflection.php +++ b/src/Meta/Reflection.php @@ -25,11 +25,12 @@ public static function metaProperties(mixed $enum): array // Traits except the `Metadata` trait $traits = array_values(array_filter($reflection->getTraits(), fn (ReflectionClass $class) => $class->getName() !== 'ArchTech\Enums\Metadata')); - foreach ($traits as $trait) { - $metaProperties = array_merge($metaProperties, static::parseMetaProperties($trait)); - } + $traitsMeta = array_map( + fn (ReflectionClass $trait) => static::parseMetaProperties($trait), + $traits + ); - return $metaProperties; + return array_merge($metaProperties, ...$traitsMeta); } /** @param ReflectionClass $reflection */ From 2e5c94b4ecf4fb60bb5065c0b523346f399132d4 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:25:31 +0100 Subject: [PATCH 08/14] Simplify control flow --- src/Options.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Options.php b/src/Options.php index 25e141f..037f4e0 100644 --- a/src/Options.php +++ b/src/Options.php @@ -29,13 +29,11 @@ public static function stringOptions(?Closure $callback = null, string $glue = ' if ($firstCase === null) { return ''; - } elseif ($firstCase instanceof BackedEnum) { - // [name => value] - $options = static::options(); - } else { - // [name, name] - $options = static::options(); + } + // [name, name] + $options = static::options(); + if (!$firstCase instanceof BackedEnum) { // [name => name, name => name] $options = array_combine($options, $options); } From aef2f724ba64967692f94b677dccd15883b34460 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:34:05 +0100 Subject: [PATCH 09/14] Use non-deprecated phpstan configuration value --- phpstan.neon | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 3fd1979..b4b7b9e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -25,5 +25,4 @@ parameters: # paths: # - tests/* # - '#should return \$this#' - - checkMissingIterableValueType: false + - identifier: missingType.iterableValue From d669fa14aff31ea082e269c30b3727048a6b424d Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:43:50 +0100 Subject: [PATCH 10/14] Add missing types --- src/InvokableCases.php | 2 +- src/Metadata.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/InvokableCases.php b/src/InvokableCases.php index b5a2150..5ba92fd 100644 --- a/src/InvokableCases.php +++ b/src/InvokableCases.php @@ -15,7 +15,7 @@ public function __invoke() } /** Return the enum's value or name when it's called ::STATICALLY(). */ - public static function __callStatic($name, $args) + public static function __callStatic(string $name, array $args) { $cases = static::cases(); diff --git a/src/Metadata.php b/src/Metadata.php index 9232e94..69d6036 100644 --- a/src/Metadata.php +++ b/src/Metadata.php @@ -30,7 +30,7 @@ public static function fromMeta(MetaProperty $metaProperty): static ); } - public function __call(string $property, $arguments): mixed + public function __call(string $property, array $arguments): mixed { $metaProperties = Meta\Reflection::metaProperties($this); From 633f1703a055fc098afc6eebfefb451637540dfd Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:55:56 +0100 Subject: [PATCH 11/14] Fix style with php-cs-fixer --- src/Comparable.php | 4 ---- src/Options.php | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Comparable.php b/src/Comparable.php index 116178a..b118ed2 100644 --- a/src/Comparable.php +++ b/src/Comparable.php @@ -4,10 +4,6 @@ namespace ArchTech\Enums; -use Exception; -use Iterator; -use IteratorAggregate; - trait Comparable { public function is(mixed $enum): bool diff --git a/src/Options.php b/src/Options.php index 037f4e0..0692ed6 100644 --- a/src/Options.php +++ b/src/Options.php @@ -33,7 +33,7 @@ public static function stringOptions(?Closure $callback = null, string $glue = ' // [name, name] $options = static::options(); - if (!$firstCase instanceof BackedEnum) { + if (! $firstCase instanceof BackedEnum) { // [name => name, name => name] $options = array_combine($options, $options); } From 06b17354afad132f9d5bfd575d59ecdb34751a55 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 08:55:03 +0100 Subject: [PATCH 12/14] Add tests that satisfy the ArrayIterator branch --- tests/Pest/ComparableTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Pest/ComparableTest.php b/tests/Pest/ComparableTest.php index 11c980f..2da6e5f 100644 --- a/tests/Pest/ComparableTest.php +++ b/tests/Pest/ComparableTest.php @@ -23,6 +23,11 @@ expect(Status::PENDING->in([Status::PENDING, Status::DONE]))->toBeTrue(); expect(Role::ADMIN->in([Role::ADMIN]))->toBeTrue(); + $iterator = new ArrayIterator([Status::PENDING, Status::DONE]); + expect(Status::PENDING->in($iterator))->toBeTrue(); + expect(Status::DONE->in($iterator))->toBeTrue(); + expect(Status::PENDING->in(new ArrayIterator([Role::ADMIN, Role::GUEST])))->toBeFalse(); + expect(Status::PENDING->in([Status::DONE]))->toBeFalse(); expect(Status::PENDING->in([Role::ADMIN, Role::GUEST]))->toBeFalse(); }); From bdd8de68ff2525010763effd484ebb7dc419879c Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Sun, 29 Dec 2024 08:56:50 +0100 Subject: [PATCH 13/14] Use is() for comparison in in() --- src/Comparable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Comparable.php b/src/Comparable.php index b118ed2..deca592 100644 --- a/src/Comparable.php +++ b/src/Comparable.php @@ -19,7 +19,7 @@ public function isNot(mixed $enum): bool public function in(iterable $enums): bool { foreach ($enums as $item) { - if ($item === $this) { + if ($this->is($item)) { return true; } } From 859fb9a03cb5c6f47fda29fc84bbb91f5b8d42fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 7 Jun 2025 01:03:01 +0200 Subject: [PATCH 14/14] from(int|string), tryFrom(int|string) --- src/From.php | 4 ++-- tests/PHPStan/InvokableCases/InvokableCasesTestCase.php | 2 ++ tests/PHPStan/InvokableCasesTest.php | 2 ++ tests/Pest.php | 2 ++ tests/Pest/ComparableTest.php | 2 ++ tests/Pest/FromTest.php | 2 ++ tests/Pest/InvokableCasesTest.php | 2 ++ tests/Pest/MetadataTest.php | 2 ++ tests/Pest/NamesTest.php | 2 ++ tests/Pest/OptionsTest.php | 2 ++ tests/Pest/TraitTest.php | 2 ++ tests/Pest/ValuesTest.php | 2 ++ tests/TestCase.php | 2 ++ 13 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/From.php b/src/From.php index 07f7c94..8d75946 100644 --- a/src/From.php +++ b/src/From.php @@ -15,7 +15,7 @@ trait From * * @throws ValueError */ - public static function from(string $case): static + public static function from(int|string $case): static { return static::fromName($case); } @@ -25,7 +25,7 @@ public static function from(string $case): static * * This will not override the `tryFrom()` method on BackedEnums */ - public static function tryFrom(string $case): ?static + public static function tryFrom(int|string $case): ?static { return static::tryFromName($case); } diff --git a/tests/PHPStan/InvokableCases/InvokableCasesTestCase.php b/tests/PHPStan/InvokableCases/InvokableCasesTestCase.php index ae04874..1b45773 100644 --- a/tests/PHPStan/InvokableCases/InvokableCasesTestCase.php +++ b/tests/PHPStan/InvokableCases/InvokableCasesTestCase.php @@ -1,5 +1,7 @@ is(Status::PENDING))->toBeTrue(); expect(Status::PENDING->is(Status::DONE))->toBeFalse(); diff --git a/tests/Pest/FromTest.php b/tests/Pest/FromTest.php index af9605a..229e510 100644 --- a/tests/Pest/FromTest.php +++ b/tests/Pest/FromTest.php @@ -1,5 +1,7 @@ expect(Status::from(0)) ->toBe(Status::PENDING); diff --git a/tests/Pest/InvokableCasesTest.php b/tests/Pest/InvokableCasesTest.php index 6dd5b8a..bd50805 100644 --- a/tests/Pest/InvokableCasesTest.php +++ b/tests/Pest/InvokableCasesTest.php @@ -1,5 +1,7 @@ color())->toBe('indigo'); expect(Role::GUEST->color())->toBe('gray'); diff --git a/tests/Pest/NamesTest.php b/tests/Pest/NamesTest.php index ec39e3f..4c51e88 100644 --- a/tests/Pest/NamesTest.php +++ b/tests/Pest/NamesTest.php @@ -1,5 +1,7 @@ expect(Status::names()) ->toBe(['PENDING', 'DONE']); diff --git a/tests/Pest/OptionsTest.php b/tests/Pest/OptionsTest.php index dda6086..e1220ed 100644 --- a/tests/Pest/OptionsTest.php +++ b/tests/Pest/OptionsTest.php @@ -1,5 +1,7 @@ expect(Status::options())->toBe([ 'PENDING' => 0, diff --git a/tests/Pest/TraitTest.php b/tests/Pest/TraitTest.php index 43730aa..6bbcae5 100644 --- a/tests/Pest/TraitTest.php +++ b/tests/Pest/TraitTest.php @@ -1,5 +1,7 @@ expect(Status::values()) ->toBe([0, 1]); diff --git a/tests/TestCase.php b/tests/TestCase.php index bb466c9..3edb27c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,5 +1,7 @@