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' diff --git a/composer.json b/composer.json index bac7027..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" @@ -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/Concerns/SelfAware.php b/src/Concerns/SelfAware.php index db42c81..2527f22 100644 --- a/src/Concerns/SelfAware.php +++ b/src/Concerns/SelfAware.php @@ -50,9 +50,28 @@ public static function isBackedByString(): bool /** * Retrieve all the meta names of the enum. * - * @return string[] + * @return list */ 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 list + */ + 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/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 */ } /** 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()