Skip to content

Commit

Permalink
Notice: improved deprecated expressions messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 18, 2022
1 parent 29cb516 commit 17dc397
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
23 changes: 18 additions & 5 deletions src/Latte/Compiler/MacroTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ public function fetchWord(): ?string
$expr = $this->nextValue('(') . $this->joinUntilSameDepth(')') . $this->nextValue(')');
} else {
$expr = $this->joinUntilSameDepth(self::T_WHITESPACE, ',');
if (preg_match('~[\w-]+\{?\$~A', $expr)) {
trigger_error("The expression '$expr' should be put in double quotes.", E_USER_DEPRECATED);
}

if ($this->isNext(...[
'%', '&', '*', '.', '<', '=', '>', '?', '^', '|', ':',
'::', '=>', '->', '?->', '??->', '<<', '>>', '<=>', '<=', '>=', '===', '!==', '==', '!=', '<>', '&&', '||', '??', '**',
Expand All @@ -124,6 +120,19 @@ public function fetchWord(): ?string
}
}

if ($expr
&& !is_numeric($expr)
&& !preg_match('#^[$([] | [\'"\ ] | ^\w+(?:-+\w+)*$ | ^[\w\\\\]{3,}::[A-Z0-9_]{2,}$#Dx', $expr) // PhpWriter::formatWord(), T_SYMBOL
&& !preg_match('~ ([./@_a-z0-9#!-] | :(?!:) | \{\$ [_a-z0-9\[\]()>-]+ }){3,}+ $ ~xAi', $expr) // Latte 3 tokenizeUnquotedString()
) {
if (preg_match('~[\w-]+\$~A', $expr)) {
$curly = preg_replace('~\$\w+(->\w+)?~', '{$0}', $expr);
trigger_error("Put variables in curly brackets: '$curly' (or wrap whole expression in double quotes)", E_USER_DEPRECATED);
} else {
trigger_error("Expression '$expr' should be wrapped in double quotes.", E_USER_DEPRECATED);
}
}

$this->nextToken(',');
$this->nextAll(self::T_WHITESPACE, self::T_COMMENT);
return $expr === '' ? null : $expr;
Expand All @@ -136,7 +145,11 @@ public function fetchWord(): ?string
public function fetchWords(): array
{
do {
$words[] = $this->joinUntil(self::T_WHITESPACE, ',', ':');
$words[] = $expr = $this->joinUntil(self::T_WHITESPACE, ',', ':');
if (preg_match('~[\w-]+\$~A', $expr)) {
$curly = preg_replace('~\$\w+(->\w+)?~', '{$0}', $expr);
trigger_error("Put variables in curly brackets: '$curly' (or wrap whole expression in double quotes)", E_USER_DEPRECATED);
}
} while ($this->nextToken(':'));

if (count($words) === 1 && ($space = $this->nextValue(self::T_WHITESPACE))
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Macros/BlockMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ public function macroIfset(MacroNode $node, PhpWriter $writer)
}

$list = [];
while ([$name, $block] = $node->tokenizer->fetchWordWithModifier(['block', '#'])) {
while ([$name, $block] = @$node->tokenizer->fetchWordWithModifier(['block', '#'])) { // disable deprecated messages here
$list[] = $block || preg_match('~\w[\w-]*$~DA', $name)
? '$this->hasBlock(' . $writer->formatWord($name) . ')'
: 'isset(' . $writer->formatArgs(new Latte\MacroTokens($name)) . ')';
Expand Down
10 changes: 5 additions & 5 deletions tests/common/MacroTokens.fetchWord().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function testWord($expr, $word, $rest = '')

testWord('', null);
@testWord('$1d-,a', '$1d-', 'a'); // @ missing quotes
testWord('"item\'1""item2"', '"item\'1""item2"');
@testWord('"item\'1""item2"', '"item\'1""item2"');
testWord('(symbol)', '(symbol)');
testWord('($expr)', '($expr)');
testWord('($expr ? (1+2) : [3,4]),x', '($expr ? (1+2) : [3,4])', 'x');
Expand All @@ -40,9 +40,9 @@ testWord('word -1', 'word', '-1');
testWord('word -$num', 'word', '-$num');

Assert::error(function () {
testWord('a-$x', 'a-$x', '');
}, E_USER_DEPRECATED, "The expression 'a-\$x' should be put in double quotes.");
testWord('a-$x-$y->x', 'a-$x-$y->x', '');
}, E_USER_DEPRECATED, 'Put variables in curly brackets: \'a-{$x}-{$y->x}\' (or wrap whole expression in double quotes)');

Assert::error(function () {
testWord('a-{$x}', 'a-{$x}', '');
}, E_USER_DEPRECATED, "The expression 'a-{\$x}' should be put in double quotes.");
testWord('a-\x', 'a-\x', '');
}, E_USER_DEPRECATED, 'Expression \'a-\x\' should be wrapped in double quotes.');
2 changes: 1 addition & 1 deletion tests/tags/expected/snippet.block.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class Template%a% extends Latte\Runtime\Template
extract($ʟ_args);
unset($ʟ_args);
echo '<div';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId($ʟ_nm = "inner-$id")) . '"';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId($ʟ_nm = "inner-{$id}")) . '"';
echo '>
';
$this->global->snippetDriver->enter($ʟ_nm, 'dynamic') /* line %d% */;
Expand Down
4 changes: 2 additions & 2 deletions tests/tags/expected/snippet.dynamic.alt.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class Template%a% extends Latte\Runtime\Template
$iterations = 0;
foreach (array(1,2,3) as $id) /* line %d% */ {
echo ' <div';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId($ʟ_nm = "inner-$id")) . '"';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId($ʟ_nm = "inner-{$id}")) . '"';
echo '>
';
$this->global->snippetDriver->enter($ʟ_nm, 'dynamic') /* line %d% */;
Expand Down Expand Up @@ -76,7 +76,7 @@ final class Template%a% extends Latte\Runtime\Template
$iterations = 0;
foreach (array(1,2,3) as $id) /* line %d% */ {
echo ' <div';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId($ʟ_nm = "inner-$id")) . '"';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId($ʟ_nm = "inner-{$id}")) . '"';
echo '>
';
$this->global->snippetDriver->enter($ʟ_nm, 'dynamic') /* line %d% */;
Expand Down
2 changes: 1 addition & 1 deletion tests/tags/snippet.block.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $template = <<<'EOD'
{snippet outer}
begin
<div n:snippet='"inner-$id"' n:block="block2">
<div n:snippet="inner-{$id}" n:block="block2">
dynamic
</div>
end
Expand Down
4 changes: 2 additions & 2 deletions tests/tags/snippet.dynamic.alt.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $latte->setLoader(new Latte\Loaders\StringLoader);
$template = <<<'EOD'
{snippet outer1}
{foreach array(1,2,3) as $id}
<div n:snippet='"inner-$id"'>
<div n:snippet="inner-{$id}">
#{$id}
</div>
{/foreach}
Expand All @@ -27,7 +27,7 @@ $template = <<<'EOD'
{snippet outer2}
{foreach array(1,2,3) as $id}
<div n:inner-snippet='"inner-$id"'>
<div n:inner-snippet="inner-{$id}">
#{$id}
</div>
{/foreach}
Expand Down

0 comments on commit 17dc397

Please sign in to comment.