PHPackages                             prinsfrank/enums - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. prinsfrank/enums

ActiveLibrary

prinsfrank/enums
================

Adds missing strictly typed methods to work with enums

v1.4.1(1y ago)2431.1k—9.7%13MITPHPPHP ^8.1 || ^8.2 || ^8.3 || ^8.4CI passing

Since Dec 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/PrinsFrank/enums)[ Packagist](https://packagist.org/packages/prinsfrank/enums)[ GitHub Sponsors](https://github.com/PrinsFrank)[ RSS](/packages/prinsfrank-enums/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (4)Versions (7)Used By (3)

  ![Banner](https://github.com/PrinsFrank/enums/raw/main/docs/images/banner_light.png)Enums
=====

[](#enums)

[![GitHub](https://camo.githubusercontent.com/571bb300953b334a9b0bf5496786cd5a06ca1ec56576dbc784d9b6dbc7d27cd1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7072696e736672616e6b2f656e756d73)](https://camo.githubusercontent.com/571bb300953b334a9b0bf5496786cd5a06ca1ec56576dbc784d9b6dbc7d27cd1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7072696e736672616e6b2f656e756d73)[![PHP Version Support](https://camo.githubusercontent.com/ac05a518e8b994ebbe30d301d15d0d0176913ab3614a3a209066259d093fce31/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7072696e736672616e6b2f656e756d73)](https://camo.githubusercontent.com/ac05a518e8b994ebbe30d301d15d0d0176913ab3614a3a209066259d093fce31/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7072696e736672616e6b2f656e756d73)[![codecov](https://camo.githubusercontent.com/d90d3dc09449102dccf664419ba32a0f19afdf1dadbc96b8fb6d0c6fdfe3d653/68747470733a2f2f636f6465636f762e696f2f67682f5072696e734672616e6b2f656e756d732f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d394f3356423536334d55)](https://codecov.io/gh/PrinsFrank/enums)[![PHPStan Level](https://camo.githubusercontent.com/83dd3d35cebed0eab9ee97ff1a5849c1344cda6a8ee9cac2cda20f5aa55b67bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/83dd3d35cebed0eab9ee97ff1a5849c1344cda6a8ee9cac2cda20f5aa55b67bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c6174)

**Adds missing strictly typed methods to work with enums**

Setup
-----

[](#setup)

> **Note**Make sure you are running PHP 8.1 or higher to use this package

To start right away, run the following command in your composer project;

`composer require prinsfrank/enums`

Or for development only;

`composer require prinsfrank/enums --dev`

How this package works
----------------------

[](#how-this-package-works)

### UnitEnum

[](#unitenum)

Let's assume we have the following UnitEnum:

```
enum Example {
    case Foo;

    #[HasSpecialMeaning]
    case Bar;
}
```

If we want to get a value by it's name from a string, we can call `fromName` or `tryFromName`;

```
UnitEnum::fromName(Example::class, 'Foo'); // Example::Foo
UnitEnum::tryFromName(Example::class, 'Foo'); // Example::Foo
```

The difference between the two methods is the way non-existing names are handled;

```
UnitEnum::fromName(Example::class, 'Example'); // @throws NameNotFoundException
UnitEnum::tryFromName(Example::class, 'Example'); // null
```

Another method that this package provides is to get an array of all the names for an enum;

```
UnitEnum::names(Example::class); // ['Foo', 'Bar']
```

And finally some methods to easily get the attributes of an enum case:

```
UnitEnum::hasCaseAttributes(Example::Bar, HasSpecialMeaning); // true
UnitEnum::getCaseAttributes(Example::Bar); // [new HasSpecialMeaning()]
```

### BackedEnum

[](#backedenum)

Let's assume we have the following BackedEnum: (It doesn't matter if an enum is backed by a string or an integer)

```
enum Example: string {
    case Foo = 'Foo';

    #[HasSpecialMeaning]
    case Bar = 'Bar';
}
```

If we want to get a value by it's name from a string, we can call `fromName` or `tryFromName`;

```
BackedEnum::fromName(Example::class, 'Foo'); // Example::Foo
BackedEnum::tryFromName(Example::class, 'Foo'); // Example::Foo
```

The difference between the two methods is the way non-existing names are handled;

```
BackedEnum::fromName(Example::class, 'Example'); // @throws NameNotFoundException
BackedEnum::tryFromName(Example::class, 'Example'); // null
```

Another method that this package provides is to get an array of all the names for an enum;

```
BackedEnum::names(Example::class); // ['Foo', 'Bar']
```

And finally some methods to easily get the attributes of an enum case:

```
BackedEnum::hasCaseAttributes(Example::Bar, HasSpecialMeaning); // true
BackedEnum::getCaseAttributes(Example::Bar); // [new HasSpecialMeaning()]
```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance40

Moderate activity, may be stable

Popularity40

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 81.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~73 days

Recently: every ~91 days

Total

6

Last Release

506d ago

PHP version history (2 changes)v1.0PHP ^8.1 || ^8.2 || ^8.3

v1.3PHP ^8.1 || ^8.2 || ^8.3 || ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/288919c24dc651727390578a2bfe6ef020f6f508c30db717b54c943a9e2ac0b3?d=identicon)[PrinsFrank](/maintainers/PrinsFrank)

---

Top Contributors

[![PrinsFrank](https://avatars.githubusercontent.com/u/25006490?v=4)](https://github.com/PrinsFrank "PrinsFrank (26 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (6 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/prinsfrank-enums/health.svg)

```
[![Health](https://phpackages.com/badges/prinsfrank-enums/health.svg)](https://phpackages.com/packages/prinsfrank-enums)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
