diff --git a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php index 40e3197c80..917db2069f 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php @@ -110,14 +110,14 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop // Is this a magic method. i.e., is prefixed with "__" ? if (preg_match('|^__[^_]|', $methodName) !== 0) { $magicPart = strtolower(substr($methodName, 2)); - if (isset($this->magicMethods[$magicPart]) === false - && isset($this->methodsDoubleUnderscore[$magicPart]) === false + if (isset($this->magicMethods[$magicPart]) === true + || isset($this->methodsDoubleUnderscore[$magicPart]) === true ) { - $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; - $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData); + return; } - return; + $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; + $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData); } // PHP4 constructors are allowed to break our rules. @@ -178,12 +178,12 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) // Is this a magic function. i.e., it is prefixed with "__". if (preg_match('|^__[^_]|', $functionName) !== 0) { $magicPart = strtolower(substr($functionName, 2)); - if (isset($this->magicFunctions[$magicPart]) === false) { - $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; - $phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData); + if (isset($this->magicFunctions[$magicPart]) === true) { + return; } - return; + $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; + $phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData); } // Ignore first underscore in functions prefixed with "_". diff --git a/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.php b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.php index e0de85de15..e260879237 100644 --- a/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.php +++ b/src/Standards/Generic/Tests/NamingConventions/CamelCapsFunctionNameUnitTest.php @@ -40,7 +40,7 @@ public function getErrorList() 31 => 1, 50 => 1, 52 => 1, - 53 => 1, + 53 => 2, 57 => 1, 58 => 1, 59 => 1, @@ -52,16 +52,16 @@ public function getErrorList() 65 => 1, 66 => 1, 67 => 1, - 68 => 1, + 68 => 2, 69 => 1, 71 => 1, 72 => 1, - 73 => 1, + 73 => 2, 74 => 1, 118 => 1, 144 => 1, 146 => 1, - 147 => 1, + 147 => 2, ]; return $errors; diff --git a/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php index 318e3dea3e..268d0f61c2 100644 --- a/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -82,12 +82,12 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop // Is this a magic method. i.e., is prefixed with "__" ? if (preg_match('|^__[^_]|', $methodName) !== 0) { $magicPart = strtolower(substr($methodName, 2)); - if (isset($this->magicMethods[$magicPart]) === false) { - $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; - $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData); + if (isset($this->magicMethods[$magicPart]) === true) { + return; } - return; + $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; + $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData); } // PHP4 constructors are allowed to break our rules. @@ -116,7 +116,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $error = 'Private method name "%s" must be prefixed with an underscore'; $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData); $phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'no'); - return; } else { $phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'yes'); } @@ -130,20 +129,11 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $errorData[0], ]; $phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data); - return; } - // If the scope was specified on the method, then the method must be - // camel caps and an underscore should be checked for. If it wasn't - // specified, treat it like a public method and remove the underscore - // prefix if there is one because we cant determine if it is private or - // public. - $testMethodName = $methodName; - if ($scopeSpecified === false && $methodName{0} === '_') { - $testMethodName = substr($methodName, 1); - } + $testMethodName = ltrim($methodName, '_'); - if (Common::isCamelCaps($testMethodName, false, $isPublic, false) === false) { + if (Common::isCamelCaps($testMethodName, false, true, false) === false) { if ($scopeSpecified === true) { $error = '%s method name "%s" is not in camel caps format'; $data = [ @@ -155,8 +145,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $error = 'Method name "%s" is not in camel caps format'; $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); } - - return; } }//end processTokenWithinScope() @@ -189,12 +177,12 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) // Is this a magic function. i.e., it is prefixed with "__". if (preg_match('|^__[^_]|', $functionName) !== 0) { $magicPart = strtolower(substr($functionName, 2)); - if (isset($this->magicFunctions[$magicPart]) === false) { - $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; - $phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData); + if (isset($this->magicFunctions[$magicPart]) === true) { + return; } - return; + $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; + $phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData); } // Function names can be in two parts; the package name and @@ -216,13 +204,11 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) if ($functionName{0} === '_') { $error = 'Function name "%s" is invalid; only private methods should be prefixed with an underscore'; $phpcsFile->addError($error, $stackPtr, 'FunctionUnderscore', $errorData); - return; } if ($functionName{0} !== strtoupper($functionName{0})) { $error = 'Function name "%s" is prefixed with a package name but does not begin with a capital letter'; $phpcsFile->addError($error, $stackPtr, 'FunctionNoCapital', $errorData); - return; } } diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.php b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.php index f5b20e6855..af06c13d22 100644 --- a/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.php +++ b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.php @@ -32,26 +32,26 @@ public function getErrorList() 14 => 1, 15 => 1, 16 => 1, - 17 => 1, - 18 => 1, - 19 => 1, - 20 => 1, + 17 => 2, + 18 => 2, + 19 => 2, + 20 => 2, 24 => 1, 25 => 1, 26 => 1, 27 => 1, 28 => 1, 29 => 1, - 30 => 1, - 31 => 1, - 32 => 1, - 33 => 1, + 30 => 2, + 31 => 2, + 32 => 2, + 33 => 2, 35 => 1, 36 => 1, - 37 => 1, - 38 => 1, - 39 => 1, - 40 => 1, + 37 => 2, + 38 => 2, + 39 => 2, + 40 => 2, 43 => 1, 44 => 1, 45 => 1, @@ -70,26 +70,26 @@ public function getErrorList() 70 => 1, 71 => 1, 72 => 1, - 73 => 1, - 74 => 1, - 75 => 1, - 76 => 1, + 73 => 2, + 74 => 2, + 75 => 2, + 76 => 2, 80 => 1, 81 => 1, 82 => 1, 83 => 1, 84 => 1, 85 => 1, - 86 => 1, - 87 => 1, - 88 => 1, - 89 => 1, + 86 => 2, + 87 => 2, + 88 => 2, + 89 => 2, 91 => 1, 92 => 1, - 93 => 1, - 94 => 1, - 95 => 1, - 96 => 1, + 93 => 2, + 94 => 2, + 95 => 2, + 96 => 2, 99 => 1, 100 => 1, 101 => 1, @@ -108,13 +108,13 @@ public function getErrorList() 124 => 1, 125 => 1, 126 => 1, - 127 => 1, - 128 => 1, - 129 => 1, - 130 => 1, + 127 => 2, + 128 => 2, + 129 => 2, + 130 => 2, 149 => 1, 151 => 1, - 152 => 1, + 152 => 2, 155 => 1, 156 => 1, 157 => 1, @@ -126,18 +126,18 @@ public function getErrorList() 163 => 1, 164 => 1, 165 => 1, - 166 => 1, + 166 => 3, 167 => 1, 169 => 1, 170 => 1, - 171 => 1, - 173 => 1, + 171 => 3, + 173 => 2, 174 => 1, 175 => 1, 207 => 1, 227 => 1, 229 => 1, - 230 => 1, + 230 => 2, ]; }//end getErrorList() diff --git a/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php index 3f8be67f8d..537d7d6680 100644 --- a/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -39,7 +39,8 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) if (preg_match('|^__[^_]|', $functionName) !== 0) { $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; $phpcsFile->addError($error, $stackPtr, 'DoubleUnderscore', $errorData); - return; + + $functionName = ltrim($functionName, '_'); } if (Common::isCamelCaps($functionName, false, true, false) === false) { diff --git a/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php b/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php index e5622af0e2..42cc8cd1af 100644 --- a/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -139,17 +139,18 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) $errorData[0], ]; $phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data); - return; } } else { if (substr($varName, 0, 1) !== '_') { $error = 'Private member variable "%s" must contain a leading underscore'; $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData); - return; } } - if (Common::isCamelCaps($varName, false, $public, false) === false) { + // Remove a potential underscore prefix for testing CamelCaps. + $varName = ltrim($varName, '_'); + + if (Common::isCamelCaps($varName, false, true, false) === false) { $error = 'Member variable "%s" is not in valid camel caps format'; $phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $errorData); } diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.php b/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.php index 9fd927cf16..77f13bb258 100644 --- a/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.php +++ b/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.php @@ -35,7 +35,7 @@ public function getErrorList() 11 => 1, 12 => 1, 13 => 1, - 14 => 1, + 14 => 2, ]; }//end getErrorList() diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php index 4e6a22ed67..65f459815c 100644 --- a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php +++ b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php @@ -51,7 +51,7 @@ public function getErrorList() 67 => 1, 81 => 1, 106 => 1, - 107 => 1, + 107 => 2, 108 => 1, 117 => 1, ]; diff --git a/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php b/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php index 02eec6cdc1..3a89901150 100644 --- a/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -141,7 +141,6 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) $error = 'Public member variable "%s" must not contain a leading underscore'; $data = [$varName]; $phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data); - return; } } else { if (substr($varName, 0, 1) !== '_') { @@ -152,11 +151,13 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) $varName, ]; $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data); - return; } } - if (Common::isCamelCaps($varName, false, $public, false) === false) { + // Remove a potential underscore prefix for testing CamelCaps. + $varName = ltrim($varName, '_'); + + if (Common::isCamelCaps($varName, false, true, false) === false) { $error = 'Member variable "%s" is not in valid camel caps format'; $data = [$varName]; $phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);