Skip to content

Commit

Permalink
Revert "PhpWriter: (expand) is converted to ..."
Browse files Browse the repository at this point in the history
This reverts commit 8062393 and 16f91e7.

Unpacking is not working inside array
  • Loading branch information
dg committed Feb 20, 2016
1 parent 5d5520a commit 8645679
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
33 changes: 24 additions & 9 deletions src/Latte/PhpWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ function ($m) use ($word, & $args) {
case 'args':
$code = $this->formatArgs(); break;
case 'array':
$code = $this->formatArgs();
$code = $cond && $code === '' ? '' : "[$code]"; break;
$code = $this->formatArray();
$code = $cond && $code === '[]' ? '' : $code; break;
case 'var':
$code = var_export($arg, TRUE); break;
case 'raw':
Expand Down Expand Up @@ -123,10 +123,16 @@ public function formatArgs(MacroTokens $tokens = NULL)
}


/** @deprecated */
/**
* Formats macro arguments to PHP array. (It advances tokenizer to the end as a side effect.)
* @return string
*/
public function formatArray(MacroTokens $tokens = NULL)
{
return '[' . $this->formatArgs($tokens) . ']';
$tokens = $this->preprocess($tokens);
$tokens = $this->expandFilter($tokens);
$tokens = $this->quoteFilter($tokens);
return $tokens->joinAll();
}


Expand All @@ -152,7 +158,6 @@ public function preprocess(MacroTokens $tokens = NULL)
$tokens = $tokens === NULL ? $this->tokens : $tokens;
$tokens = $this->removeCommentsFilter($tokens);
$tokens = $this->shortTernaryFilter($tokens);
$tokens = $this->expandFilter($tokens);
return $tokens;
}

Expand Down Expand Up @@ -208,15 +213,25 @@ public function shortTernaryFilter(MacroTokens $tokens)
*/
public function expandFilter(MacroTokens $tokens)
{
$res = new MacroTokens;
$res = new MacroTokens('[');
$expand = NULL;
while ($tokens->nextToken()) {
if ($tokens->isCurrent('(expand)')) {
$tokens->nextAll(MacroTokens::T_WHITESPACE);
$res->append('...');
if ($tokens->isCurrent('(expand)') && $tokens->depth === 0) {
$expand = TRUE;
$res->append('],');
} elseif ($expand && $tokens->isCurrent(',') && !$tokens->depth) {
$expand = FALSE;
$res->append(', [');
} else {
$res->append($tokens->currentToken());
}
}

if ($expand === NULL) {
$res->append(']');
} else {
$res->prepend('array_merge(')->append($expand ? ', [])' : '])');
}
return $res;
}

Expand Down
6 changes: 0 additions & 6 deletions tests/Latte/PhpWriter.formatArgs().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ test(function () { // short ternary operators
});


test(function () { // variadics/expand operator
Assert::same("'item', \$list, ...\$list", formatArgs('item, $list, ...$list'));
Assert::same("'item', \$list, ...\$list", formatArgs('item, $list, (expand) $list'));
});


test(function () { // special
Assert::same('$var', formatArgs('$var'));
Assert::same('$var => $var', formatArgs('$var => $var'));
Expand Down
2 changes: 1 addition & 1 deletion tests/Latte/PhpWriter.formatArray().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ test(function () { // symbols


test(function () { // expand
Assert::same("['item', \$list, ...\$list]", formatArray('item, $list, (expand) $list'));
Assert::same('array_merge([\'item\', $list, ], $list, [])', formatArray('item, $list, (expand) $list'));
});

0 comments on commit 8645679

Please sign in to comment.