Skip to content

Commit

Permalink
Add T_USE to sniffed control structures
Browse files Browse the repository at this point in the history
See #417
  • Loading branch information
JDGrimes committed Aug 25, 2015
1 parent 3dbcb35 commit 7d19272
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
44 changes: 38 additions & 6 deletions WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function register()
T_ELSEIF,
T_FUNCTION,
T_CLOSURE,
T_USE,
);

}//end register()
Expand Down Expand Up @@ -119,12 +120,19 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
}

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

if ( T_USE === $tokens[ $stackPtr ]['code'] ) {
$scopeOpener = $phpcsFile->findNext( T_OPEN_CURLY_BRACKET, $stackPtr + 1 );
$scopeCloser = $tokens[ $scopeOpener ]['scope_closer'];
} else {
return;
}

$scopeOpener = $tokens[$stackPtr]['scope_opener'];
$scopeCloser = $tokens[$stackPtr]['scope_closer'];
} else {
$scopeOpener = $tokens[ $stackPtr ]['scope_opener'];
$scopeCloser = $tokens[ $stackPtr ]['scope_closer'];
}

// alternative syntax
if ( $tokens[$scopeOpener]['code'] === T_COLON ) {
Expand Down Expand Up @@ -212,7 +220,31 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
}

} elseif ( $tokens[$parenthesisOpener]['code'] !== T_COLON ) {
} elseif ( T_CLOSURE === $tokens[ $stackPtr ]['code'] ) {

// Check if there is a use () statement.
if ( isset( $tokens[ $parenthesisOpener ]['parenthesis_closer'] ) ) {

$usePtr = $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
$tokens[ $parenthesisOpener ]['parenthesis_closer'] + 1,
null,
true,
null,
true
);

// If it is, we set that as the "scope opener".
if ( T_USE === $tokens[ $usePtr ]['code'] ) {
$scopeOpener = $usePtr;
}
}
}

if (
T_COLON !== $tokens[ $parenthesisOpener ]['code']
&& T_FUNCTION !== $tokens[ $stackPtr ]['code']
) {

if (
T_CLOSURE === $tokens[ $stackPtr ]['code']
Expand Down
14 changes: 14 additions & 0 deletions WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,18 @@ function( $arg ) {} // OK
function($arg){} // Bad
function () {} // Bad

$closureWithArgsAndVars = function( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // OK
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad

// @codingStandardsChangeSetting WordPress.WhiteSpace.ControlStructureSpacing spaces_before_closure_open_paren 1

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // OK

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use( $var1, $var2 ) {}; // Bad, no space before open parenthesis prohibited.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space before opening parenthesis.

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ){}; // Bad, space between closing parenthesis and control structure required.
$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.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space between closing parenthesis and control structure.
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,18 @@ function( $arg ) {} // OK
function( $arg ) {} // Bad
function() {} // Bad

$closureWithArgsAndVars = function( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // OK
$closureWithArgsAndVars = function( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad

// @codingStandardsChangeSetting WordPress.WhiteSpace.ControlStructureSpacing spaces_before_closure_open_paren 1

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // OK

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, no space before open parenthesis prohibited.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space before opening parenthesis.

$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, space between closing parenthesis and control structure required.
$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.
$closureWithArgsAndVars = function ( $arg1, $arg2 ) use ( $var1, $var2 ) {}; // Bad, expected exactly one space between closing parenthesis and control structure.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public function getErrorList()
72 => 1,
81 => 3,
82 => 1,
85 => 1,
91 => 2,
92 => 1,
94 => 1,
95 => 1,
97 => 1,
98 => 1,
);

// Uncomment when "$blank_line_check" parameter will be "true" by default.
Expand Down

0 comments on commit 7d19272

Please sign in to comment.