From 338469ee322b4e09d213bc617e65ffebb2e74c70 Mon Sep 17 00:00:00 2001 From: Klaus Purer <klaus.purer@protonmail.ch> Date: Sat, 4 Jan 2025 12:27:25 +0100 Subject: [PATCH] feat(ValidEnumCase): Add UpperCamel name sniff for enum cases (#3492126) --- .../NamingConventions/ValidEnumCaseSniff.php | 35 ++++++++++++++ .../ValidEnumCaseUnitTest.inc | 8 ++++ .../ValidEnumCaseUnitTest.php | 48 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 coder_sniffer/Drupal/Sniffs/NamingConventions/ValidEnumCaseSniff.php create mode 100644 tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc create mode 100644 tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.php diff --git a/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidEnumCaseSniff.php b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidEnumCaseSniff.php new file mode 100644 index 00000000..7eca94a7 --- /dev/null +++ b/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidEnumCaseSniff.php @@ -0,0 +1,35 @@ +<?php +/** + * Enum case upper camel case sniff. + * + * @category PHP + * @package PHP_CodeSniffer + * @link http://pear.php.net/package/PHP_CodeSniffer + */ + +namespace Drupal\Sniffs\NamingConventions; + +/** + * Checks that enum case definitions are in upper camel case. + * + * @category PHP + * @package PHP_CodeSniffer + * @link http://pear.php.net/package/PHP_CodeSniffer + */ +class ValidEnumCaseSniff extends ValidClassNameSniff +{ + + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array<int|string> + */ + public function register() + { + return [T_ENUM_CASE]; + + }//end register() + + +}//end class diff --git a/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc b/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc new file mode 100644 index 00000000..d1819d99 --- /dev/null +++ b/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc @@ -0,0 +1,8 @@ +<?php + +enum Test: int { + // Must not start with lower case. + case one = 1; + // Must not contain underscores. + case TWO_TEST = 2; +} diff --git a/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.php b/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.php new file mode 100644 index 00000000..eea3216c --- /dev/null +++ b/tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.php @@ -0,0 +1,48 @@ +<?php + +namespace Drupal\Test\NamingConventions; + +use Drupal\Test\CoderSniffUnitTest; + +class ValidEnumCaseUnitTest extends CoderSniffUnitTest +{ + + + /** + * Returns the lines where errors should occur. + * + * 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 file being tested. + * + * @return array<int, int> + */ + protected function getErrorList(string $testFile): array + { + return [ + 5 => 1, + 7 => 1, + ]; + + }//end getErrorList() + + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @param string $testFile The name of the file being tested. + * + * @return array<int, int> + */ + protected function getWarningList(string $testFile): array + { + return []; + + }//end getWarningList() + + +}//end class -- GitLab