From a6e5237375b88a3383d6178466fe0f71542da974 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 20 Feb 2022 16:56:50 +0100 Subject: [PATCH 1/2] Tests: add dedicated test for the `SyntaxError::translateTokens()` method This safeguards and documents the current behaviour of the method. --- .../Errors/SyntaxErrorTranslateTokensTest.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/Unit/Errors/SyntaxErrorTranslateTokensTest.php diff --git a/tests/Unit/Errors/SyntaxErrorTranslateTokensTest.php b/tests/Unit/Errors/SyntaxErrorTranslateTokensTest.php new file mode 100644 index 0000000..5760dce --- /dev/null +++ b/tests/Unit/Errors/SyntaxErrorTranslateTokensTest.php @@ -0,0 +1,65 @@ +assertSame($expected, $error->getNormalizedMessage(true)); + } + + /** + * Data provider. + * + * @return array + */ + public function dataTranslateTokens() + { + return array( + 'No token name in message' => array( + 'message' => 'Methods with the same name as their class will not be constructors in a future version of PHP', + 'expected' => 'Methods with the same name as their class will not be constructors in a future version of PHP', + ), + 'Non-existent token name in message (shouldn\'t be possible)' => array( + 'message' => 'Unexpected T_1H2, expecting T_STRING', + 'expected' => 'Unexpected T_1H2, expecting T_STRING', + ), + 'Token names in message, but not in translation list' => array( + // phpcs:disable Generic.Files.LineLength.TooLong + 'message' => 'Unexpected \'\' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)', + 'expected' => 'Unexpected \'\' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)', + // phpcs:enable Generic.Files.LineLength + ), + 'PHP 5.3-style message with token name without PHP native translation [1]' => array( + 'message' => 'Unexpected T_FILE, expecting T_STRING', + 'expected' => 'Unexpected __FILE__ (T_FILE), expecting T_STRING', + ), + 'PHP 5.3-style message with token name without PHP native translation [2]' => array( + 'message' => 'Unexpected T_INC', + 'expected' => 'Unexpected ++ (T_INC)', + ), + 'Message with multiple tokens without 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)', + ), + ); + } +} From 2c1f74733d5f2e79770d97d9408193572ce8d062 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 20 Feb 2022 17:07:02 +0100 Subject: [PATCH 2/2] 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. --- src/Errors/SyntaxError.php | 2 +- tests/Unit/Errors/SyntaxErrorTranslateTokensTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Errors/SyntaxError.php b/src/Errors/SyntaxError.php index 9a4a36e..f13a796 100644 --- a/src/Errors/SyntaxError.php +++ b/src/Errors/SyntaxError.php @@ -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('~(? '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)', + ), ); } }