From 9cdf7ad45f8c55552f9bfcbec1b18d9032927b09 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 27 Aug 2018 17:55:02 +0200 Subject: [PATCH] Sniff::is_in_isset_or_empty(): improve code-style independence Includes unit tests in the ValidatedSanitizedInput test case file. --- WordPress/Sniff.php | 3 ++- .../Security/ValidatedSanitizedInputUnitTest.inc | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/WordPress/Sniff.php b/WordPress/Sniff.php index e688876d45..b0ba2031e6 100644 --- a/WordPress/Sniff.php +++ b/WordPress/Sniff.php @@ -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 ); } /** diff --git a/WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc b/WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc index 376e05db4f..46d5498248 100644 --- a/WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc +++ b/WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc @@ -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. +}