From 59905dbc335b330ea874650588f598a65d20df9c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 17 Aug 2024 05:36:50 +0200 Subject: [PATCH] Generic/LowerCaseKeyword: remove some redundant code The `Tokens::$contextSensitiveKeywords` token array was introduced in PHPCS 3.7.0 via PR squizlabs/PHP_CodeSniffer 3484. PR squizlabs/PHP_CodeSniffer 3574 build onto that by removing the bulk of the target tokens from the `register()` method for the Generic/LowerCaseKeyword sniff in favour of using the predefined `Tokens::$contextSensitiveKeywords` token array. The `T_EMPTY`, `T_EVAL`, `T_ISSET` and `T_UNSET` tokens were initially missing from the `Tokens::$contextSensitiveKeywords` array. This was fixed in PHPCS 3.7.1 via PRs squizlabs/PHP_CodeSniffer 3608 and squizlabs/PHP_CodeSniffer 3610. This means those tokens no longer need to be explicitly added as targets for the Generic/LowerCaseKeyword sniff as they are now (and have been since PHPCS 3.7.1) inherited via the `Tokens::$contextSensitiveKeywords` token array. This commit removes the redundancy, but also adds tests to safeguard that those keywords will still be checked by the sniff. --- src/Standards/Generic/Sniffs/PHP/LowerCaseKeywordSniff.php | 4 ---- src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc | 5 +++++ .../Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc.fixed | 5 +++++ src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.php | 2 ++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Standards/Generic/Sniffs/PHP/LowerCaseKeywordSniff.php b/src/Standards/Generic/Sniffs/PHP/LowerCaseKeywordSniff.php index 4edcc5d92b..76886471d7 100644 --- a/src/Standards/Generic/Sniffs/PHP/LowerCaseKeywordSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/LowerCaseKeywordSniff.php @@ -29,14 +29,10 @@ public function register() $targets += [ T_ANON_CLASS => T_ANON_CLASS, T_CLOSURE => T_CLOSURE, - T_EMPTY => T_EMPTY, T_ENUM_CASE => T_ENUM_CASE, - T_EVAL => T_EVAL, - T_ISSET => T_ISSET, T_MATCH_DEFAULT => T_MATCH_DEFAULT, T_PARENT => T_PARENT, T_SELF => T_SELF, - T_UNSET => T_UNSET, ]; return $targets; diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc b/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc index 429ddebddf..a50ac22853 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc +++ b/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc @@ -48,5 +48,10 @@ new Class {}; new clasS extends stdClass {}; new class {}; +if (isset($a) && !empty($a)) { unset($a); } +if (ISSET($a) && !Empty($a)) { UnSeT($a); } +eval('foo'); +eVaL('foo'); + __HALT_COMPILER(); // An exception due to phar support. function diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc.fixed b/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc.fixed index 2d68e55a2c..5e15765146 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc.fixed @@ -48,5 +48,10 @@ new class {}; new class extends stdClass {}; new class {}; +if (isset($a) && !empty($a)) { unset($a); } +if (isset($a) && !empty($a)) { unset($a); } +eval('foo'); +eval('foo'); + __HALT_COMPILER(); // An exception due to phar support. function diff --git a/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.php b/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.php index c2f8831e47..6d337504f4 100644 --- a/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.php +++ b/src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.php @@ -50,6 +50,8 @@ public function getErrorList() 44 => 1, 47 => 1, 48 => 1, + 52 => 3, + 54 => 1, ]; }//end getErrorList()