Infer non-falsy-string for the magic name property on the UnitEnum/BackedEnum interfaces#5894
Open
phpstan-bot wants to merge 7 commits into
Open
Infer non-falsy-string for the magic name property on the UnitEnum/BackedEnum interfaces#5894phpstan-bot wants to merge 7 commits into
non-falsy-string for the magic name property on the UnitEnum/BackedEnum interfaces#5894phpstan-bot wants to merge 7 commits into
Conversation
…um`/`BackedEnum` interfaces - In `PhpClassReflectionExtension::createProperty()`, set the PHPDoc type of the magic `name` property to `non-falsy-string` when it is declared on the `UnitEnum` interface (also inherited by `BackedEnum` and any `T of UnitEnum` template bound). Previously BetterReflection synthesized it as plain `string`. - Enum case names are always valid PHP labels, so they can never be empty or "0". - Concrete enum types (`Foo $foo`) and individual enum case objects (`Foo::A->name`) were already correct — they resolve to a union of constant strings / a single constant string, which are non-falsy. The backed-enum `value` property is intentionally left as-is, since it may legitimately be an empty string or "0".
staabm
reviewed
Jun 18, 2026
Move the UnitEnum/BackedEnum interface `name` narrowing next to the existing concrete-enum `name`/`value` narrowing, so all the synthesized enum-property logic lives in one block instead of being split across the method. The interface branch produces non-falsy-string; `value` is deliberately left as its native int|string (a backing value may be "" or "0"), which the test now documents and guards with `Bar::value`. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Accessing the magic
nameproperty on a value typed as the\UnitEnumor\BackedEnuminterface (or a@template T of \UnitEnumbound) inferred plainstring. Since an enum case name is always a valid PHP label, it can never be an empty string or"0", so it should be inferred asnon-falsy-string.Changes
src/Reflection/Php/PhpClassReflectionExtension.php: increateProperty(), when the resolved property is the magicnamedeclared on theUnitEnuminterface and no PHPDoc type was found, set its PHPDoc type tonon-falsy-string(StringTypeintersected withAccessoryNonFalsyStringType).tests/PHPStan/Analyser/nsrt/bug-14839.php.Root cause
BetterReflection synthesizes the magic enum members (
ReflectionClass::getEnumProperties()): thenameproperty on theUnitEnuminterface is created with the native typestring. PHPStan already special-cases concrete enum classes (producing a union of constant-string case names) and individual enum-case objects (EnumCaseObjectType, producing a singleConstantStringType) — both of which are non-falsy. But values typed only by theUnitEnum/BackedEnuminterface (or a template bounded by them) never hit those special cases and fell back to the synthesizedstring.The fix narrows the
nameproperty tonon-falsy-stringat the single place where the interface-declared property is reflected, which also coversBackedEnum(inheritsnamefromUnitEnum) and@template T of \UnitEnumparameters.Test
tests/PHPStan/Analyser/nsrt/bug-14839.phpasserts:Foo $foo/Bar $bar(concrete enums) →'A'|'B'(already correct, kept as a guard).\UnitEnum $u→non-falsy-string(wasstring).\BackedEnum $b→non-falsy-stringfor->name(wasstring) andint|stringfor->value(unchanged).@template T of \UnitEnumparameter →non-falsy-string(wasstring).Verified the test fails before the fix (the
UnitEnum/BackedEnum/template assertions reportedstring) and passes after.Analogous cases probed
EnumCaseObjectType) — already non-falsy, no change needed.valueproperty — intentionally left asint|string, since a backing value may legitimately be an empty string or"0".Fixes phpstan/phpstan#14839