Skip to content

Replace json_encode with Json::encode - #621

Closed
foremtehan wants to merge 1 commit into
hypervel:0.4from
foremtehan:patch-6
Closed

foremtehan wants to merge 1 commit into
hypervel:0.4from
foremtehan:patch-6

Conversation

@foremtehan

@foremtehan foremtehan commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

This is kind of poc, Is is make sense to use Json cast whenever json_encode used in codebase?

Review in cubic

@coderabbitai

coderabbitai Bot commented Sep 27, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository: hypervel/components/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: bf52acf6-002a-4adc-80be-46625c1c8f71

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Route collection serialization through Eloquent's JSON encoder

✨ Enhancement 🕐 Less than 10 minutes

Grey Divider

AI Description

• Routes collection serialization through Eloquent's configurable JSON cast encoder.
• Preserves caller options and forced JSON exceptions during encoding.
Diagram

graph TD
  C["Collection toJson"] --> J["Eloquent JSON cast"] --> N["Native JSON encoder"]
  J --> U["Custom cast encoder"]
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Retain native json_encode
  • ➕ Preserves existing collection behavior and return guarantees
  • ➕ Keeps the collections package independent of database
  • ➕ Avoids a circular dependency because database already requires collections
  • ➖ Collection serialization cannot use Eloquent's configurable encoder
2. Introduce a lower-level JSON abstraction
  • ➕ Allows shared configurable encoding without coupling collections to Eloquent
  • ➕ Provides one framework-wide location for JSON behavior
  • ➖ Requires broader package and API design changes
  • ➖ May be excessive without a cross-package customization requirement

Recommendation: Retain direct json_encode unless collection-wide encoder customization is an explicit requirement. The proposed Eloquent dependency is not declared by hypervel/collections and creates a circular package relationship because hypervel/database already depends on collections; if shared customization is required, place the abstraction in a lower-level package.

Files changed (1) +2 / -1

Refactor (1) +2 / -1
EnumeratesValues.phpDelegate collection JSON encoding to Eloquent's cast utility +2/-1

Delegate collection JSON encoding to Eloquent's cast utility

• Imports the Eloquent JSON cast helper and uses it from Collection::toJson() instead of calling json_encode directly. Existing options remain combined with JSON_THROW_ON_ERROR, while configured Eloquent encoders can now affect collection serialization.

src/collections/src/Traits/EnumeratesValues.php

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Action required

1. Standalone collections cannot serialize 🐞 Bug ≡ Correctness
Description
EnumeratesValues::toJson() now calls Hypervel\Database\Eloquent\Casts\Json, although the
collections package neither requires nor autoloads the database package. Calling toJson() in a
standalone hypervel/collections installation therefore reaches an unavailable class and terminates
with a class-not-found error.
Code

src/collections/src/Traits/EnumeratesValues.php[969]

+        return Json::encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);
Evidence
The collections manifest has no database dependency and only maps its own Hypervel\Support
namespace, while the referenced class is supplied through the database package's separate autoload
mapping. The database package already requires collections, so adding the opposite dependency would
also create a dependency cycle.

src/collections/composer.json[25-45]
src/database/composer.json[24-27]
src/database/composer.json[70-74]
src/database/src/Eloquent/Casts/Json.php[5-7]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Collection JSON serialization now depends on an Eloquent class from `hypervel/database`, which is not a dependency of the standalone collections package and itself depends on collections.

## Fix Focus Areas
- src/collections/src/Traits/EnumeratesValues.php[17-17]
- src/collections/src/Traits/EnumeratesValues.php[967-969]

## Recommended Fix
Remove the database `Json` import and restore direct `json_encode` with `JSON_THROW_ON_ERROR`, preserving collection serialization without introducing a circular package dependency.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Model encoders corrupt collections 🐞 Bug ≡ Correctness
Description
EnumeratesValues::toJson() now delegates to the Eloquent cast encoder, whose process-wide callback
can return any mixed value instead of native JSON. When an application configures that supported
callback for model attributes, every collection starts returning the callback's arbitrary output or
throws a return-type error if it returns false.
Code

src/collections/src/Traits/EnumeratesValues.php[969]

+        return Json::encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);
Evidence
The Eloquent encoder invokes a static custom callback whenever one is registered, and the callback
persists for the worker lifetime. Repository tests demonstrate supported callbacks returning both
the non-JSON string encoded and false, while the collection interface requires toJson() to
return a JSON string.

src/database/src/Eloquent/Casts/Json.php[17-32]
src/database/src/Eloquent/Casts/Json.php[40-48]
tests/Database/DatabaseEloquentModelTest.php[4142-4151]
tests/Database/DatabaseEloquentJsonCastTest.php[72-88]
src/collections/src/Enumerable.php[1137-1145]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Collection serialization now shares Eloquent's configurable global encoder, allowing model-specific encoding configuration to alter or break every collection's `toJson()` result.

