From 56b6785d5dd6eaba77e75478aee59e4866da5c52 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 24 Oct 2024 15:03:47 -0300 Subject: [PATCH 1/3] Generic/CamelCapsFunctionName: improve inline documentation * This sniff only listens to `T_FUNCTION`. It does not listen to `T_FN` or `T_CLOSURE`. So the inline code comments were incorrect. The if conditions are still valid to bail early when live coding, but they do not ever apply to closures or arrow functions (anymore - it was possible in the past though). * The inline comments regarding leading underscores was inaccurate as the code removes all leading underscores, not just the first. --- .../NamingConventions/CamelCapsFunctionNameSniff.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php index ec1cfd0029..ed5bbe4acc 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php @@ -113,7 +113,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $methodName = $phpcsFile->getDeclarationName($stackPtr); if ($methodName === null) { - // Ignore closures. + // Live coding or parse error. Bow out. return; } @@ -150,7 +150,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop return; } - // Ignore first underscore in methods prefixed with "_". + // Ignore leading underscores in the method name. $methodName = ltrim($methodName, '_'); $methodProps = $phpcsFile->getMethodProperties($stackPtr); @@ -189,7 +189,7 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); if ($functionName === null) { - // Ignore closures. + // Live coding or parse error. Bow out. return; } @@ -206,7 +206,7 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) $phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData); } - // Ignore first underscore in functions prefixed with "_". + // Ignore leading underscores in the method name. $functionName = ltrim($functionName, '_'); if (Common::isCamelCaps($functionName, false, true, $this->strict) === false) { From afcb7a2f3bd96b53f85a72e723813ec19afa6028 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 24 Oct 2024 15:11:43 -0300 Subject: [PATCH 2/3] Generic/CamelCapsFunctionName: rename test case file Doing this to be able to create tests with syntax errors on separate files. --- ...nc => CamelCapsFunctionNameUnitTest.1.inc} | 0 .../CamelCapsFunctionNameUnitTest.php | 97 ++++++++++--------- 2 files changed, 51 insertions(+), 46 deletions(-) rename src/Standards/Generic/Tests/NamingConventions/{CamelCapsFunctionNameUnitTest.inc => CamelCapsFunctionNameUnitTest.1.inc} (100%) diff --git a/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.inc b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.1.inc similarity index 100% rename from src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.inc rename to src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.1.inc diff --git a/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.php b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.php index 777d81c711..e854deab89 100644 --- a/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.php +++ b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.php @@ -26,55 +26,60 @@ final class CamelCapsFunctionNameUnitTest 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='') { - $errors = [ - 10 => 1, - 11 => 1, - 12 => 1, - 13 => 1, - 16 => 1, - 17 => 1, - 20 => 1, - 21 => 1, - 24 => 1, - 25 => 1, - 30 => 1, - 31 => 1, - 50 => 1, - 52 => 1, - 53 => 2, - 57 => 1, - 58 => 1, - 59 => 1, - 60 => 1, - 61 => 1, - 62 => 1, - 63 => 1, - 64 => 1, - 65 => 1, - 66 => 1, - 67 => 1, - 68 => 2, - 69 => 1, - 71 => 1, - 72 => 1, - 73 => 2, - 118 => 1, - 144 => 1, - 146 => 1, - 147 => 2, - 158 => 1, - 159 => 1, - 179 => 1, - 180 => 2, - 183 => 1, - 184 => 1, - ]; - - return $errors; + switch ($testFile) { + case 'CamelCapsFunctionNameUnitTest.1.inc': + return[ + 10 => 1, + 11 => 1, + 12 => 1, + 13 => 1, + 16 => 1, + 17 => 1, + 20 => 1, + 21 => 1, + 24 => 1, + 25 => 1, + 30 => 1, + 31 => 1, + 50 => 1, + 52 => 1, + 53 => 2, + 57 => 1, + 58 => 1, + 59 => 1, + 60 => 1, + 61 => 1, + 62 => 1, + 63 => 1, + 64 => 1, + 65 => 1, + 66 => 1, + 67 => 1, + 68 => 2, + 69 => 1, + 71 => 1, + 72 => 1, + 73 => 2, + 118 => 1, + 144 => 1, + 146 => 1, + 147 => 2, + 158 => 1, + 159 => 1, + 179 => 1, + 180 => 2, + 183 => 1, + 184 => 1, + ]; + default: + return []; + }//end switch }//end getErrorList() From d59edc4fd66c6e2c6fdd3901ac0f93146310c87b Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 24 Oct 2024 16:27:23 -0300 Subject: [PATCH 3/3] Generic/CamelCapsFunctionName: improve code coverage and remove unnecessary return The `return;` is unnecessary as the last statement in a method. Test improvements: * Add separate test case files with live coding/parse error tests. * Add tests verifying sand safeguarding that the name of methods in interfaces is also checked. * Add tests covering the public `strict` property which can be toggled from within rulesets. --- .../CamelCapsFunctionNameSniff.php | 1 - .../CamelCapsFunctionNameUnitTest.1.inc | 19 +++++++++++++++++++ .../CamelCapsFunctionNameUnitTest.2.inc | 9 +++++++++ .../CamelCapsFunctionNameUnitTest.3.inc | 7 +++++++ .../CamelCapsFunctionNameUnitTest.php | 3 +++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.2.inc create mode 100644 src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.3.inc diff --git a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php index ed5bbe4acc..d596f17442 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php @@ -168,7 +168,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no'); - return; } else { $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes'); } diff --git a/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.1.inc b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.1.inc index 8441060d27..92368d8c8d 100644 --- a/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.1.inc +++ b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.1.inc @@ -183,3 +183,22 @@ enum Suit: string implements Colorful, CardGame { public function parseMyDSN() {} public function get_some_value() {} } + +interface MyInterface { + public function getSomeValue(); + public function get_some_value(); +} + +class MyClass { + // phpcs:set Generic.NamingConventions.CamelCapsFunctionName strict false + function strictFOrmatDIsabled() {} // Ok. + // phpcs:set Generic.NamingConventions.CamelCapsFunctionName strict true + + function strictFOrmatIsENabled() {} // Not ok. +} + +// phpcs:set Generic.NamingConventions.CamelCapsFunctionName strict false +function strictFOrmatDIsabled() {} // Ok. +// phpcs:set Generic.NamingConventions.CamelCapsFunctionName strict true + +function strictFOrmatIsENabled() {} // Not ok. diff --git a/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.2.inc b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.2.inc new file mode 100644 index 0000000000..bfd1cfe25e --- /dev/null +++ b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.2.inc @@ -0,0 +1,9 @@ + 2, 183 => 1, 184 => 1, + 189 => 1, + 197 => 1, + 204 => 1, ]; default: return [];