Skip to content

Commit 0aeb095

Browse files
committed
Fixed bug #3197 : Squiz.NamingConventions.ValidVariableName does not use correct error code for all member vars
The sniff now uses the MemberNotCamelCaps error code when member vars are being used, not just when they are defined.
1 parent 59d4c8d commit 0aeb095

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
8888
-- Thanks to Juliette Reinders Folmer for the patch
8989
- Fixed bug #3188 : Squiz.WhiteSpace.ScopeKeywordSpacing false positive for static return type
9090
-- Thanks to Juliette Reinders Folmer for the patch
91+
- Fixed bug #3197 : Squiz.NamingConventions.ValidVariableName does not use correct error code for all member vars
9192
</notes>
9293
<contents>
9394
<dir name="/">

src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,38 @@ protected function processVariable(File $phpcsFile, $stackPtr)
5757
}
5858

5959
if (Common::isCamelCaps($objVarName, false, true, false) === false) {
60-
$error = 'Variable "%s" is not in valid camel caps format';
60+
$error = 'Member variable "%s" is not in valid camel caps format';
6161
$data = [$originalVarName];
62-
$phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
62+
$phpcsFile->addError($error, $var, 'MemberNotCamelCaps', $data);
6363
}
6464
}//end if
6565
}//end if
6666
}//end if
6767

68+
$objOperator = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
69+
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
70+
// The variable lives within a class, and is referenced like
71+
// this: MyClass::$_variable, so we don't know its scope.
72+
$objVarName = $varName;
73+
if (substr($objVarName, 0, 1) === '_') {
74+
$objVarName = substr($objVarName, 1);
75+
}
76+
77+
if (Common::isCamelCaps($objVarName, false, true, false) === false) {
78+
$error = 'Member variable "%s" is not in valid camel caps format';
79+
$data = [$tokens[$stackPtr]['content']];
80+
$phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $data);
81+
}
82+
83+
return;
84+
}
85+
6886
// There is no way for us to know if the var is public or private,
6987
// so we have to ignore a leading underscore if there is one and just
7088
// check the main part of the variable name.
7189
$originalVarName = $varName;
7290
if (substr($varName, 0, 1) === '_') {
73-
$objOperator = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
74-
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
75-
// The variable lives within a class, and is referenced like
76-
// this: MyClass::$_variable, so we don't know its scope.
77-
$inClass = true;
78-
} else {
79-
$inClass = $phpcsFile->hasCondition($stackPtr, Tokens::$ooScopeTokens);
80-
}
81-
91+
$inClass = $phpcsFile->hasCondition($stackPtr, Tokens::$ooScopeTokens);
8292
if ($inClass === true) {
8393
$varName = substr($varName, 1);
8494
}

0 commit comments

Comments
 (0)