Add Countable and JsonSerializable interfaces to Collection#3
Conversation
Collection had a count() method but didn't formally implement the Countable interface, preventing use with PHP's native count(). Added JsonSerializable for seamless json_encode() support. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThis PR extends the Collection class to implement the PHP standard Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Countable— enables nativecount($collection)usageJsonSerializable— enables nativejson_encode($collection)usage#[\ReturnTypeWillChange]attribute tocount()for PHP 8.1+ compatibilityjsonSerialize()method to BaseTrait returning internal items arrayWhy
The Collection class had a
count()method but didn't formally implementCountable, meaning PHP's nativecount()function would not work correctly with Collection objects. Similarly,json_encode()on a Collection would produce{}instead of the actual items — a common pain point when using collections in REST APIs.Backward compatible
All changes are additive — no existing API is modified or removed.
Test plan
testCountable— verifiescount()andassertCount()worktestJsonSerializable— verifiesjson_encode()produces correct JSONtestJsonSerializeMethod— verifiesjsonSerialize()returns items array🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests