Add typed enum configuration getters

Merged
BI
1d ago
+312-50

Enum configuration values

Apps and packages can read a setting with config()->enum('passport.key_store', KeyStore::class). The getter returns a case of the requested enum. A backed enum also accepts an exact string or integer backing value, so the configuration can still read text from an environment variable. An invalid value raises an exception naming the config key.

Use nullableEnum() for settings that allow null. As with the other nullable getters, an absent key needs an explicit default. PHPStan checks known settings and infers the enum type or individual case when it can.

Framework changes

  • Passport reads its signing key store through the getter. Its environment setting keeps the same values.
  • Data ships ValidationStrategy::OnlyRequests directly in its config. Existing string overrides still work.
  • config:show displays the enum class and case. JSON output uses a backed enum's value or a pure unit enum's case name.
  • Custom implementations of the configuration repository contract need both methods. Extending the framework repository supplies them.

Summary by cubic

Adds enum() and nullableEnum() typed getters to the config repository so settings can be read as concrete enum cases instead of parsed strings.

  • Both getters accept a case or an exact backing value; invalid values throw an InvalidArgumentException naming the config key.
  • The Config contract now requires both methods; extending the framework repository supplies them.
  • config:show renders enum values as Class::Case in text, backing values in JSON, and case names for unit enums.
  • PHPStan infers enum return types, checks known values, and recommends enum() for enum-valued config; it also preserves concrete enum types for unpacked argument arrays.
  • Passport and Data read settings through the new getters; existing string overrides like PASSPORT_KEY_STORE and published Data config keep working.
  • The shipped data.validation_strategy default is now a ValidationStrategy case instead of its string backing value; invalid strategies raise InvalidArgumentException instead of ValueError.

[!NOTE]

Add enum() and nullableEnum() typed config getters

  • Adds enum getters to the Repository implementation, contract, Config facade, and docs. The resolver accepts an enum instance or an exact backing value; other values throw InvalidArgumentException.
  • Migrates existing config reads to the new getters: validation_strategy in DataConfig.php now defaults to the ValidationStrategy case itself, and Passport commands/provider reads replace KeyStore::from() calls.
  • PHPStan rules recognize the new getters, infer enum return types from the class argument, and include the required enum-class argument in diagnostics (see ConfigHelper.php).
  • config:show now renders enums as Class::Case and converts non-JsonSerializable unit enums to case names in --json output (see ConfigShowCommand.php).
  • Risk: implementations of the Repository contract must add the two new methods; invalid enum config values now throw InvalidArgumentException instead of a ValueError from ::from() — in-tree readers in DataConfig and Passport are updated.

Macroscope summarized 8a41311.

Summary by CodeRabbit

  • New Features
    • Added typed configuration accessors for enum values, including nullable enum settings and support for defaults.
    • Configuration displays enums as Class::Case; JSON output uses the backing value for backed enums and the case name for unit enums.
    • Static analysis now recognizes enum configuration getters and can recommend them for enum settings.
  • Documentation
    • Updated configuration guidance and examples to cover enum values and retrieval.