Skip to content

Fix BackedEnum support when bStrictObjectTypeChecking is enabled#253

Open
djairhogeuens wants to merge 1 commit intocweiske:masterfrom
djairhogeuens:fix/issue-247-backedenum-strict-mode
Open

Fix BackedEnum support when bStrictObjectTypeChecking is enabled#253
djairhogeuens wants to merge 1 commit intocweiske:masterfrom
djairhogeuens:fix/issue-247-backedenum-strict-mode

Conversation

@djairhogeuens
Copy link
Copy Markdown

Closes #247.

BackedEnum cases are represented in JSON as their scalar backing value (string or int). Strict object type checking previously rejected scalar to object conversion across the board, which made BackedEnum properties unusable when the (default) bStrictObjectTypeChecking flag was on.

Add a narrow exemption: when the target class is a \BackedEnum subclass, fall through to the existing createInstance($class, true, $jvalue) path, which already handles ::from($jvalue) correctly. Strict checking for non-enum classes is unchanged, and invalid enum values still raise \ValueError.

Adds tests/EnumsStrictTypes_PHP81_Test (9 tests) and a fixture for array-of-enum and nullable-enum cases.

Closes cweiske#247.

BackedEnum cases are represented in JSON as their scalar backing value
(string or int). Strict object type checking previously rejected scalar
to object conversion across the board, which made BackedEnum properties
unusable when the (default) bStrictObjectTypeChecking flag was on.

Add a narrow exemption: when the target class is a \BackedEnum subclass,
fall through to the existing createInstance($class, true, $jvalue) path,
which already handles \::from($jvalue) correctly. Strict checking
for non-enum classes is unchanged, and invalid enum values still raise
\ValueError.

Adds tests/EnumsStrictTypes_PHP81_Test (9 tests) and a fixture for
array-of-enum and nullable-enum cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strict object type checking doesn't allow backed enums.

1 participant