Skip to content

Add enum support in ArrayHelper::toArray()#20781

Open
WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
WarLikeLaux:add-enum-support-ArrayHelper-toArray
Open

Add enum support in ArrayHelper::toArray()#20781
WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
WarLikeLaux:add-enum-support-ArrayHelper-toArray

Conversation

@WarLikeLaux
Copy link
Contributor

@WarLikeLaux WarLikeLaux commented Mar 4, 2026

Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues #20065

What does this PR do?

Fixes enum handling in ArrayHelper::toArray(). BackedEnum resolves to ->value, UnitEnum resolves to ->name.

Before:

ArrayHelper::toArray([Status::Active, Status::Inactive]);
// [['name' => 'Active', 'value' => 1], ['name' => 'Inactive', 'value' => 0]]

ArrayHelper::toArray(Status::Active);
// ['name' => 'Active', 'value' => 1]

After:

ArrayHelper::toArray([Status::Active, Status::Inactive]);
// [1, 0]

ArrayHelper::toArray(Status::Active);
// [1]

Enums inside arrays resolve to scalar values directly. Single enum is wrapped in array to preserve the @return array contract - same as toArray('foo') returns ['foo'].

How other frameworks handle this

  • Symfony - BackedEnumNormalizer returns ->value (scalar). Source
  • Laravel - Eloquent casts enum attributes to ->value on toArray(). Source
  • Yii2 (current) - iterates enum as generic object, leaks internal structure: ['name' => 'Active', 'value' => 1]
  • PHP json_encode - BackedEnum serializes to its scalar value natively

All 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/value properties) instead of extracting the meaningful value.

Alternative to #20065, which only handled UnitEnum and had no tests. This PR also handles BackedEnum separately, is compatible with PHP 7.4/8.0 (tests skipped, phpstan clean on both configs), and includes 6 unit tests.

@WarLikeLaux WarLikeLaux marked this pull request as draft March 4, 2026 18:04
@codecov
Copy link

codecov bot commented Mar 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.57%. Comparing base (379b296) to head (743afc5).
✅ All tests successful. No failed tests found.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@WarLikeLaux WarLikeLaux marked this pull request as ready for review March 4, 2026 18:33
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.

1 participant