From 7ea70d08aa59febd4019a66b90d3daa1f2fe7823 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sat, 11 Jan 2025 16:52:19 +0100 Subject: [PATCH 1/4] fix(ValidClassName): Allow upper case with number --- .../NamingConventions/ValidClassNameSniff.php | 2 +- .../NamingConventions/ValidEnumCaseUnitTest.inc | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php index a7df880c..c151b828 100644 --- a/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php +++ b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php @@ -76,7 +76,7 @@ public function process(File $phpcsFile, $stackPtr) // @todo We could make this more strict to check if there are more than // 2 upper case characters in a row, but not decided yet. // See https://www.drupal.org/project/coder/issues/3497433 - if (strtoupper($name) === $name) { + if (preg_match('|^[A-Z]{3}[A-Z_]*$|', $name) === 1) { $error = '%s name must use UpperCamel naming and not contain multiple upper case letters in a row'; $phpcsFile->addError($error, $stackPtr, 'NoUpperAcronyms', $errorData); } diff --git a/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc b/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc index b6b42807..0270522f 100644 --- a/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc +++ b/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc @@ -11,3 +11,17 @@ enum Test: int { case FourJSONCase = 4; case FiveAndAHorseCorrect = 5; } + +// Those are all ok. +enum FiscalQuarter { + case Q1; + case Q2; + case Q3; + case Q4; +} + +enum Plan { + case A; + case B; + case C; +} From 452693ff2c3797bebfb0ddf955c1d32f8e3d6175 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sat, 11 Jan 2025 16:59:42 +0100 Subject: [PATCH 2/4] simpler regex --- .../Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php index c151b828..72f962ef 100644 --- a/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php +++ b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php @@ -74,9 +74,9 @@ public function process(File $phpcsFile, $stackPtr) // Ensure the name is not all uppercase. // @todo We could make this more strict to check if there are more than - // 2 upper case characters in a row, but not decided yet. + // 2 upper case characters in a row anywhere, but not decided yet. // See https://www.drupal.org/project/coder/issues/3497433 - if (preg_match('|^[A-Z]{3}[A-Z_]*$|', $name) === 1) { + if (preg_match('|^[A-Z]{3}|', $name) === 1) { $error = '%s name must use UpperCamel naming and not contain multiple upper case letters in a row'; $phpcsFile->addError($error, $stackPtr, 'NoUpperAcronyms', $errorData); } From 47bc595eba93c5839fc945356466c98ec418ead6 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sat, 11 Jan 2025 17:08:30 +0100 Subject: [PATCH 3/4] more lenient --- .../Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php | 2 +- tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php index 72f962ef..e1ec417b 100644 --- a/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php +++ b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php @@ -76,7 +76,7 @@ public function process(File $phpcsFile, $stackPtr) // @todo We could make this more strict to check if there are more than // 2 upper case characters in a row anywhere, but not decided yet. // See https://www.drupal.org/project/coder/issues/3497433 - if (preg_match('|^[A-Z]{3}|', $name) === 1) { + if (preg_match('|^[A-Z]{3}[A-Z_0-9]*$|', $name) === 1) { $error = '%s name must use UpperCamel naming and not contain multiple upper case letters in a row'; $phpcsFile->addError($error, $stackPtr, 'NoUpperAcronyms', $errorData); } diff --git a/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc b/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc index 0270522f..d037f5ba 100644 --- a/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc +++ b/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc @@ -10,6 +10,7 @@ enum Test: int { // Upper case parts are allowed for now. case FourJSONCase = 4; case FiveAndAHorseCorrect = 5; + case UIExample = 6; } // Those are all ok. From 58ad2e77e6214346001b58b7bccfb90e92d7ac3c Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sat, 11 Jan 2025 17:12:12 +0100 Subject: [PATCH 4/4] reverse lower case --- .../Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php index e1ec417b..0ff3a569 100644 --- a/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php +++ b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidClassNameSniff.php @@ -76,7 +76,7 @@ public function process(File $phpcsFile, $stackPtr) // @todo We could make this more strict to check if there are more than // 2 upper case characters in a row anywhere, but not decided yet. // See https://www.drupal.org/project/coder/issues/3497433 - if (preg_match('|^[A-Z]{3}[A-Z_0-9]*$|', $name) === 1) { + if (preg_match('|^[A-Z]{3}[^a-z]*$|', $name) === 1) { $error = '%s name must use UpperCamel naming and not contain multiple upper case letters in a row'; $phpcsFile->addError($error, $stackPtr, 'NoUpperAcronyms', $errorData); }