Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public function parseFilterExpressionRaw($node, $tag = null)
* Parses arguments.
*
* @param bool $namedArguments Whether to allow named arguments or not
* @param bool $definition Whether we are parsing arguments for a function definition
* @param bool $definition Whether we are parsing arguments for a function (or macro) definition
*
* @return Node
*
Expand Down Expand Up @@ -642,6 +642,7 @@ public function parseArguments($namedArguments = false, $definition = false, $al
if (null === $name) {
$name = $value->getAttribute('name');
$value = new ConstantExpression(null, $this->parser->getCurrentToken()->getLine());
$value->setAttribute('isImplicit', true);
}
$args[$name] = $value;
} else {
Expand Down
32 changes: 32 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Error\SyntaxError;
use Twig\Lexer;
use Twig\Loader\LoaderInterface;
use Twig\Node\Node;
use Twig\Node\SetNode;
Expand Down Expand Up @@ -175,6 +176,37 @@ public function testGetVarName()
$this->addToAssertionCount(1);
}

public function testImplicitMacroArgumentDefaultValues()
{
$template = '{% macro marco (po, lo = null) %}{% endmacro %}';
$lexer = new Lexer(new Environment($this->createMock(LoaderInterface::class)));
$stream = $lexer->tokenize(new Source($template, 'index'));

$argumentNodes = $this->getParser()
->parse($stream)
->getNode('macros')
->getNode('marco')
->getNode('arguments')
;

self::assertTrue(
$argumentNodes->getNode('po')->hasAttribute('isImplicit')
);
self::assertTrue(
$argumentNodes->getNode('po')->getAttribute('isImplicit')
);
self::assertNull(
$argumentNodes->getNode('po')->getAttribute('value')
);

self::assertFalse(
$argumentNodes->getNode('lo')->hasAttribute('isImplicit')
);
self::assertNull(
$argumentNodes->getNode('lo')->getAttribute('value')
);
}

protected function getParser()
{
$parser = new Parser(new Environment($this->createMock(LoaderInterface::class)));
Expand Down