From d2545e10cb932626f38d4ac9a4f01283cfb8a629 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 5 Nov 2024 20:10:43 -0300 Subject: [PATCH 01/12] Generic/UpperCaseConstantName: handle unconventional spacing and comments after `define(` This commit fixes false negatives in the sniff when handling unconventional spacing and comments after `define(`. When checking for the constant name after the opening parenthesis, the sniff would incorrectly exclude only whitespaces while comments can also be placed between the opening parenthesis and the constant name. Looking for the constant name by getting the next non-empty token after the opening parenthesis fixes this problem. The added tests also include comments between `define` and the opening parenthesis but that was already handled correctly by the sniff. --- .../NamingConventions/UpperCaseConstantNameSniff.php | 4 ++-- .../UpperCaseConstantNameUnitTest.inc | 10 ++++++++++ .../UpperCaseConstantNameUnitTest.php | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index f62cfe94bd..5be892e880 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -103,8 +103,8 @@ public function process(File $phpcsFile, $stackPtr) return; } - // The next non-whitespace token must be the constant name. - $constPtr = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true); + // The next non-empty token must be the constant name. + $constPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true); if ($tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) { return; } diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc index 20bbf90e26..60bb415cf9 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc @@ -41,3 +41,13 @@ class TypedConstants { const FALSE false = false; // Yes, false can be used as a constant name, don't ask. const array ARRAY = array(); // Same goes for array. } + +define /* comment */ ( /* comment */ 'CommentsInUnconventionalPlaces', 'value' ); + +define +// comment +( + // phpcs:ignore Stnd.Cat.SniffName -- for reasons. + 'CommentsInUnconventionalPlaces', + 'value' +); diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php index afe3625589..4394951012 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php @@ -40,6 +40,8 @@ public function getErrorList() 30 => 1, 40 => 1, 41 => 1, + 45 => 1, + 47 => 1, ]; }//end getErrorList() From 712eb98b393a2cd67eef7d295f1d5325f8d57f26 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Wed, 6 Nov 2024 11:33:23 -0300 Subject: [PATCH 02/12] Generic/UpperCaseConstantName: handle unconventional spacing and comments before `define` This commit fixes false positives in the sniff when handling unconventional spacing and comments before `define`. When checking to see if the `define` found is not a method call, the sniff would incorrectly exclude only whitespaces. But comments can also be placed between `define` and one of the tokens the sniff looks for (T_OBJECT_OPERATOR, T_DOUBLE_COLON or T_NULLSAFE_OBJECT_OPERATOR). Looking for the first non-empty token before `define` fixes this problem. --- .../Sniffs/NamingConventions/UpperCaseConstantNameSniff.php | 2 +- .../NamingConventions/UpperCaseConstantNameUnitTest.inc | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index 5be892e880..a2c22ecb73 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -88,7 +88,7 @@ public function process(File $phpcsFile, $stackPtr) } // Make sure this is not a method call. - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); if ($tokens[$prev]['code'] === T_OBJECT_OPERATOR || $tokens[$prev]['code'] === T_DOUBLE_COLON || $tokens[$prev]['code'] === T_NULLSAFE_OBJECT_OPERATOR diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc index 60bb415cf9..6531b8d49a 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc @@ -51,3 +51,8 @@ define 'CommentsInUnconventionalPlaces', 'value' ); + +$foo-> /* comment */ define('bar'); +$foo?-> +// phpcs:ignore Stnd.Cat.SniffName -- for reasons. +define('bar'); From 2c77bad6f676beac615b3b02997827b356037eb7 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Wed, 6 Nov 2024 12:07:58 -0300 Subject: [PATCH 03/12] Generic/UpperCaseConstantName: false positives when constant name is "DEFINE" This commit fixes false positives when the sniff encounters a constant named "DEFINE". When looking for calls to `define()`, the code was checking only if there was a non-empty token after `define`. Instead, it should check if the next non-empty token after `define` is `T_OPEN_PARENTHESIS`. --- .../Sniffs/NamingConventions/UpperCaseConstantNameSniff.php | 2 +- .../Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index a2c22ecb73..3b59343cd0 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -99,7 +99,7 @@ public function process(File $phpcsFile, $stackPtr) // If the next non-whitespace token after this token // is not an opening parenthesis then it is not a function call. $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); - if ($openBracket === false) { + if ($openBracket === false || $tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { return; } diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc index 6531b8d49a..7ca6196c78 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc @@ -56,3 +56,5 @@ $foo-> /* comment */ define('bar'); $foo?-> // phpcs:ignore Stnd.Cat.SniffName -- for reasons. define('bar'); + +const DEFINE = 'value'; From e053795915c3797e09841e8ce65dc56e52e3da96 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Wed, 6 Nov 2024 12:07:58 -0300 Subject: [PATCH 04/12] Generic/UpperCaseConstantName: fix attributes false positive This commit fixes false positives in the sniff when handling attributes called `define`. Before this change the sniff was not taking into account that `define` is a valid name for an attribute, and it would incorrectly handle these cases as calls to the `define()` function. --- .../NamingConventions/UpperCaseConstantNameSniff.php | 5 +++++ .../NamingConventions/UpperCaseConstantNameUnitTest.inc | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index 3b59343cd0..6a0e39b49f 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -96,6 +96,11 @@ public function process(File $phpcsFile, $stackPtr) return; } + // Make sure this is not an attribute. + if (empty($tokens[$stackPtr]['nested_attributes']) === false) { + return; + } + // If the next non-whitespace token after this token // is not an opening parenthesis then it is not a function call. $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc index 7ca6196c78..1680676fb6 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc @@ -58,3 +58,12 @@ $foo?-> define('bar'); const DEFINE = 'value'; + +#[Define('some param')] +class MyClass {} + +#[ + AttributeA, + define('some param') +] +class MyClass {} From db558c94cc74e93b87d650d8f7ad0d8f4ead0ebe Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 5 Nov 2024 20:40:50 -0300 Subject: [PATCH 05/12] Generic/UpperCaseConstantName: minor improvement to the error message and metrics This commit implements a minor improvement to the error message and metric recording when handling calls to `define()`. Now the line of the error or the line recorded in the metric will be the line of the constant name. Before it was the line of the T_DEFINE token. --- .../NamingConventions/UpperCaseConstantNameSniff.php | 8 ++++---- .../NamingConventions/UpperCaseConstantNameUnitTest.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index 6a0e39b49f..f129aa6ef2 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -133,9 +133,9 @@ public function process(File $phpcsFile, $stackPtr) if (strtoupper($constName) !== $constName) { if (strtolower($constName) === $constName) { - $phpcsFile->recordMetric($stackPtr, 'Constant name case', 'lower'); + $phpcsFile->recordMetric($constPtr, 'Constant name case', 'lower'); } else { - $phpcsFile->recordMetric($stackPtr, 'Constant name case', 'mixed'); + $phpcsFile->recordMetric($constPtr, 'Constant name case', 'mixed'); } $error = 'Constants must be uppercase; expected %s but found %s'; @@ -143,9 +143,9 @@ public function process(File $phpcsFile, $stackPtr) $prefix.strtoupper($constName), $prefix.$constName, ]; - $phpcsFile->addError($error, $stackPtr, 'ConstantNotUpperCase', $data); + $phpcsFile->addError($error, $constPtr, 'ConstantNotUpperCase', $data); } else { - $phpcsFile->recordMetric($stackPtr, 'Constant name case', 'upper'); + $phpcsFile->recordMetric($constPtr, 'Constant name case', 'upper'); } }//end process() diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php index 4394951012..797627ae7f 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php @@ -41,7 +41,7 @@ public function getErrorList() 40 => 1, 41 => 1, 45 => 1, - 47 => 1, + 51 => 1, ]; }//end getErrorList() From 3b6db7257b30a166058ca75d4da5f38c17e6ffda Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 11 Nov 2024 11:43:44 -0300 Subject: [PATCH 06/12] Generic/UpperCaseConstantName: remove special case for double-colon In early versions of this sniff, constant names were checked not only when defined, but also when used. At that time, code was added to handle the dynamic retrieval of class constants in calls to `constant()` by checking if the constant name contained a double-colon (see https://github.com/squizlabs/PHP_CodeSniffer/commit/610b0cc3f1584ccd050a4970ab80843832c46d73 and https://pear.php.net/bugs/bug.php?id=18670). If so, the sniff would enforce upper case only for the substring after the double-colon. Later, another commit removed support for constant usage (https://github.com/squizlabs/PHP_CodeSniffer/commit/4af13118b1fedd89faadd78b9bc and https://pear.php.net/bugs/bug.php?id=20090). It seems to me that the code that is removed in this commit, should have been removed when support for constant usage was removed. The removed code is only triggered when a constant is defined using `define()`. As far as I can tell, over time, PHP changed how it behaves when a double-colon is passed as part of the first parameter of `define()`. However, never in a way that justifies special treatment in the context of this sniff (https://3v4l.org/erZcK). At least not since PHP 5.0 (I'm assuming it is not needed to check PHP 4). --- .../NamingConventions/UpperCaseConstantNameSniff.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index f129aa6ef2..c06695c105 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -115,14 +115,7 @@ public function process(File $phpcsFile, $stackPtr) } $constName = $tokens[$constPtr]['content']; - - // Check for constants like self::CONSTANT. - $prefix = ''; - $splitPos = strpos($constName, '::'); - if ($splitPos !== false) { - $prefix = substr($constName, 0, ($splitPos + 2)); - $constName = substr($constName, ($splitPos + 2)); - } + $prefix = ''; // Strip namespace from constant like /foo/bar/CONSTANT. $splitPos = strrpos($constName, '\\'); From 59b86fce025f53d6c556aff96dd449de77ae96f2 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 5 Nov 2024 08:27:27 -0300 Subject: [PATCH 07/12] Generic/UpperCaseConstantName: rename test case file Doing this to be able to create tests with syntax errors on separate files. --- ...nc => UpperCaseConstantNameUnitTest.1.inc} | 0 .../UpperCaseConstantNameUnitTest.php | 35 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) rename src/Standards/Generic/Tests/NamingConventions/{UpperCaseConstantNameUnitTest.inc => UpperCaseConstantNameUnitTest.1.inc} (100%) diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc similarity index 100% rename from src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.inc rename to src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php index 797627ae7f..aafe62a56b 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php @@ -26,23 +26,30 @@ final class UpperCaseConstantNameUnitTest extends AbstractSniffUnitTest * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * + * @param string $testFile The name of the test file to process. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 8 => 1, - 10 => 1, - 12 => 1, - 14 => 1, - 19 => 1, - 28 => 1, - 30 => 1, - 40 => 1, - 41 => 1, - 45 => 1, - 51 => 1, - ]; + switch ($testFile) { + case 'UpperCaseConstantNameUnitTest.1.inc': + return [ + 8 => 1, + 10 => 1, + 12 => 1, + 14 => 1, + 19 => 1, + 28 => 1, + 30 => 1, + 40 => 1, + 41 => 1, + 45 => 1, + 51 => 1, + ]; + default: + return []; + } }//end getErrorList() From 1352ce8fd81be64373e9189e7eea8918c826f07f Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 8 Nov 2024 11:22:04 -0300 Subject: [PATCH 08/12] Generic/UpperCaseConstantName: `findNext()` may return `false` This commit takes into account that `findNext()` may return `false` and properly handles this case in the modified `if` condition. There were no issues without this extra check, as when `$constPtr` is `false`, `$tokens[$constPrt]` evaluates to the first token in the stack and the first token can never be `T_CONSTANT_ENCAPSED_STRING`, so the sniff would bail early anyway. --- .../NamingConventions/UpperCaseConstantNameSniff.php | 2 +- .../NamingConventions/UpperCaseConstantNameUnitTest.2.inc | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.2.inc diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index c06695c105..8461a1abaf 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -110,7 +110,7 @@ public function process(File $phpcsFile, $stackPtr) // The next non-empty token must be the constant name. $constPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true); - if ($tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) { + if ($constPtr === false || $tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) { return; } diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.2.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.2.inc new file mode 100644 index 0000000000..4136912dc9 --- /dev/null +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.2.inc @@ -0,0 +1,7 @@ + Date: Wed, 6 Nov 2024 10:51:23 -0300 Subject: [PATCH 09/12] Generic/UpperCaseConstantName: improve code coverage Includes updating a sniff code comment to better describe what it does. Some of the cases described were not covered by tests before the changes in this commit. --- .../UpperCaseConstantNameSniff.php | 5 ++++- .../UpperCaseConstantNameUnitTest.1.inc | 19 ++++++++++++++++--- .../UpperCaseConstantNameUnitTest.3.inc | 7 +++++++ .../UpperCaseConstantNameUnitTest.4.inc | 7 +++++++ .../UpperCaseConstantNameUnitTest.php | 2 ++ 5 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.3.inc create mode 100644 src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.4.inc diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index 8461a1abaf..ad4d936b96 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -108,7 +108,10 @@ public function process(File $phpcsFile, $stackPtr) return; } - // The next non-empty token must be the constant name. + // Bow out if next non-empty token after the opening parenthesis is not a string (the + // constant name). This could happen when live coding, if the constant is a variable or an + // expression, or if handling a first-class callable or a function definition outside the + // global scope. $constPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true); if ($constPtr === false || $tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) { return; diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc index 1680676fb6..cb60d04986 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc @@ -5,7 +5,7 @@ namespace foo\bar; namespace bar\foo\baz; define('VALID_NAME', true); -define('invalidName', true); +DEFINE('invalidName', true); define("VALID_NAME", true); define("invalidName", true); define('bar\foo\baz\VALID_NAME_WITH_NAMESPACE', true); @@ -37,9 +37,9 @@ class TypedConstants { const MISSING_VALUE; // Parse error. const MyClass MYCONST = new MyClass; const int VALID_NAME = 0; - const INT invalid_name = 0; + final public const INT invalid_name = 0; const FALSE false = false; // Yes, false can be used as a constant name, don't ask. - const array ARRAY = array(); // Same goes for array. + final protected const array ARRAY = array(); // Same goes for array. } define /* comment */ ( /* comment */ 'CommentsInUnconventionalPlaces', 'value' ); @@ -67,3 +67,16 @@ class MyClass {} define('some param') ] class MyClass {} + +const MixedCase = 1; + +define('lower_case_name', 'value'); +define($var, 'sniff should bow out'); +define(constantName(), 'sniff should bow out'); +define($obj->constantName(), 'sniff should bow out'); +define(MyClass::constantName(), 'sniff should bow out'); +define(condition() ? 'name1' : 'name2', 'sniff should bow out'); + +// Valid if outside the global namespace. Sniff should bow out. +function define($param) {} +$callable = define(...); diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.3.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.3.inc new file mode 100644 index 0000000000..5808142144 --- /dev/null +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.3.inc @@ -0,0 +1,7 @@ + 1, 45 => 1, 51 => 1, + 71 => 1, + 73 => 1, ]; default: return []; From 4f42d3ca365e16dd431f11fc690612f5ed999c6f Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 26 Nov 2024 15:33:41 -0300 Subject: [PATCH 10/12] Improve tests based on feedback from PR review --- .../UpperCaseConstantNameUnitTest.1.inc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc index cb60d04986..93a8835c01 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc @@ -77,6 +77,13 @@ define($obj->constantName(), 'sniff should bow out'); define(MyClass::constantName(), 'sniff should bow out'); define(condition() ? 'name1' : 'name2', 'sniff should bow out'); +$callable = define(...); + // Valid if outside the global namespace. Sniff should bow out. function define($param) {} -$callable = define(...); + +class MyClass { + public function define($param) {} +} + +$a = ($cond) ? DEFINE : SOMETHING_ELSE; From e861eaf34c6b1958aeee4a22633744467d822e66 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 26 Nov 2024 15:44:11 -0300 Subject: [PATCH 11/12] Generic/UpperCaseConstantName: fix false positive for class instantiation This commit fixes a false positive that would trigger an error when this sniff encountered the instantiation of a class named `Define`. --- .../Sniffs/NamingConventions/UpperCaseConstantNameSniff.php | 3 ++- .../NamingConventions/UpperCaseConstantNameUnitTest.1.inc | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php index ad4d936b96..259fa2963d 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php @@ -87,11 +87,12 @@ public function process(File $phpcsFile, $stackPtr) return; } - // Make sure this is not a method call. + // Make sure this is not a method call or class instantiation. $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); if ($tokens[$prev]['code'] === T_OBJECT_OPERATOR || $tokens[$prev]['code'] === T_DOUBLE_COLON || $tokens[$prev]['code'] === T_NULLSAFE_OBJECT_OPERATOR + || $tokens[$prev]['code'] === T_NEW ) { return; } diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc index 93a8835c01..7c1dc41a04 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc @@ -87,3 +87,5 @@ class MyClass { } $a = ($cond) ? DEFINE : SOMETHING_ELSE; + +$object = new Define('value'); From af0b0f59fefab83da1f1164bd823e3b508ec457e Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 26 Nov 2024 15:48:31 -0300 Subject: [PATCH 12/12] Generic/UpperCaseConstantName: move parse error test to its own file --- .../UpperCaseConstantNameUnitTest.1.inc | 2 +- .../UpperCaseConstantNameUnitTest.5.inc | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.5.inc diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc index 7c1dc41a04..c5af2349cb 100644 --- a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.1.inc @@ -34,7 +34,7 @@ $foo->getBar()?->define('foo'); // PHP 8.3 introduces typed constants. class TypedConstants { - const MISSING_VALUE; // Parse error. + const MyClass MYCONST = new MyClass; const int VALID_NAME = 0; final public const INT invalid_name = 0; diff --git a/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.5.inc b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.5.inc new file mode 100644 index 0000000000..96e36a3b1a --- /dev/null +++ b/src/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.5.inc @@ -0,0 +1,9 @@ +