Make Node::__toString() more readable

This commit is contained in:
Fabien Potencier
2024-08-15 20:14:56 +02:00
parent 72d6d9c9c5
commit b431ecad3a
4 changed files with 29 additions and 2 deletions
+9 -1
View File
@@ -15,6 +15,7 @@ namespace Twig\Node;
use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Source;
use Twig\TwigCallableInterface;
/**
* Represents a node in the AST.
@@ -58,7 +59,14 @@ class Node implements \Countable, \IteratorAggregate
{
$attributes = [];
foreach ($this->attributes as $name => $value) {
$attributes[] = \sprintf('%s: %s', $name, \is_callable($value) ? '\Closure' : str_replace("\n", '', var_export($value, true)));
if (\is_callable($value)) {
$v = '\Closure';
} elseif ($value instanceof \Stringable) {
$v = (string) $value;
} else {
$v = str_replace("\n", '', var_export($value, true));
}
$attributes[] = \sprintf('%s: %s', $name, $v);
}
$repr = [static::class.'('.implode(', ', $attributes)];