Add enum support in ArrayHelper::toArray()#20781
Open
WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
Open
Add enum support in ArrayHelper::toArray()#20781WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
ArrayHelper::toArray()#20781WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #20781 +/- ##
=========================================
Coverage 78.56% 78.57%
- Complexity 11568 11576 +8
=========================================
Files 376 376
Lines 30750 30758 +8
=========================================
+ Hits 24160 24168 +8
Misses 6590 6590 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
What does this PR do?
Fixes enum handling in
ArrayHelper::toArray().BackedEnumresolves to->value,UnitEnumresolves to->name.Before:
After:
Enums inside arrays resolve to scalar values directly. Single enum is wrapped in array to preserve the
@return arraycontract - same astoArray('foo')returns['foo'].How other frameworks handle this
BackedEnumNormalizerreturns->value(scalar). Source->valueontoArray(). Source['name' => 'Active', 'value' => 1]json_encode-BackedEnumserializes to its scalar value nativelyAll major frameworks resolve enums to scalar values. The current Yii2 behavior is a side effect of generic object iteration - enums were not handled explicitly, so
toArray()leaked their internal structure (name/valueproperties) instead of extracting the meaningful value.Alternative to #20065, which only handled
UnitEnumand had no tests. This PR also handlesBackedEnumseparately, is compatible with PHP 7.4/8.0 (tests skipped, phpstan clean on both configs), and includes 6 unit tests.