Skip to content

Commit

Permalink
Merge pull request #597 from jrfnl/WPCS/feature/fix-bug-last-in-class
Browse files Browse the repository at this point in the history
Fix bug in control structures whitespace sniff triggered on last method of class
  • Loading branch information
westonruter authored Jul 15, 2016
2 parents 4cb2a26 + eee2279 commit a2b0383
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
6 changes: 3 additions & 3 deletions WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,9 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
// Another control structure's closing brace.
if ( isset( $tokens[ $trailingContent ]['scope_condition'] ) ) {
$owner = $tokens[ $trailingContent ]['scope_condition'];
if ( T_FUNCTION === $tokens[ $owner ]['code'] ) {
// The next content is the closing brace of a function
// so normal function rules apply and we can ignore it.
if ( in_array( $tokens[ $owner ]['code'], array( T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT ), true ) ) {
// The next content is the closing brace of a function, class, interface or trait
// so normal function/class rules apply and we can ignore it.
return;
}
}
Expand Down
23 changes: 23 additions & 0 deletions WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,26 @@ function ( $arg ) {} // OK

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

/**
* Test for bug where this sniff was triggering a "Blank line found after control structure" error
* if there is a blank line after the last method in a class.
*
* Bug did not trigger when a comment was found after the closing brace of the method.
*
* Neither of the below examples should trigger the error.
*/
class Bar_Foo {

function foo() {
} // Now you won't see the bug.

}

class Foo_Bar {

// Now you will.
function bar() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,26 @@ function ( $arg ) {} // OK

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

/**
* Test for bug where this sniff was triggering a "Blank line found after control structure" error
* if there is a blank line after the last method in a class.
*
* Bug did not trigger when a comment was found after the closing brace of the method.
*
* Neither of the below examples should trigger the error.
*/
class Bar_Foo {

function foo() {
} // Now you won't see the bug.

}

class Foo_Bar {

// Now you will.
function bar() {
}

}

0 comments on commit a2b0383

Please sign in to comment.