## Fix Focus Areas
- src/collections/src/Traits/EnumeratesValues.php[17-17]
- src/collections/src/Traits/EnumeratesValues.php[967-969]

## Recommended Fix
Remove the Eloquent cast encoder from collection serialization and use native `json_encode` with the supplied options plus `JSON_THROW_ON_ERROR`.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context sources
Review mode: ⚖️ Balanced: This is a runtime serialization behavior change in a shared collection path, so it warrants a careful single-pass review despite the small diff.

Grey Divider

Tip of the day
💡 Did you know, you can describe a rule in plain language on the Rules page and Qodo drafts it for you

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

public function toJson(int $options = 0): string
{
return json_encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);
return Json::encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. Standalone collections cannot serialize 🐞 Bug ≡ Correctness

EnumeratesValues::toJson() now calls Hypervel\Database\Eloquent\Casts\Json, although the
collections package neither requires nor autoloads the database package. Calling toJson() in a
standalone hypervel/collections installation therefore reaches an unavailable class and terminates
with a class-not-found error.
Agent Prompt
## Issue description
Collection JSON serialization now depends on an Eloquent class from `hypervel/database`, which is not a dependency of the standalone collections package and itself depends on collections.

## Fix Focus Areas
- src/collections/src/Traits/EnumeratesValues.php[17-17]
- src/collections/src/Traits/EnumeratesValues.php[967-969]

## Recommended Fix
Remove the database `Json` import and restore direct `json_encode` with `JSON_THROW_ON_ERROR`, preserving collection serialization without introducing a circular package dependency.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

public function toJson(int $options = 0): string
{
return json_encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);
return Json::encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

2. Model encoders corrupt collections 🐞 Bug ≡ Correctness

EnumeratesValues::toJson() now delegates to the Eloquent cast encoder, whose process-wide callback
can return any mixed value instead of native JSON. When an application configures that supported
callback for model attributes, every collection starts returning the callback's arbitrary output or
throws a return-type error if it returns false.
Agent Prompt
## Issue description
Collection serialization now shares Eloquent's configurable global encoder, allowing model-specific encoding configuration to alter or break every collection's `toJson()` result.

## Fix Focus Areas
- src/collections/src/Traits/EnumeratesValues.php[17-17]
- src/collections/src/Traits/EnumeratesValues.php[967-969]

## Recommended Fix
Remove the Eloquent cast encoder from collection serialization and use native `json_encode` with the supplied options plus `JSON_THROW_ON_ERROR`.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

@greptile-apps

greptile-apps Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 3/5

[Medium risk] Replaces built-in JSON encoding with a custom wrapper.

The PR is not safe to merge while collection serialization can fail without database or with a valid custom cast-encoder failure result.

Fix All in Claude CodeFindings

  1. P1 Collections now require database ▶
  2. P1 Cast encoder can break collections ▶

Summary

The PR replaces native JSON encoding in collection toJson() with the Eloquent JSON cast helper.

  • This introduces a database-package dependency into collections.
  • It also makes collection output subject to the configurable Eloquent cast encoder.

Reviews (1) · Last reviewed commit: "Replace json_encode with Json::encode"

use Hypervel\Support\Collection;
use Hypervel\Support\Enumerable;
use Hypervel\Support\HigherOrderCollectionProxy;
use Hypervel\Database\Eloquent\Casts\Json;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Collections now require database

If hypervel/collections is installed without hypervel/database, the new Json call cannot load its class: collections does not require the database package, and the database package owns that class. Calling toJson(), toPrettyJson(), or casting a collection to a string now fails with a class-not-found error instead of producing JSON.

Fix in Claude Code Fix in Codex

public function toJson(int $options = 0): string
{
return json_encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);
return Json::encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Cast encoder can break collections

If an application configures Eloquent’s JSON cast encoder and it returns false to signal an encoding failure, this call passes that result into toJson(): string. Collection serialization then throws a TypeError instead of retaining its previous JSON-string or JsonException behavior. The configured encoder also applies to collections even though it was set for Eloquent casts.

Fix in Claude Code Fix in Codex

@foremtehan foremtehan closed this Sep 27, 2026
@binaryfire

Copy link
Copy Markdown
Member

@foremtehan Thanks for taking the time to submit this PR.

Hypervel closely tracks Laravel’s public APIs and behavior. Laravel’s collections use json_encode() directly; routing them through Eloquent’s JSON cast helper would make model-attribute encoder configuration affect ordinary collection serialization too. It would also introduce a Database dependency into Collections, breaking standalone installations.

Without a custom encoder, this adds a method call and callback check before calling the same native JSON encoder, with no benefit to the default behavior.

For a general feature like configurable collection serialization, please propose it to Laravel first. If accepted upstream, we can consider porting it. Hypervel-specific improvements, such as coroutine safety and Swoole integration, remain welcome here.

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