server icon indicating copy to clipboard operation
server copied to clipboard

[Feature] display \Stringable values of objects

Open iGrog opened this issue 2 years ago • 6 comments

I have a complex object with pure __toString() method. Or object implements Stringable interface

It would be nice, if Buggregator shows result of that __toString() somewhere (without need to expand object)

class ComplexObject implements \Stringable
{
    public function __construct(private int $a, private string $b, private \DateTimeImmutable $c) {}

    public function __toString(): string
    {
        return $this->a . ' / ' . $this->b . ' = ' . $this->c->format('Y-m-d');
    }
}

example:

        $obj = new ComplexObject(5, 'Hello World', new \DateTimeImmutable());
        dd($obj);

Expected view:

image

image

Actual view: image

image

iGrog avatar Dec 04 '23 13:12 iGrog

All information is obtained using the dump and dd functions from the var-dumper tool. If these functions provide the necessary information, we will utilize it. Perhaps @roxblnfk could offer additional insights or context in the trap package on GitHub?

butschster avatar Dec 04 '23 17:12 butschster

Need to investigate.

roxblnfk avatar Dec 04 '23 18:12 roxblnfk

@roxblnfk are you able to share the investigation result?

Kreezag avatar Jun 17 '24 18:06 Kreezag

Hi. Yes, of course. After diving into varDumper, I can confidently say that we can send any metadata along with the main content. This can include the result of calling the __toString() and for example getSolution() methods in FriendlyException, and anything else.

However, there are still unresolved questions:

  1. What if the content from __toString is quite large?
  2. What if calling __toString is undesirable because this call might affect something (mutable behavior, additional resource consumption)?
  3. Is a separate option needed for this? If so, what should it be and what should its default value be?

roxblnfk avatar Jun 17 '24 19:06 roxblnfk

Is it somehow possible to get these answers from attributes? Like this:

#[Pure] // phpstorm uses this to display toString value in debugger. Only `Pure` functions are evaluated
#[Buggregator(show: true)] // naming is up to you :)
public function __toString(): string
{
    return $this->variable;
}

iGrog avatar Jun 26 '24 21:06 iGrog

#[Pure] // phpstorm uses this to display toString value in debugger. Only Pure functions are evaluated

Something new for me. Thanks. I think we can reuse this attribute for \Stringable variables. @butschster need to think how it might be rendered and what context name or structure we will use

roxblnfk avatar Jun 27 '24 10:06 roxblnfk