From cd972098e0e5521296037a4a9873e25d10243d78 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sat, 16 Nov 2024 18:07:58 +0100 Subject: [PATCH 1/5] Upgrade PHPStan --- composer.json | 2 +- phpstan.neon | 8 ++++++++ src/CasesCollection.php | 4 +++- src/Enums.php | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index bac7027..af563d0 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ }, "require-dev": { "pestphp/pest": "^2.0", - "phpstan/phpstan": "^1.9", + "phpstan/phpstan": "^2.0", "scrutinizer/ocular": "^1.9", "squizlabs/php_codesniffer": "^3.0", "tightenco/duster": "^2.0" diff --git a/phpstan.neon b/phpstan.neon index 5209c3e..1c54e9c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,5 +2,13 @@ parameters: level: max paths: - src + ignoreErrors: + - + messages: + - '#Cannot call method [a-zA-Z0-9\\_]+\(\) on TValue of mixed.#' + - '#Cannot access property \$(?:name|value) on TValue of mixed#' + path: src/CasesCollection.php + - + identifier: trait.unused includes: - phpstan-baseline.neon diff --git a/src/CasesCollection.php b/src/CasesCollection.php index 60fff23..6d8e17c 100644 --- a/src/CasesCollection.php +++ b/src/CasesCollection.php @@ -131,10 +131,11 @@ public function first(callable $callback = null): mixed /** * Retrieve all the names of the cases. * - * @return string[] + * @return list */ public function names(): array { + /** @var list */ return array_column($this->cases, 'name'); } @@ -145,6 +146,7 @@ public function names(): array */ public function values(): array { + /** @var list */ return array_column($this->cases, 'value'); } diff --git a/src/Enums.php b/src/Enums.php index 3c0d781..f2da1e4 100644 --- a/src/Enums.php +++ b/src/Enums.php @@ -72,7 +72,7 @@ public static function handleStaticCall(string $enum, string $name, array $argum { return static::$onStaticCall ? (static::$onStaticCall)($enum, $name, $arguments) - : $enum::fromName($name)->value(); + : $enum::fromName($name)->value(); /** @phpstan-ignore method.nonObject */ } /** From 2d9c6bc2e85ddc364094ce7b1ac0750fa5caebd3 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sat, 16 Nov 2024 18:08:34 +0100 Subject: [PATCH 2/5] Add method to list meta attribute names --- src/Concerns/SelfAware.php | 25 +++++++++++++++++++------ tests/BackedEnumTest.php | 4 ++++ tests/PureEnumTest.php | 4 ++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Concerns/SelfAware.php b/src/Concerns/SelfAware.php index db42c81..8336854 100644 --- a/src/Concerns/SelfAware.php +++ b/src/Concerns/SelfAware.php @@ -53,6 +53,25 @@ public static function isBackedByString(): bool * @return string[] */ public static function metaNames(): array + { + $meta = self::metaAttributeNames(); + $enum = new ReflectionEnum(self::class); + + foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { + if (! $method->isStatic() && $method->getFileName() == $enum->getFileName()) { + $meta[] = $method->getShortName(); + } + } + + return array_values(array_unique($meta)); + } + + /** + * Retrieve all the meta attribute names of the enum. + * + * @return string[] + */ + public static function metaAttributeNames(): array { $meta = []; $enum = new ReflectionEnum(self::class); @@ -67,12 +86,6 @@ public static function metaNames(): array } } - foreach ($enum->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { - if (! $method->isStatic() && $method->getFileName() == $enum->getFileName()) { - $meta[] = $method->getShortName(); - } - } - return array_values(array_unique($meta)); } diff --git a/tests/BackedEnumTest.php b/tests/BackedEnumTest.php index 0d2545f..e880fe8 100644 --- a/tests/BackedEnumTest.php +++ b/tests/BackedEnumTest.php @@ -431,6 +431,10 @@ expect(BackedEnum::metaNames())->toBe(['color', 'shape', 'isOdd']); }); +it('retrieves the meta attribute names of an enum', function() { + expect(BackedEnum::metaAttributeNames())->toBe(['color', 'shape']); +}); + it('retrieves a case item') ->expect(fn(string $item, mixed $value) => BackedEnum::one->resolveItem($item) === $value) ->toBeTrue() diff --git a/tests/PureEnumTest.php b/tests/PureEnumTest.php index aaa2d12..f6b9d9c 100644 --- a/tests/PureEnumTest.php +++ b/tests/PureEnumTest.php @@ -440,6 +440,10 @@ expect(PureEnum::metaNames())->toBe(['color', 'shape', 'isOdd']); }); +it('retrieves the meta attribute names of an enum', function() { + expect(PureEnum::metaAttributeNames())->toBe(['color', 'shape']); +}); + it('retrieves the item of a case') ->expect(fn(string $item, mixed $value) => PureEnum::one->resolveItem($item) === $value) ->toBeTrue() From 0e6a3fcf2981f5ea1ea7e21c8e763d08814b1211 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sat, 16 Nov 2024 18:08:45 +0100 Subject: [PATCH 3/5] Update readme --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6bdbd6a..013a777 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,12 @@ [![PER][ico-per]][link-per] [![Total Downloads][ico-downloads]][link-downloads] -Zero-dependencies PHP library to supercharge enum functionalities. +Zero-dependencies package to supercharge enum functionalities. + +> [!TIP] +> Need to supercharge enums in a Laravel application? +> +> Consider using [🎲 Laravel Enum](https://github.com/cerbero90/laravel-enum) instead. ## 📦 Install @@ -391,7 +396,10 @@ Finally, the following methods can be useful for inspecting enums or auto-genera ```php PureEnum::isPure(); // true PureEnum::isBacked(); // false +PureEnum::isBackedByInteger(); // false +PureEnum::isBackedByString(); // false PureEnum::metaNames(); // ['color', 'shape', 'isOdd'] +PureEnum::metaAttributeNames(); // ['color', 'shape'] PureEnum::One->resolveItem('name'); // 'One' PureEnum::One->resolveMeta('isOdd'); // true PureEnum::One->resolveMetaAttribute('color'); // 'red' @@ -399,7 +407,10 @@ PureEnum::One->value(); // 'One' BackedEnum::isPure(); // false BackedEnum::isBacked(); // true +BackedEnum::isBackedByInteger(); // true +BackedEnum::isBackedByString(); // false BackedEnum::metaNames(); // ['color', 'shape', 'isOdd'] +BackedEnum::metaAttributeNames(); // ['color', 'shape'] BackedEnum::One->resolveItem('value'); // 1 BackedEnum::One->resolveMeta('isOdd'); // true BackedEnum::One->resolveMetaAttribute('color'); // 'red' From 195ba69b893c4e647e4e4cc00eb124151135150d Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sat, 16 Nov 2024 18:20:09 +0100 Subject: [PATCH 4/5] Update description --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index af563d0..301aa30 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "cerbero/enum", "type": "library", - "description": "PHP library to extend enum functionalities.", + "description": "Zero-dependencies package to supercharge enum functionalities.", "keywords": [ "enum", "enumeration" From 60f1eef3301ecb641205abbd5e7e4805cc5f83af Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sat, 16 Nov 2024 19:48:32 +0100 Subject: [PATCH 5/5] Update docblock --- src/Concerns/SelfAware.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Concerns/SelfAware.php b/src/Concerns/SelfAware.php index 8336854..2527f22 100644 --- a/src/Concerns/SelfAware.php +++ b/src/Concerns/SelfAware.php @@ -50,7 +50,7 @@ public static function isBackedByString(): bool /** * Retrieve all the meta names of the enum. * - * @return string[] + * @return list */ public static function metaNames(): array { @@ -69,7 +69,7 @@ public static function metaNames(): array /** * Retrieve all the meta attribute names of the enum. * - * @return string[] + * @return list */ public static function metaAttributeNames(): array {