Skip to content

Commit

Permalink
Merge pull request #2228 from WordPress/feature/validvariablename-min…
Browse files Browse the repository at this point in the history
…or-changes

NamingConventions/ValidVariableName: various minor changes
  • Loading branch information
dingo-d committed Apr 20, 2023
2 parents a25d821 + f3461c6 commit 2279dc4
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions WordPress/Sniffs/NamingConventions/ValidVariableNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff as PHPCS_AbstractVariableSniff;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\Scopes;
use PHPCSUtils\Utils\TextStrings;
use PHPCSUtils\Utils\Variables;
use WordPressCS\WordPress\Helpers\ContextHelper;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Helpers\SnakeCaseHelper;

Expand Down Expand Up @@ -102,38 +102,38 @@ final class ValidVariableNameSniff extends PHPCS_AbstractVariableSniff {
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcs_file The file being scanned.
* @param int $stack_ptr The position of the current token in the
* stack passed in $tokens.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
protected function processVariable( File $phpcs_file, $stack_ptr ) {
$tokens = $phpcs_file->getTokens();
protected function processVariable( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();

// If it's a php reserved var, then its ok.
if ( Variables::isPHPReservedVarName( $tokens[ $stack_ptr ]['content'] ) ) {
if ( Variables::isPHPReservedVarName( $tokens[ $stackPtr ]['content'] ) ) {
return;
}

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

$var_name = ltrim( $tokens[ $stack_ptr ]['content'], '$' );
$var_name = ltrim( $tokens[ $stackPtr ]['content'], '$' );

// Likewise if it is a mixed-case var used by WordPress core.
if ( isset( $this->wordpress_mixed_case_vars[ $var_name ] ) ) {
return;
}

$obj_operator = $phpcs_file->findNext( Tokens::$emptyTokens, ( $stack_ptr + 1 ), null, true );
$obj_operator = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
if ( \T_OBJECT_OPERATOR === $tokens[ $obj_operator ]['code']
|| \T_NULLSAFE_OBJECT_OPERATOR === $tokens[ $obj_operator ]['code']
) {
// Check to see if we are using a variable from an object.
$var = $phpcs_file->findNext( Tokens::$emptyTokens, ( $obj_operator + 1 ), null, true );
$var = $phpcsFile->findNext( Tokens::$emptyTokens, ( $obj_operator + 1 ), null, true );
if ( \T_STRING === $tokens[ $var ]['code'] ) {
$bracket = $phpcs_file->findNext( Tokens::$emptyTokens, ( $var + 1 ), null, true );
$bracket = $phpcsFile->findNext( Tokens::$emptyTokens, ( $var + 1 ), null, true );
if ( \T_OPEN_PARENTHESIS !== $tokens[ $bracket ]['code'] ) {
$obj_var_name = $tokens[ $var ]['content'];

Expand All @@ -148,15 +148,14 @@ protected function processVariable( File $phpcs_file, $stack_ptr ) {
$obj_var_name,
$suggested_name,
);
$phpcs_file->addError( $error, $var, 'UsedPropertyNotSnakeCase', $data );
$phpcsFile->addError( $error, $var, 'UsedPropertyNotSnakeCase', $data );
}
}
}
}

$in_class = false;
$obj_operator = $phpcs_file->findPrevious( Tokens::$emptyTokens, ( $stack_ptr - 1 ), null, true );
if ( isset( Collections::objectOperators()[ $tokens[ $obj_operator ]['code'] ] ) ) {
$in_class = false;
if ( ContextHelper::has_object_operator_before( $phpcsFile, $stackPtr ) === true ) {
// The variable lives within a class, and is referenced like
// this: MyClass::$_variable or $class->variable.
$in_class = true;
Expand All @@ -177,31 +176,31 @@ protected function processVariable( File $phpcs_file, $stack_ptr ) {
$var_name,
$suggested_name,
);
$phpcs_file->addError( $error, $stack_ptr, $error_name, $data );
$phpcsFile->addError( $error, $stackPtr, $error_name, $data );
}
}
}

/**
* Processes class member variables.
*
* @param \PHP_CodeSniffer\Files\File $phpcs_file The file being scanned.
* @param int $stack_ptr The position of the current token in the
* stack passed in $tokens.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
protected function processMemberVar( File $phpcs_file, $stack_ptr ) {
protected function processMemberVar( File $phpcsFile, $stackPtr ) {
// Make sure this is actually an OO property and not an OO method parameter or illegal property declaration.
if ( Scopes::isOOProperty( $phpcs_file, $stack_ptr ) === false ) {
if ( Scopes::isOOProperty( $phpcsFile, $stackPtr ) === false ) {
return;
}

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

$tokens = $phpcs_file->getTokens();
$var_name = ltrim( $tokens[ $stack_ptr ]['content'], '$' );
$tokens = $phpcsFile->getTokens();
$var_name = ltrim( $tokens[ $stackPtr ]['content'], '$' );

if ( isset( $this->allowed_mixed_case_member_var_names[ $var_name ] ) ) {
return;
Expand All @@ -214,24 +213,24 @@ protected function processMemberVar( File $phpcs_file, $stack_ptr ) {
$var_name,
$suggested_name,
);
$phpcs_file->addError( $error, $stack_ptr, 'PropertyNotSnakeCase', $data );
$phpcsFile->addError( $error, $stackPtr, 'PropertyNotSnakeCase', $data );
}
}

/**
* Processes the variables found within a double quoted string.
*
* @param \PHP_CodeSniffer\Files\File $phpcs_file The file being scanned.
* @param int $stack_ptr The position of the double quoted
* string.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the double quoted
* string.
*
* @return void
*/
protected function processVariableInString( File $phpcs_file, $stack_ptr ) {
$tokens = $phpcs_file->getTokens();
protected function processVariableInString( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();

// There will always be embeds if the processVariableInString() was called.
$embeds = TextStrings::getEmbeds( $tokens[ $stack_ptr ]['content'] );
$embeds = TextStrings::getEmbeds( $tokens[ $stackPtr ]['content'] );

// Merge any custom variables with the defaults.
$this->merge_allow_lists();
Expand Down Expand Up @@ -260,7 +259,7 @@ protected function processVariableInString( File $phpcs_file, $stack_ptr ) {
$var_name,
$suggested_name,
);
$phpcs_file->addError( $error, $stack_ptr, 'InterpolatedVariableNotSnakeCase', $data );
$phpcsFile->addError( $error, $stackPtr, 'InterpolatedVariableNotSnakeCase', $data );
}
}
}
Expand Down

0 comments on commit 2279dc4

Please sign in to comment.