Skip to content

ConvertsToJson::toJson() can return false despite its : string return type #92

Description

@dru1x

Summary

ConvertsToJson::toJson() calls json_encode() with no flags, so it can return false on encoding failure despite its declared : string return type. Every other JSON boundary in the codebase throws instead.

Details

src/Support/ConvertsToJson.php:24-27:

public function toJson(): string
{
    return json_encode($this);
}

json_encode() returns false on failure (e.g. malformed UTF-8 in a title/body/data field), which violates the declared : string return type. Without strict_types in this file, this doesn't fatal immediately at the call site, but it's a silent false-as-string bug waiting to surface — e.g. under strict_types=1, or in any caller that treats the result as a genuine JSON string (writing it to a file, sending it over HTTP, etc.).

By contrast:

  • ConvertsFromJson::jsonDecode() (src/Support/ConvertsFromJson.php:28) uses JSON_THROW_ON_ERROR.
  • SendNotificationsRequest::preventTooMuchMessageData() (src/Request/SendNotificationsRequest.php:164) uses JSON_THROW_ON_ERROR.

toJson() is the odd one out.

Suggested fix

Add the JSON_THROW_ON_ERROR flag to json_encode() in toJson(), matching the rest of the codebase's JSON-boundary handling.

Test gap

No test currently encodes a value that would fail JSON encoding (e.g. invalid UTF-8 in a message field) to confirm a JsonException is thrown rather than a silent false.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions