Skip to content

Commit

Permalink
Merge branch 'release/2.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Nov 19, 2024
2 parents 2a05cbc + 0054491 commit 3d19256
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
- Nothing


## 2.2.0 - 2024-11-19

### Added
- Method `SelfAware::metaAttributeNames()` to list the names of all meta attributes

### Changed
- Upgraded PHPStan to v2


## 2.1.0 - 2024-10-30

### Added
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -391,15 +396,21 @@ 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'
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'
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/CasesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ public function first(callable $callback = null): mixed
/**
* Retrieve all the names of the cases.
*
* @return string[]
* @return list<string>
*/
public function names(): array
{
/** @var list<string> */
return array_column($this->cases, 'name');
}

Expand All @@ -145,6 +146,7 @@ public function names(): array
*/
public function values(): array
{
/** @var list<string|int> */
return array_column($this->cases, 'value');
}

Expand Down
27 changes: 20 additions & 7 deletions src/Concerns/SelfAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,28 @@ public static function isBackedByString(): bool
/**
* Retrieve all the meta names of the enum.
*
* @return string[]
* @return list<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 list<string>
*/
public static function metaAttributeNames(): array
{
$meta = [];
$enum = new ReflectionEnum(self::class);
Expand All @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/BackedEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions tests/PureEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3d19256

Please sign in to comment.