Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in control structures whitespace sniff triggered on last method of class #597

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc
Original file line number Diff line number Diff line change
@@ -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
@@ -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() {
}

}