Skip to content

Commit

Permalink
Only check T_USE tokens associated with closures
Browse files Browse the repository at this point in the history
The `T_USE` token is also used to import traits to classes, and to
import classes into namespaces.

Fixes #431
  • Loading branch information
JDGrimes committed Aug 29, 2015
1 parent d5717e7 commit 84a7ffa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
34 changes: 34 additions & 0 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,40 @@ protected function is_comparison( $stackPtr ) {

return false;
}

/**
* Check what type of 'use' statement a token is part of.
*
* The T_USE token has multiple different uses:
*
* 1. In a closure: function () use ( $var ) {}
* 2. In a class, to import a trait: use Trait_Name
* 3. In a namespace, to import a class: use Some\Class;
*
* This function will check the token and return 'closure', 'trait', or 'class',
* based on which of these uses the use is being used for.
*
* @param int $stackPtr The position of the token to check.
*
* @return string The type of use.
*/
protected function get_use_type( $stackPtr ) {

// USE keywords inside closures.
$next = $this->phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true );

if ( T_OPEN_PARENTHESIS === $this->tokens[ $next ]['code'] ) {
return 'closure';
}

// USE keywords for traits.
if ( $this->phpcsFile->hasCondition( $stackPtr, array( T_CLASS, T_TRAIT ) ) ) {
return 'trait';
}

// USE keywords for classes to import to a namespace.
return 'class';
}
}

// EOF
7 changes: 4 additions & 3 deletions WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
* @author Greg Sherwood <[email protected]>
* @author Marc McIntyre <[email protected]>
*/
class WordPress_Sniffs_WhiteSpace_ControlStructureSpacingSniff implements PHP_CodeSniffer_Sniff
{
class WordPress_Sniffs_WhiteSpace_ControlStructureSpacingSniff extends WordPress_Sniff {

/**
* A list of tokenizers this sniff supports.
Expand Down Expand Up @@ -103,6 +102,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)

$tokens = $phpcsFile->getTokens();

$this->init( $phpcsFile );

if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE
&& ! ( $tokens[$stackPtr]['code'] === T_ELSE && $tokens[($stackPtr + 1)]['code'] === T_COLON )
&& ! ( T_CLOSURE === $tokens[ $stackPtr ]['code'] && 0 === (int) $this->spaces_before_closure_open_paren )
Expand All @@ -122,7 +123,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)

if ( isset( $tokens[ $stackPtr ]['scope_closer'] ) === false ) {

if ( T_USE === $tokens[ $stackPtr ]['code'] ) {
if ( T_USE === $tokens[ $stackPtr ]['code'] && 'closure' === $this->get_use_type( $stackPtr ) ) {
$scopeOpener = $phpcsFile->findNext( T_OPEN_CURLY_BRACKET, $stackPtr + 1 );
$scopeCloser = $tokens[ $scopeOpener ]['scope_closer'];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ $closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; //

$closureWithArgsAndVars = function ( $arg1, $arg2 )use ( $var1, $var2 ) {}; // Bad, expected exactly one space between closing parenthesis and control structure.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space between closing parenthesis and control structure.

// Namespaces.
use Foo\Admin;
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ $closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; //

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space between closing parenthesis and control structure.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space between closing parenthesis and control structure.

// Namespaces.
use Foo\Admin;

0 comments on commit 84a7ffa

Please sign in to comment.