Skip to content

Commit 2c1f747

Browse files
committed
SyntaxError::translateTokens(): prevent double translation
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.
1 parent a6e5237 commit 2c1f747

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Errors/SyntaxError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected function translateTokens($message)
9292
'T_ECHO' => 'echo'
9393
);
9494

95-
return preg_replace_callback('~T_([A-Z_]*)~', function ($matches) use ($translateTokens) {
95+
return preg_replace_callback('~(?<!\()T_([A-Z_]*)(?!\))~', function ($matches) use ($translateTokens) {
9696
list($tokenName) = $matches;
9797
if (isset($translateTokens[$tokenName])) {
9898
$operator = $translateTokens[$tokenName];

tests/Unit/Errors/SyntaxErrorTranslateTokensTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ public function dataTranslateTokens()
6060
'message' => 'Unexpected T_INC, T_IS_IDENTICAL, T_OBJECT_OPERATOR, T_START_HEREDOC',
6161
'expected' => 'Unexpected ++ (T_INC), === (T_IS_IDENTICAL), -> (T_OBJECT_OPERATOR), <<< (T_START_HEREDOC)',
6262
),
63+
'PHP 5.4-style message with token name with PHP native translation [1] - prevent double translation' => array(
64+
'message' => 'Unexpected \'__FILE__\' (T_FILE), expecting T_STRING',
65+
'expected' => 'Unexpected \'__FILE__\' (T_FILE), expecting T_STRING',
66+
),
67+
'PHP 5.4-style message with token name with PHP native translation [2] - prevent double translation' => array(
68+
'message' => 'Unexpected \'++\' (T_INC) in',
69+
'expected' => 'Unexpected \'++\' (T_INC) in',
70+
),
71+
'Message with multiple tokens with PHP native translation' => array(
72+
'message' => 'Unexpected ++ (T_INC), === (T_IS_IDENTICAL), -> (T_OBJECT_OPERATOR), <<< (T_START_HEREDOC)',
73+
'expected' => 'Unexpected ++ (T_INC), === (T_IS_IDENTICAL), -> (T_OBJECT_OPERATOR), <<< (T_START_HEREDOC)',
74+
),
6375
);
6476
}
6577
}

0 commit comments

Comments
 (0)