Skip to content
Permalink
Browse files

added support for types in {var type $var} and {default type $var}

  • Loading branch information
dg committed Jan 8, 2020
1 parent 59a64b4 commit 441d52a24af4a916bbd0b4ddfe14c4ed4bd556c4
Showing with 29 additions and 2 deletions.
  1. +8 −2 src/Latte/Macros/CoreMacros.php
  2. +21 −0 tests/Latte/CoreMacros.var.default.phpt
@@ -412,11 +412,16 @@ public function macroVar(MacroNode $node, PhpWriter $writer)
}

$var = true;
$tokens = $writer->preprocess();
$tokens = $node->tokenizer;
$res = new Latte\MacroTokens;
while ($tokens->nextToken()) {
if ($var && $tokens->isCurrent($tokens::T_SYMBOL)) {
if ($var && $tokens->isCurrent($tokens::T_SYMBOL) && ($tokens->isNext(',', '=>', '=') || !$tokens->isNext())) {
trigger_error("Inside macro {{$node->name} {$node->args}} should be '{$tokens->currentValue()}' replaced with '\${$tokens->currentValue()}'", E_USER_DEPRECATED);

} elseif ($var && $tokens->isCurrent($tokens::T_SYMBOL, '?', 'null', '\\')) { // type
$tokens->nextToken();
$tokens->nextAll($tokens::T_SYMBOL, '\\', '|', '[', ']', '?', 'null');
continue;
}

if ($var && $tokens->isCurrent($tokens::T_SYMBOL, $tokens::T_VARIABLE)) {
@@ -451,6 +456,7 @@ public function macroVar(MacroNode $node, PhpWriter $writer)
if ($var === null) {
$res->append($node->name === 'default' ? '=>null' : '=null');
}
$res = $writer->preprocess($res);
$out = $writer->quotingPass($res)->joinAll();
return $node->name === 'default' ? "extract([$out], EXTR_SKIP)" : "$out;";
}
@@ -27,13 +27,25 @@ test(function () use ($compiler) { // {var ... }
Assert::same('<?php $var1 = 123; $var2 = "nette framework"; ?>', $compiler->expandMacro('var', '$var1 = 123, $var2 = "nette framework"', '')->openingCode);
Assert::same('<?php $temp->var1 = 123; ?>', $compiler->expandMacro('var', '$temp->var1 = 123', '')->openingCode);

// types
Assert::same('<?php ; ; ?>', $compiler->expandMacro('var', 'int var, string var2', '')->openingCode); // invalid syntax
Assert::same('<?php $temp->var1 = 123; ?>', $compiler->expandMacro('var', 'int $temp->var1 = 123', '')->openingCode);
Assert::same('<?php $temp->var1 = 123; ?>', $compiler->expandMacro('var', 'null|int|?string[] $temp->var1 = 123', '')->openingCode);
Assert::same('<?php $var1 = 123; $var2 = "nette framework"; ?>', $compiler->expandMacro('var', 'int|string[] $var1 = 123, ?class|null $var2 = "nette framework"', '')->openingCode);
Assert::same('<?php $var1 = 123; $var2 = 456; ?>', $compiler->expandMacro('var', 'A\B $var1 = 123, ?A\B $var2 = 456', '')->openingCode);
Assert::same('<?php $var1 = 123; $var2 = 456; ?>', $compiler->expandMacro('var', '\A\B $var1 = 123, ?\A\B $var2 = 456', '')->openingCode);

// errors
Assert::exception(function () use ($compiler) {
$compiler->expandMacro('var', '$var => "123', '');
}, Latte\CompileException::class, 'Unexpected %a% on line 1, column 9.');

Assert::exception(function () use ($compiler) {
$compiler->expandMacro('var', '$var => 123', '|filter');
}, Latte\CompileException::class, 'Modifiers are not allowed in {var}');

// preprocess
Assert::same("<?php \$temp->var1 = true ? 'a' : null; ?>", $compiler->expandMacro('var', '$temp->var1 = true ? a', '')->openingCode);
});


@@ -46,11 +58,20 @@ test(function () use ($compiler) { // {default ...}
Assert::same("<?php extract(['var1' => 123, 'var2' => \"nette framework\"], EXTR_SKIP) ?>", @$compiler->expandMacro('default', 'var1 = 123, $var2 => "nette framework"', '')->openingCode); // @ deprecated syntax
Assert::same("<?php extract(['var1' => 123, 'var2' => \"nette framework\"], EXTR_SKIP) ?>", $compiler->expandMacro('default', '$var1 = 123, $var2 = "nette framework"', '')->openingCode);

// types
Assert::same('<?php extract([, ], EXTR_SKIP) ?>', $compiler->expandMacro('default', 'int var, string var2', '')->openingCode); // invalid syntax
Assert::same("<?php extract([ 'var' => 123], EXTR_SKIP) ?>", $compiler->expandMacro('default', 'null|int|?string[] $var = 123', '')->openingCode);
Assert::same("<?php extract([ 'var1' => 123, 'var2' => \"nette framework\"], EXTR_SKIP) ?>", $compiler->expandMacro('default', 'int|string[] $var1 = 123, ?class|null $var2 = "nette framework"', '')->openingCode);

// errors
Assert::exception(function () use ($compiler) {
$compiler->expandMacro('default', '$temp->var1 = 123', '');
}, Latte\CompileException::class, "Unexpected '->' in {default \$temp->var1 = 123}");

Assert::exception(function () use ($compiler) {
$compiler->expandMacro('default', '$var => 123', '|filter');
}, Latte\CompileException::class, 'Modifiers are not allowed in {default}');

// preprocess
Assert::same("<?php extract(['var1' => true ? 'a' : null], EXTR_SKIP) ?>", $compiler->expandMacro('default', '$var1 = true ? a', '')->openingCode);
});

0 comments on commit 441d52a

Please sign in to comment.
You can’t perform that action at this time.