[Feature] display \Stringable values of objects
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:
Actual view:
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?
Need to investigate.
@roxblnfk are you able to share the investigation result?
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:
- What if the content from
__toStringis quite large? - What if calling
__toStringis undesirable because this call might affect something (mutable behavior, additional resource consumption)? - Is a separate option needed for this? If so, what should it be and what should its default value be?
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;
}
#[Pure]// phpstorm uses this to display toString value in debugger. OnlyPurefunctions 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