Skip to content
Permalink
Browse files

deprecated syntax {var xx} and {var $xx => ...} (BC break)

  • Loading branch information
dg committed Jan 8, 2020
1 parent 3f92a90 commit 59a64b4f0bca193a37b829616f519ab98ec20f7e
Showing with 23 additions and 12 deletions.
  1. +7 −0 src/Latte/Macros/CoreMacros.php
  2. +13 −9 tests/Latte/CoreMacros.var.default.phpt
  3. +3 −3 tests/Latte/templates/general.latte
@@ -415,6 +415,10 @@ public function macroVar(MacroNode $node, PhpWriter $writer)
$tokens = $writer->preprocess();
$res = new Latte\MacroTokens;
while ($tokens->nextToken()) {
if ($var && $tokens->isCurrent($tokens::T_SYMBOL)) {
trigger_error("Inside macro {{$node->name} {$node->args}} should be '{$tokens->currentValue()}' replaced with '\${$tokens->currentValue()}'", E_USER_DEPRECATED);
}

if ($var && $tokens->isCurrent($tokens::T_SYMBOL, $tokens::T_VARIABLE)) {
if ($node->name === 'default') {
$res->append("'" . ltrim($tokens->currentValue(), '$') . "'");
@@ -424,6 +428,9 @@ public function macroVar(MacroNode $node, PhpWriter $writer)
$var = null;

} elseif ($tokens->isCurrent('=', '=>') && $tokens->depth === 0) {
if ($tokens->isCurrent('=>')) {
trigger_error("Inside macro {{$node->name} {$node->args}} should be => replaced with =", E_USER_DEPRECATED);
}
$res->append($node->name === 'default' ? '=>' : '=');
$var = false;

@@ -17,12 +17,14 @@ $compiler = new Latte\Compiler;
CoreMacros::install($compiler);

test(function () use ($compiler) { // {var ... }
Assert::same('<?php $var=null; $var2=null; ?>', $compiler->expandMacro('var', 'var, var2', '')->openingCode);
Assert::same('<?php $var = \'hello\'; ?>', $compiler->expandMacro('var', 'var => hello', '')->openingCode);
Assert::same('<?php $var = \'hello\'; $var2 = \'world\'; ?>', $compiler->expandMacro('var', 'var => hello, var2 = world', '')->openingCode);
Assert::same('<?php $var = 123; ?>', $compiler->expandMacro('var', '$var => 123', '')->openingCode);
Assert::same('<?php $var=null; $var2=null; ?>', @$compiler->expandMacro('var', 'var, var2', '')->openingCode); // @ deprecated syntax
Assert::same('<?php $var=null; $var2=null; ?>', $compiler->expandMacro('var', '$var, $var2', '')->openingCode);
Assert::same('<?php $var = \'hello\'; ?>', @$compiler->expandMacro('var', 'var => hello', '')->openingCode); // @ deprecated syntax
Assert::same('<?php $var = \'hello\'; $var2 = \'world\'; ?>', @$compiler->expandMacro('var', 'var => hello, var2 = world', '')->openingCode); // @ deprecated syntax
Assert::same('<?php $var = 123; ?>', @$compiler->expandMacro('var', '$var => 123', '')->openingCode); // @ deprecated syntax
Assert::same('<?php $var = 123; ?>', $compiler->expandMacro('var', '$var = 123', '')->openingCode);
Assert::same('<?php $var1 = 123; $var2 = "nette framework"; ?>', $compiler->expandMacro('var', 'var1 = 123, $var2 => "nette framework"', '')->openingCode);
Assert::same('<?php $var1 = 123; $var2 = "nette framework"; ?>', @$compiler->expandMacro('var', 'var1 = 123, $var2 => "nette framework"', '')->openingCode); // @ deprecated syntax
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);

Assert::exception(function () use ($compiler) {
@@ -36,11 +38,13 @@ test(function () use ($compiler) { // {var ... }


test(function () use ($compiler) { // {default ...}
Assert::same("<?php extract(['var'=>null, 'var2'=>null], EXTR_SKIP) ?>", $compiler->expandMacro('default', 'var, var2', '')->openingCode);
Assert::same("<?php extract(['var' => 'hello'], EXTR_SKIP) ?>", $compiler->expandMacro('default', 'var => hello', '')->openingCode);
Assert::same("<?php extract(['var' => 123], EXTR_SKIP) ?>", $compiler->expandMacro('default', '$var => 123', '')->openingCode);
Assert::same("<?php extract(['var'=>null, 'var2'=>null], EXTR_SKIP) ?>", @$compiler->expandMacro('default', 'var, var2', '')->openingCode); // @ deprecated syntax
Assert::same("<?php extract(['var'=>null, 'var2'=>null], EXTR_SKIP) ?>", $compiler->expandMacro('default', '$var, $var2', '')->openingCode);
Assert::same("<?php extract(['var' => 'hello'], EXTR_SKIP) ?>", @$compiler->expandMacro('default', 'var => hello', '')->openingCode); // @ deprecated syntax
Assert::same("<?php extract(['var' => 123], EXTR_SKIP) ?>", @$compiler->expandMacro('default', '$var => 123', '')->openingCode); // @ deprecated syntax
Assert::same("<?php extract(['var' => 123], EXTR_SKIP) ?>", $compiler->expandMacro('default', '$var = 123', '')->openingCode);
Assert::same("<?php extract(['var1' => 123, 'var2' => \"nette framework\"], EXTR_SKIP) ?>", $compiler->expandMacro('default', 'var1 = 123, $var2 => "nette framework"', '')->openingCode);
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);

Assert::exception(function () use ($compiler) {
$compiler->expandMacro('default', '$temp->var1 = 123', '');
@@ -109,10 +109,10 @@ var prop2 = {$people};
{default $varx = 2}{$varx}
{default varx => 4}{$varx}
{default $varx = 4}{$varx}
{var varx => 8}{$varx}
{var $varx => 9}{$varx}
{var $varx = 8}{$varx}
{var $varx = 9}{$varx}
{var $now = new DateTime}
{var $foo = $now->format('u')}

0 comments on commit 59a64b4

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