Skip to content

Commit

Permalink
added support for types in {var type $var} and {default type $var}
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 12, 2020
1 parent 6e3176b commit 02ab8e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Latte/Macros/CoreMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;";
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Latte/CoreMacros.var.default.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ 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);

// 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);
});


Expand All @@ -46,11 +56,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 02ab8e4

Please sign in to comment.