Skip to content

Commit

Permalink
minor #3935 Simplify generated code by using named arguments (fabpot)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Simplify generated code by using named arguments

Commits
-------

bce22e4 Use named arguments
a64561b Simplify code
  • Loading branch information
fabpot committed Dec 9, 2023
2 parents aa4b59c + bce22e4 commit a2eb71c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
28 changes: 19 additions & 9 deletions src/Node/Expression/GetAttrExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,27 @@ public function compile(Compiler $compiler): void
;

if ($this->hasNode('arguments')) {
$compiler->raw(', ')->subcompile($this->getNode('arguments'));
} else {
$compiler->raw(', []');
$compiler->raw(', arguments: ')->subcompile($this->getNode('arguments'));
}

$compiler->raw(', ')
->repr($this->getAttribute('type'))
->raw(', ')->repr($this->getAttribute('is_defined_test'))
->raw(', ')->repr($this->getAttribute('ignore_strict_check'))
->raw(', ')->repr($env->hasExtension(SandboxExtension::class))
->raw(', ')->repr($this->getNode('node')->getTemplateLine())
if (Template::ANY_CALL !== $type = $this->getAttribute('type')) {
$compiler->raw(', type: ')->repr($type);
}

if ($this->getAttribute('is_defined_test')) {
$compiler->raw(', isDefinedTest: true');
}

if ($this->getAttribute('ignore_strict_check')) {
$compiler->raw(', ignoreStrictCheck: true');
}

if ($env->hasExtension(SandboxExtension::class)) {
$compiler->raw(', sandboxed: true');
}

$compiler
->raw(', lineno: ')->repr($this->getNode('node')->getTemplateLine())
->raw(')')
;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Test/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Twig\Test;

use PHPUnit\Framework\Constraint\Exception;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Error\Error;
Expand Down Expand Up @@ -235,8 +236,7 @@ protected function doIntegrationTest($file, $message, $condition, $templates, $e

if (false !== $exception) {
[$class] = explode(':', $exception);
$constraintClass = class_exists('PHPUnit\Framework\Constraint\Exception') ? 'PHPUnit\Framework\Constraint\Exception' : 'PHPUnit_Framework_Constraint_Exception';
$this->assertThat(null, new $constraintClass($class));
$this->assertThat(null, new Exception($class));
}

$expected = trim($match[3], "\n ");
Expand Down
4 changes: 2 additions & 2 deletions tests/Node/Expression/GetAttrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getTests()
$attr = new ConstantExpression('bar', 1);
$args = new ArrayExpression([], 1);
$node = new GetAttrExpression($expr, $attr, $args, Template::ANY_CALL, 1);
$tests[] = [$node, sprintf('%s%s, "bar", [], "any", false, false, false, 1)', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1))];
$tests[] = [$node, sprintf('%s%s, "bar", arguments: [], lineno: 1)', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1))];

$node = new GetAttrExpression($expr, $attr, $args, Template::ARRAY_CALL, 1);
$tests[] = [$node, '(($__internal_%s = // line 1'."\n".
Expand All @@ -53,7 +53,7 @@ public function getTests()
$args->addElement(new NameExpression('foo', 1));
$args->addElement(new ConstantExpression('bar', 1));
$node = new GetAttrExpression($expr, $attr, $args, Template::METHOD_CALL, 1);
$tests[] = [$node, sprintf('%s%s, "bar", [%s, "bar"], "method", false, false, false, 1)', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1), $this->getVariableGetter('foo'))];
$tests[] = [$node, sprintf('%s%s, "bar", arguments: [%s, "bar"], type: "method", lineno: 1)', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1), $this->getVariableGetter('foo'))];

return $tests;
}
Expand Down

0 comments on commit a2eb71c

Please sign in to comment.