From 5f7890041c016f2012039becdfc4ad70828700a6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 13 Jun 2022 11:41:09 +0200 Subject: [PATCH] Tokenizer/PHP: bug fix in improved context sensitive keyword support As reported in 3609, the `empty` keyword, as well as the `isset` and `unset` keywords, was not included in the list of context sensitive keyword. This is a regression compared to PHPCS 3.6.2. Fixed now, including unit tests. Note: I've now done a full comparison with the [reserved keyword list as per the PHP manual](https://www.php.net/manual/en/reserved.keywords.php). This should hopefully fix the remaining stranglers. Fixes 3609 --- src/Util/Tokens.php | 3 +++ .../Tokenizer/ContextSensitiveKeywordsTest.inc | 9 ++++++++- .../Tokenizer/ContextSensitiveKeywordsTest.php | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Util/Tokens.php b/src/Util/Tokens.php index 2368a90d3c..1208e4f059 100644 --- a/src/Util/Tokens.php +++ b/src/Util/Tokens.php @@ -687,6 +687,7 @@ final class Tokens T_ECHO => T_ECHO, T_ELSE => T_ELSE, T_ELSEIF => T_ELSEIF, + T_EMPTY => T_EMPTY, T_ENDDECLARE => T_ENDDECLARE, T_ENDFOR => T_ENDFOR, T_ENDFOREACH => T_ENDFOREACH, @@ -712,6 +713,7 @@ final class Tokens T_INSTANCEOF => T_INSTANCEOF, T_INSTEADOF => T_INSTEADOF, T_INTERFACE => T_INTERFACE, + T_ISSET => T_ISSET, T_LIST => T_LIST, T_LOGICAL_AND => T_LOGICAL_AND, T_LOGICAL_OR => T_LOGICAL_OR, @@ -732,6 +734,7 @@ final class Tokens T_THROW => T_THROW, T_TRAIT => T_TRAIT, T_TRY => T_TRY, + T_UNSET => T_UNSET, T_USE => T_USE, T_VAR => T_VAR, T_WHILE => T_WHILE, diff --git a/tests/Core/Tokenizer/ContextSensitiveKeywordsTest.inc b/tests/Core/Tokenizer/ContextSensitiveKeywordsTest.inc index b78062aa55..82fe564382 100644 --- a/tests/Core/Tokenizer/ContextSensitiveKeywordsTest.inc +++ b/tests/Core/Tokenizer/ContextSensitiveKeywordsTest.inc @@ -22,6 +22,7 @@ class ContextSensitiveKeywords const /* testEcho */ ECHO = 'ECHO'; const /* testElse */ ELSE = 'ELSE'; const /* testElseIf */ ELSEIF = 'ELSEIF'; + const /* testEmpty */ EMPTY = 'EMPTY'; const /* testEndDeclare */ ENDDECLARE = 'ENDDECLARE'; const /* testEndFor */ ENDFOR = 'ENDFOR'; const /* testEndForeach */ ENDFOREACH = 'ENDFOREACH'; @@ -47,6 +48,7 @@ class ContextSensitiveKeywords const /* testInstanceOf */ INSTANCEOF = 'INSTANCEOF'; const /* testInsteadOf */ INSTEADOF = 'INSTEADOF'; const /* testInterface */ INTERFACE = 'INTERFACE'; + const /* testIsset */ ISSET = 'ISSET'; const /* testList */ LIST = 'LIST'; const /* testMatch */ MATCH = 'MATCH'; const /* testNamespace */ NAMESPACE = 'NAMESPACE'; @@ -66,6 +68,7 @@ class ContextSensitiveKeywords const /* testThrows */ THROW = 'THROW'; const /* testTrait */ TRAIT = 'TRAIT'; const /* testTry */ TRY = 'TRY'; + const /* testUnset */ UNSET = 'UNSET'; const /* testUse */ USE = 'USE'; const /* testVar */ VAR = 'VAR'; const /* testWhile */ WHILE = 'WHILE'; @@ -121,7 +124,7 @@ $object = /* testNewIsKeyword */ new SomeClass(); $object /* testInstanceOfIsKeyword */ instanceof SomeClass; $copy = /* testCloneIsKeyword */ clone $object; -/* testIfIsKeyword */ if (true): +/* testIfIsKeyword */ if (/* testEmptyIsKeyword */ empty($a)): /* testElseIfIsKeyword */ elseif (false): /* testElseIsKeyword */ else: /* testEndIfIsKeyword */ endif; @@ -170,6 +173,10 @@ die($foo); eval('