From 0066452243626fd9260c1d6f516966bacec6bc5a Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 24 Oct 2024 15:03:47 -0300 Subject: [PATCH 1/5] Generic/CamelCapsFunctionName: remove two incorrect code comments This sniff only listens to `T_FUNCTION`. It does not listen to `T_FN` or `T_CLOSURE`. So the removed code comments are incorrect. The if conditions are still valid to bail early when live coding, but it does not ever apply to closures or arrow functions. --- .../Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php index ec1cfd0029..ff657cbae1 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php @@ -113,7 +113,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $methodName = $phpcsFile->getDeclarationName($stackPtr); if ($methodName === null) { - // Ignore closures. return; } @@ -189,7 +188,6 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); if ($functionName === null) { - // Ignore closures. return; } From 55c6faff7fa67396bf065b010064115245fff852 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 24 Oct 2024 15:41:43 -0300 Subject: [PATCH 2/5] Generic/CamelCapsFunctionName: update code comment The original inline comment was inaccurate as code removes all leading underscores and not just the first. --- .../Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php index ff657cbae1..9d1405c9e4 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php @@ -149,7 +149,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); From 5fb0769d4723b3daf00b772a6f1fe16cec0156fe Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 24 Oct 2024 16:27:23 -0300 Subject: [PATCH 3/5] Generic/CamelCapsFunctionName: remove unnecessary return `return;` is unnecessary as the last statement in a method. --- .../Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php index 9d1405c9e4..cb633c7066 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php @@ -167,7 +167,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no'); - return; } else { $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes'); } From 2c6631827daed566d95edda299e2125311e6ac1a Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 24 Oct 2024 15:11:43 -0300 Subject: [PATCH 4/5] 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 9ea58ef0eb5e9758dc4dec903d733a5ea1a6cc96 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 24 Oct 2024 15:15:42 -0300 Subject: [PATCH 5/5] Generic/CamelCapsFunctionName: improve code coverage --- .../CamelCapsFunctionNameUnitTest.1.inc | 19 +++++++++++++++++++ .../CamelCapsFunctionNameUnitTest.2.inc | 9 +++++++++ .../CamelCapsFunctionNameUnitTest.3.inc | 7 +++++++ .../CamelCapsFunctionNameUnitTest.php | 3 +++ 4 files changed, 38 insertions(+) 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/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.1.inc b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.1.inc index 8441060d27..ce045b6426 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 { + function getSomeValue(); // Ok. + function get_some_value(); // Not ok. +} + +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 [];