Skip to content

Commit

Permalink
Sniff::is_in_isset_or_empty(): improve code-style independence
Browse files Browse the repository at this point in the history
Includes unit tests in the ValidatedSanitizedInput test case file.
  • Loading branch information
jrfnl committed Aug 28, 2018
1 parent 4513849 commit 9cdf7ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,8 @@ protected function is_in_isset_or_empty( $stackPtr ) {
end( $nested_parenthesis );
$open_parenthesis = key( $nested_parenthesis );

return \in_array( $this->tokens[ ( $open_parenthesis - 1 ) ]['code'], array( \T_ISSET, \T_EMPTY ), true );
$previous_non_empty = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $open_parenthesis - 1 ), null, true, null, true);
return in_array( $this->tokens[ $previous_non_empty ]['code'], array( \T_ISSET, \T_EMPTY ), true );
}

/**
Expand Down
15 changes: 15 additions & 0 deletions WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,18 @@ EOD

if ( ( $_POST['foo'] ?? 'post' ) === 'post' ) {} // OK.
if ( ( $_POST['foo'] <=> 'post' ) === 0 ) {} // OK.

// Test whitespace independent isset/empty detection.
function foobar() {
if ( ! isset ($_GET['test']) ) {
return ;
}
echo sanitize_text_field( wp_unslash( $_GET['test'] ) ); // OK.
}

function barfoo() {
if ( empty ($_GET['test']) ) {
return ;
}
echo sanitize_text_field( wp_unslash( $_GET['test'] ) ); // OK.
}

0 comments on commit 9cdf7ad

Please sign in to comment.