From 7d192722e6a6c3b8a3f7d9b5216f0582685de8af Mon Sep 17 00:00:00 2001 From: JDGrimes Date: Mon, 24 Aug 2015 20:21:02 -0400 Subject: [PATCH] Add T_USE to sniffed control structures See #417 --- .../ControlStructureSpacingSniff.php | 44 ++++++++++++++++--- .../ControlStructureSpacingUnitTest.inc | 14 ++++++ .../ControlStructureSpacingUnitTest.inc.fixed | 14 ++++++ .../ControlStructureSpacingUnitTest.php | 7 +++ 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php index 3dd825314e..12b5e026d9 100644 --- a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php @@ -81,6 +81,7 @@ public function register() T_ELSEIF, T_FUNCTION, T_CLOSURE, + T_USE, ); }//end register() @@ -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 ) { @@ -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'] diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc index d9717a1de0..a337d2f7a5 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc @@ -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. diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed index 918ec11f42..b2c016c7c6 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed @@ -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. diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php index 076b220509..7d732713cd 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php @@ -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.