Skip to content

Commit

Permalink
Merge pull request #671 from WordPress-Coding-Standards/feature/impro…
Browse files Browse the repository at this point in the history
…ve-variable-name-whitelisting

Improve VariableName whitelisting for the `ValidVariableName` sniff.
  • Loading branch information
westonruter authored Aug 27, 2016
2 parents 94b4858 + 9a63577 commit 25b07b3
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class WordPress_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_Code
*
* @var bool
*/
public static $addedCustomVariables = false;
protected $addedCustomVariables = false;

/**
* Processes this test, when one of its tokens is encountered.
Expand All @@ -88,12 +88,8 @@ class WordPress_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_Code
*/
protected function processVariable( PHP_CodeSniffer_File $phpcs_file, $stack_ptr ) {

// Merge any custom variables with the defaults, if we haven't already.
if ( ! self::$addedCustomVariables ) {
$this->whitelisted_mixed_case_member_var_names = array_merge( $this->whitelisted_mixed_case_member_var_names, $this->customVariablesWhitelist );

self::$addedCustomVariables = true;
}
// Merge any custom variables with the defaults.
$this->mergeWhiteList();

$tokens = $phpcs_file->getTokens();
$var_name = ltrim( $tokens[ $stack_ptr ]['content'], '$' );
Expand Down Expand Up @@ -173,6 +169,9 @@ protected function processVariable( PHP_CodeSniffer_File $phpcs_file, $stack_ptr
*/
protected function processMemberVar( PHP_CodeSniffer_File $phpcs_file, $stack_ptr ) {

// Merge any custom variables with the defaults.
$this->mergeWhiteList();

$tokens = $phpcs_file->getTokens();

$var_name = ltrim( $tokens[ $stack_ptr ]['content'], '$' );
Expand Down Expand Up @@ -203,6 +202,7 @@ protected function processMemberVar( PHP_CodeSniffer_File $phpcs_file, $stack_pt
* @return void
*/
protected function processVariableInString( PHP_CodeSniffer_File $phpcs_file, $stack_ptr ) {

$tokens = $phpcs_file->getTokens();

if ( preg_match_all( '|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[ $stack_ptr ]['content'], $matches ) > 0 ) {
Expand Down Expand Up @@ -232,4 +232,17 @@ static function isSnakeCase( $var_name ) {
return (bool) preg_match( '/^[a-z0-9_]+$/', $var_name );
}

/**
* Merge a custom whitelist provided via a custom ruleset with the predefined whitelist,
* if we haven't already.
*
* @return void
*/
protected function mergeWhiteList() {
if ( false === $this->addedCustomVariables && ! empty( $this->customVariablesWhitelist ) ) {
$this->whitelisted_mixed_case_member_var_names = array_merge( $this->whitelisted_mixed_case_member_var_names, $this->customVariablesWhitelist );
$this->addedCustomVariables = true;
}
}

} // End class.

0 comments on commit 25b07b3

Please sign in to comment.