mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-10 09:26:29 +00:00
minor #4205 Make Node::__toString() more readable (fabpot)
This PR was merged into the 3.x branch.
Discussion
----------
Make Node::__toString() more readable
Commits
-------
b431ecad Make Node::__toString() more readable
This commit is contained in:
@@ -39,6 +39,11 @@ abstract class AbstractTwigCallable implements TwigCallableInterface
|
||||
], $options);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf('%s(%s)', static::class, $this->name);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
|
||||
+9
-1
@@ -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)];
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace Twig;
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
interface TwigCallableInterface
|
||||
interface TwigCallableInterface extends \Stringable
|
||||
{
|
||||
public function getName(): string;
|
||||
|
||||
|
||||
@@ -15,6 +15,9 @@ use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
|
||||
use Twig\Node\NameDeprecation;
|
||||
use Twig\Node\Node;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
use Twig\TwigTest;
|
||||
|
||||
class NodeTest extends TestCase
|
||||
{
|
||||
@@ -28,6 +31,17 @@ class NodeTest extends TestCase
|
||||
$this->assertEquals('Twig\Node\Node(value: \Closure)', (string) $node);
|
||||
}
|
||||
|
||||
public function testToStringWithTwigCallables()
|
||||
{
|
||||
$node = new Node([], [
|
||||
'function' => new TwigFunction('a_function'),
|
||||
'filter' => new TwigFilter('a_filter'),
|
||||
'test' => new TwigTest('a_test'),
|
||||
], 1);
|
||||
|
||||
$this->assertEquals('Twig\Node\Node(function: Twig\TwigFunction(a_function), filter: Twig\TwigFilter(a_filter), test: Twig\TwigTest(a_test))', (string) $node);
|
||||
}
|
||||
|
||||
public function testAttributeDeprecationIgnore()
|
||||
{
|
||||
$node = new Node([], ['foo' => false]);
|
||||
|
||||
Reference in New Issue
Block a user