Skip to content

Mixed data type not supported on QueryCollector addMessage method #2008

@ssw1cblarrion

Description

@ssw1cblarrion

public function addMessage(string $message): void

I also have a problem here, if I want to send debugbar('queries')->addMessage($my_collection->count()) i get TypeError
it's because $message expects a string, and count is a integer

Some clean way to handle int, float, maybe bool,
What about arrays, Stringable?

of course and I'm now doing a cast debugbar('queries')->addMessage((string) $my_collection->count()), but it's annoying to do it in every message.

maybe (ChatGPT suggestion)

public function addMessage(mixed $message) {
    ///...
    'sql' => $this->normalizeToString($message),
    ///...
}
private function normalizeToString(mixed $value): string
{
    if ($value === null) {
        return 'null';
    }

    if (is_scalar($value)) {
        return (string) $value;
    }

    if (is_object($value) && method_exists($value, '__toString')) {
        return (string) $value;
    }

    if (is_array($value) || is_object($value)) {
       $json = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        return $json !== false ? $json : '[json encode error]';
    }

    // fallback (resources, etc.)
    return '[unstringable value]';
}

maybe a formatter already exists for this and can be reused, any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions