Skip to content

Commit 5f78900

Browse files
committed
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
1 parent d8313c6 commit 5f78900

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/Util/Tokens.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ final class Tokens
687687
T_ECHO => T_ECHO,
688688
T_ELSE => T_ELSE,
689689
T_ELSEIF => T_ELSEIF,
690+
T_EMPTY => T_EMPTY,
690691
T_ENDDECLARE => T_ENDDECLARE,
691692
T_ENDFOR => T_ENDFOR,
692693
T_ENDFOREACH => T_ENDFOREACH,
@@ -712,6 +713,7 @@ final class Tokens
712713
T_INSTANCEOF => T_INSTANCEOF,
713714
T_INSTEADOF => T_INSTEADOF,
714715
T_INTERFACE => T_INTERFACE,
716+
T_ISSET => T_ISSET,
715717
T_LIST => T_LIST,
716718
T_LOGICAL_AND => T_LOGICAL_AND,
717719
T_LOGICAL_OR => T_LOGICAL_OR,
@@ -732,6 +734,7 @@ final class Tokens
732734
T_THROW => T_THROW,
733735
T_TRAIT => T_TRAIT,
734736
T_TRY => T_TRY,
737+
T_UNSET => T_UNSET,
735738
T_USE => T_USE,
736739
T_VAR => T_VAR,
737740
T_WHILE => T_WHILE,

tests/Core/Tokenizer/ContextSensitiveKeywordsTest.inc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ContextSensitiveKeywords
2222
const /* testEcho */ ECHO = 'ECHO';
2323
const /* testElse */ ELSE = 'ELSE';
2424
const /* testElseIf */ ELSEIF = 'ELSEIF';
25+
const /* testEmpty */ EMPTY = 'EMPTY';
2526
const /* testEndDeclare */ ENDDECLARE = 'ENDDECLARE';
2627
const /* testEndFor */ ENDFOR = 'ENDFOR';
2728
const /* testEndForeach */ ENDFOREACH = 'ENDFOREACH';
@@ -47,6 +48,7 @@ class ContextSensitiveKeywords
4748
const /* testInstanceOf */ INSTANCEOF = 'INSTANCEOF';
4849
const /* testInsteadOf */ INSTEADOF = 'INSTEADOF';
4950
const /* testInterface */ INTERFACE = 'INTERFACE';
51+
const /* testIsset */ ISSET = 'ISSET';
5052
const /* testList */ LIST = 'LIST';
5153
const /* testMatch */ MATCH = 'MATCH';
5254
const /* testNamespace */ NAMESPACE = 'NAMESPACE';
@@ -66,6 +68,7 @@ class ContextSensitiveKeywords
6668
const /* testThrows */ THROW = 'THROW';
6769
const /* testTrait */ TRAIT = 'TRAIT';
6870
const /* testTry */ TRY = 'TRY';
71+
const /* testUnset */ UNSET = 'UNSET';
6972
const /* testUse */ USE = 'USE';
7073
const /* testVar */ VAR = 'VAR';
7174
const /* testWhile */ WHILE = 'WHILE';
@@ -121,7 +124,7 @@ $object = /* testNewIsKeyword */ new SomeClass();
121124
$object /* testInstanceOfIsKeyword */ instanceof SomeClass;
122125
$copy = /* testCloneIsKeyword */ clone $object;
123126

124-
/* testIfIsKeyword */ if (true):
127+
/* testIfIsKeyword */ if (/* testEmptyIsKeyword */ empty($a)):
125128
/* testElseIfIsKeyword */ elseif (false):
126129
/* testElseIsKeyword */ else:
127130
/* testEndIfIsKeyword */ endif;
@@ -170,6 +173,10 @@ die($foo);
170173
eval('<?php echo 5;');
171174
/* testExitIsKeyword */
172175
exit;
176+
/* testIssetIsKeyword */
177+
$a = isset($a);
178+
/* testUnsetIsKeyword */
179+
unset($a);
173180

174181
/* testIncludeIsKeyword */
175182
include 'file.php';

tests/Core/Tokenizer/ContextSensitiveKeywordsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function dataStrings()
6565
['/* testEcho */'],
6666
['/* testElse */'],
6767
['/* testElseIf */'],
68+
['/* testEmpty */'],
6869
['/* testEndDeclare */'],
6970
['/* testEndFor */'],
7071
['/* testEndForeach */'],
@@ -90,6 +91,7 @@ public function dataStrings()
9091
['/* testInstanceOf */'],
9192
['/* testInsteadOf */'],
9293
['/* testInterface */'],
94+
['/* testIsset */'],
9395
['/* testList */'],
9496
['/* testMatch */'],
9597
['/* testNamespace */'],
@@ -109,6 +111,7 @@ public function dataStrings()
109111
['/* testThrows */'],
110112
['/* testTrait */'],
111113
['/* testTry */'],
114+
['/* testUnset */'],
112115
['/* testUse */'],
113116
['/* testVar */'],
114117
['/* testWhile */'],
@@ -278,6 +281,10 @@ public function dataKeywords()
278281
'/* testIfIsKeyword */',
279282
'T_IF',
280283
],
284+
[
285+
'/* testEmptyIsKeyword */',
286+
'T_EMPTY',
287+
],
281288
[
282289
'/* testElseIfIsKeyword */',
283290
'T_ELSEIF',
@@ -388,6 +395,14 @@ public function dataKeywords()
388395
'/* testExitIsKeyword */',
389396
'T_EXIT',
390397
],
398+
[
399+
'/* testIssetIsKeyword */',
400+
'T_ISSET',
401+
],
402+
[
403+
'/* testUnsetIsKeyword */',
404+
'T_UNSET',
405+
],
391406

392407
[
393408
'/* testIncludeIsKeyword */',

0 commit comments

Comments
 (0)