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.
jrfnl committed Aug 28, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 4513849 commit b4b50a6
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
@@ -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 );
}

/**
15 changes: 15 additions & 0 deletions WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc
Original file line number Diff line number Diff line change
@@ -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 b4b50a6

Please sign in to comment.