From 8e1ae5f370254385df3056a1baa1a05f0f13ee53 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 10 Jan 2020 16:57:14 +0100 Subject: [PATCH] PhpWriter: fixed invalid code for ternary [Closes #210] --- src/Latte/Compiler/PhpWriter.php | 2 +- tests/Latte/PhpWriter.formatArgs().phpt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Latte/Compiler/PhpWriter.php b/src/Latte/Compiler/PhpWriter.php index ef3b74e1c..e06bce145 100644 --- a/src/Latte/Compiler/PhpWriter.php +++ b/src/Latte/Compiler/PhpWriter.php @@ -254,7 +254,7 @@ public function shortTernaryPass(MacroTokens $tokens): MacroTokens $inTernary = $tmp = []; $errors = 0; while ($tokens->nextToken()) { - if ($tokens->isCurrent('?') && $tokens->isNext() && !$tokens->isNext(':', ',', ')', ']', '|')) { + if ($tokens->isCurrent('?') && $tokens->isNext() && !$tokens->isNext(',', ')', ']', '|')) { $inTernary[] = $tokens->depth; $tmp[] = $tokens->isNext('['); diff --git a/tests/Latte/PhpWriter.formatArgs().phpt b/tests/Latte/PhpWriter.formatArgs().phpt index 7db20f9d3..f7b251d9f 100644 --- a/tests/Latte/PhpWriter.formatArgs().phpt +++ b/tests/Latte/PhpWriter.formatArgs().phpt @@ -63,6 +63,8 @@ test(function () { // short ternary operators Assert::same("'a'?", formatArgs('a?')); // value must exists Assert::same('$a ?(1) : null', formatArgs('$a?(1)')); // with braces Assert::same('$a ? \Foo::BAR : null', formatArgs('$a ? \Foo::BAR')); + Assert::same('$c ?: ($a ?: $b)', formatArgs('$c ?: ($a ?: $b)')); + Assert::same('$c ? ($a ?: $b) : null', formatArgs('$c ? ($a ?: $b)')); });