Skip to content

Commit

Permalink
SyntaxError::translateTokens(): prevent double translation
Browse files Browse the repository at this point in the history
Generally speaking, the `$translate` parameter for the `SyntaxError::getNormalizedMessage()` method should only be passed as `true` when on a PHP version which doesn't do the PHP native token translation yet.

However, in edge cases, it could be possible that tokens could be double "translated", both by PHP itself as well as by the `SyntaxError::translateTokens()` method.

This minor fix prevents this by not matching token names when surrounded by parentheses.

Includes additional unit tests.
  • Loading branch information
jrfnl authored and grogy committed Apr 19, 2022
1 parent 0ca22f4 commit a93187e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Errors/SyntaxError.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function translateTokens($message)
'T_ECHO' => 'echo'
);

return preg_replace_callback('~T_([A-Z_]*)~', function ($matches) use ($translateTokens) {
return preg_replace_callback('~(?<!\()T_([A-Z_]*)(?!\))~', function ($matches) use ($translateTokens) {
list($tokenName) = $matches;
if (isset($translateTokens[$tokenName])) {
$operator = $translateTokens[$tokenName];
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Errors/SyntaxErrorTranslateTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public function dataTranslateTokens()
'message' => 'Unexpected T_INC, T_IS_IDENTICAL, T_OBJECT_OPERATOR, T_START_HEREDOC',
'expected' => 'Unexpected ++ (T_INC), === (T_IS_IDENTICAL), -> (T_OBJECT_OPERATOR), <<< (T_START_HEREDOC)',
),
'PHP 5.4-style message with token name with PHP native translation [1] - prevent double translation' => array(
'message' => 'Unexpected \'__FILE__\' (T_FILE), expecting T_STRING',
'expected' => 'Unexpected \'__FILE__\' (T_FILE), expecting T_STRING',
),
'PHP 5.4-style message with token name with PHP native translation [2] - prevent double translation' => array(
'message' => 'Unexpected \'++\' (T_INC) in',
'expected' => 'Unexpected \'++\' (T_INC) in',
),
'Message with multiple tokens with PHP native translation' => array(
'message' => 'Unexpected ++ (T_INC), === (T_IS_IDENTICAL), -> (T_OBJECT_OPERATOR), <<< (T_START_HEREDOC)',
'expected' => 'Unexpected ++ (T_INC), === (T_IS_IDENTICAL), -> (T_OBJECT_OPERATOR), <<< (T_START_HEREDOC)',
),
);
}
}

0 comments on commit a93187e

Please sign in to comment.