Skip to content

Add Countable and JsonSerializable interfaces to Collection#3

Closed
kai3769 wants to merge 1 commit into
hiqdev:masterfrom
kai3769:add-countable-jsonserializable
Closed

Add Countable and JsonSerializable interfaces to Collection#3
kai3769 wants to merge 1 commit into
hiqdev:masterfrom
kai3769:add-countable-jsonserializable

Conversation

@kai3769
Copy link
Copy Markdown

@kai3769 kai3769 commented Apr 10, 2026

Summary

  • Collection class now implements Countable — enables native count($collection) usage
  • Collection class now implements JsonSerializable — enables native json_encode($collection) usage
  • Added #[\ReturnTypeWillChange] attribute to count() for PHP 8.1+ compatibility
  • Added jsonSerialize() method to BaseTrait returning internal items array
  • Added tests for both interfaces

Why

The Collection class had a count() method but didn't formally implement Countable, meaning PHP's native count() 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 — verifies count() and assertCount() work
  • testJsonSerializable — verifies json_encode() produces correct JSON
  • testJsonSerializeMethod — verifies jsonSerialize() returns items array

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Collections now support the standard count() function to retrieve collection size.
    • Collections can now be serialized to JSON using json_encode(), enabling seamless API integration and data export.
  • Tests

    • Added test coverage for count() functionality.
    • Added test coverage for JSON serialization.
    • Added validation tests for the serialization output.

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>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 25051dff-dfb7-4e53-8e04-025cf2c6d694

📥 Commits

Reviewing files that changed from the base of the PR and between 0d60d43 and 0aa5598.

📒 Files selected for processing (3)
  • src/BaseTrait.php
  • src/Collection.php
  • tests/unit/CollectionTestTrait.php

📝 Walkthrough

Walkthrough

This PR extends the Collection class to implement the PHP standard Countable and JsonSerializable interfaces. The count() method receives a return-type attribute, and a new jsonSerialize() method is added. Corresponding test methods validate the new functionality.

Changes

Cohort / File(s) Summary
Interface Implementation
src/BaseTrait.php, src/Collection.php
Added #[\ReturnTypeWillChange] attribute to count() method and new public jsonSerialize() method. Updated Collection class to implement Countable and JsonSerializable interfaces with appropriate use statements.
Test Coverage
tests/unit/CollectionTestTrait.php
Added three test methods: testCountable() validates count() functionality, testJsonSerializable() verifies JSON encoding/decoding round-trip, and testJsonSerializeMethod() confirms jsonSerialize() output matches collection items.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐰 A collection now counts with pride,
And JSON flows with ease inside,
With standard interfaces aligned,
Our rabbit's work leaves bugs behind! 🎉

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: adding Countable and JsonSerializable interfaces to the Collection class.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@kai3769 kai3769 closed this Apr 20, 2026
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.

2 participants