Skip to content

Commit

Permalink
Fixed bug #3197 : Squiz.NamingConventions.ValidVariableName does not …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
gsherwood committed Jan 14, 2021
1 parent 59d4c8d commit 0aeb095
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #3188 : Squiz.WhiteSpace.ScopeKeywordSpacing false positive for static return type
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #3197 : Squiz.NamingConventions.ValidVariableName does not use correct error code for all member vars
</notes>
<contents>
<dir name="/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,38 @@ protected function processVariable(File $phpcsFile, $stackPtr)
}

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

$objOperator = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
// The variable lives within a class, and is referenced like
// this: MyClass::$_variable, so we don't know its scope.
$objVarName = $varName;
if (substr($objVarName, 0, 1) === '_') {
$objVarName = substr($objVarName, 1);
}

if (Common::isCamelCaps($objVarName, false, true, false) === false) {
$error = 'Member variable "%s" is not in valid camel caps format';
$data = [$tokens[$stackPtr]['content']];
$phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $data);
}

return;
}

// There is no way for us to know if the var is public or private,
// so we have to ignore a leading underscore if there is one and just
// check the main part of the variable name.
$originalVarName = $varName;
if (substr($varName, 0, 1) === '_') {
$objOperator = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
// The variable lives within a class, and is referenced like
// this: MyClass::$_variable, so we don't know its scope.
$inClass = true;
} else {
$inClass = $phpcsFile->hasCondition($stackPtr, Tokens::$ooScopeTokens);
}

$inClass = $phpcsFile->hasCondition($stackPtr, Tokens::$ooScopeTokens);
if ($inClass === true) {
$varName = substr($varName, 1);
}
Expand Down

0 comments on commit 0aeb095

Please sign in to comment.