agentleFS
AnyOrgNameAnyOrgName/php-skills/skills/php/enums/SKILL.md

enums

Skill4 starsChanged 4 months agoMITView on GitHub

Model fixed sets of named values with PHP 8.1+ enumerations — backed and pure enums, safe input validation, methods, and interfaces. Use when the user needs a fixed, closed set of named values such as order status, role, or state; the user wants to replace magic strings or class constants with a type-safe enum on PHP 8.1+; the user maps untrusted HTTP or API input to a known enum set and must reject invalid values. Keywords: enums, php81, type-safety, backed-enum, validation.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Do not use this skill when the value set is open-ended or third parties must add values at runtime.

    line 66
  2. Distinctive:

    Do not use enums when the target runtime is PHP 7.4 or 8.0.

    line 67
  3. Very specific:

    Add a label(): string method using match ($this) to map each case to display text.

    line 80
  4. Very specific:

    Use the generated Enum::cases() method to iterate all values (for dropdowns, validation lists, or seeding) instead of maintaining a …

    line 85
  5. Very specific:

    Put value-specific behavior (labels, colors, transitions) in methods on the enum itself, typically with a match ($this) expression, rather …

    line 89

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- generated: do not edit — source: knowledge/atoms/php/enums/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->

PHP Enums

When to use

  • The user needs a fixed, closed set of named values such as order status, role, or state.
  • The user wants to replace magic strings or class constants with a type-safe enum on PHP 8.1+.
  • The user maps untrusted HTTP or API input to a known enum set and must reject invalid values.

When NOT to use

  • Do not use this skill when the value set is open-ended or third parties must add values at runtime.
  • Do not use enums when the target runtime is PHP 7.4 or 8.0; use class constants with allow-list validation instead.

Core guidance

  • Choose backed enums when values are persisted or transmitted — Use a backed enum (enum Status: string) when values are stored in a database, serialized to JSON, or received from input. Use a pure enum when the values are purely in-memory tokens with no external representation.
  • Use tryFrom for untrusted input, from for trusted values — Call tryFrom() on untrusted input and handle the null result; call from() only when the value originates from a trusted, already-validated source.

Quick recipe

  1. Declare enum OrderStatus: string and list each case with its stored scalar value. (verify: OrderStatus::Paid->value === 'paid')
  2. Add a label(): string method using match ($this) to map each case to display text. (verify: OrderStatus::Paid->label() returns the expected string)
  3. At the controller boundary, resolve input with OrderStatus::tryFrom($input) and return 422 on null. (verify: Invalid input yields a 422 response, not a 500)

Best practices

  • Use the generated Enum::cases() method to iterate all values (for dropdowns, validation lists, or seeding) instead of maintaining a separate array
  • Have enums implement an interface when several enums must be used interchangeably (e.g
  • Put value-specific behavior (labels, colors, transitions) in methods on the enum itself, typically with a match ($this) expression, rather than in external arrays

Anti-patterns

Anti-patternWhy it failsDo instead
Class constants where an enum fitsClass constants give no type safety — any string is accepted, and IDEs cannot enforce the set.Use a backed enum so the type system rejects invalid values at the boundary.
Passing scalar around instead of the enumDowngrading to a scalar discards type safety, re-introducing the stringly-typed bugs enums remove.Keep the enum type across the domain; convert to ->value only at the persistence or serialization edge.
Comparing enum to raw stringAn enum instance is never identical to a scalar, so the comparison is always false and the bug is silent.Compare enum-to-enum ($status === Status::Paid) or compare $status->value === 'paid' when a scalar is unavoidable.

See references/anti-patterns.md.

Common mistakes

  1. from() throws ValueError on unknown input — Calling Enum::from($userInput) on an unknown value throws an uncaught ValueError. Fix: Use tryFrom() and handle null, or wrap from() in a try/catch at trust boundaries.
  2. Adding a case breaks non-exhaustive match — A match ($enum) without a default throws UnhandledMatchError when a new case is added. Fix: Rely on static analysis (PHPStan level 8) to flag non-exhaustive matches at build time rather than adding a catch-all default that hides new cases.
  3. Pure enums do not serialize to a scalar — A pure (non-backed) enum has no ->value, so json_encode() emits an object, not a scalar. Fix: Use a backed enum when the value crosses a serialization boundary (JSON, DB, cache).

Version notes

  • Enums introduced in PHP 8.1 (PHP >=8.1) — Enumerations (pure and backed), cases(), from(), and tryFrom() are available from PHP 8.1.
  • Typed class constants complement enums in PHP 8.3 (PHP >=8.3) — From PHP 8.3, constants declared on an enum can be typed, tightening contracts shared via interfaces.
  • PHP 8.5 enum refinements (PHP >=8.5) — PHP 8.5 refines enum constant handling; no code changes are required when migrating enum-heavy code from 8.4 to 8.5.

Security

  • Validate enum input at the trust boundary — Treat any enum value sourced from HTTP, CLI, or third-party payloads as untrusted: resolve it with tryFrom() and reject null before it enters domain logic.

Testing

  • Activation and decision behavior are verified in eval/scenarios/php/enums.yaml.

Additional resources

  • references/anti-patterns.md
  • references/core.md
  • references/examples.md
  • references/pitfalls.md
  • references/recipes.md
  • references/version-differences.md

Knowledge graph

Prerequisites: php/typed-properties
Related: php/attributes, php/class-constants, php/match-expression, php/value-objects

<!-- graph-fragment: graph/fragments/php/enums.yaml -->

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.