Skip to content

Commit 15b7ffb

Browse files
authored
Merge pull request #1664 from WordPress-Coding-Standards/feature/is_sanitized-code-style-independence
Sniff::is_sanitized(): make the method more code style independent
2 parents 5f27d8e + d43381a commit 15b7ffb

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

WordPress/Sniff.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,54 +1572,55 @@ protected function is_sanitized( $stackPtr, $require_unslash = false ) {
15721572
if ( $require_unslash ) {
15731573
$this->add_unslash_error( $stackPtr );
15741574
}
1575+
15751576
return false;
15761577
}
15771578

15781579
// Get the function that it's in.
15791580
$nested_parenthesis = $this->tokens[ $stackPtr ]['nested_parenthesis'];
1580-
$function_closer = end( $nested_parenthesis );
1581-
$function_opener = key( $nested_parenthesis );
1582-
$function = $this->tokens[ ( $function_opener - 1 ) ];
1581+
$nested_openers = array_keys( $nested_parenthesis );
1582+
$function_opener = array_pop( $nested_openers );
1583+
$functionPtr = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $function_opener - 1 ), null, true, null, true );
15831584

15841585
// If it is just being unset, the value isn't used at all, so it's safe.
1585-
if ( \T_UNSET === $function['code'] ) {
1586+
if ( \T_UNSET === $this->tokens[ $functionPtr ]['code'] ) {
15861587
return true;
15871588
}
15881589

1589-
// If this isn't a call to a function, it sure isn't sanitizing function.
1590-
if ( \T_STRING !== $function['code'] ) {
1590+
// If this isn't a call to a function, it sure isn't a sanitizing function.
1591+
if ( \T_STRING !== $this->tokens[ $functionPtr ]['code'] ) {
15911592
if ( $require_unslash ) {
15921593
$this->add_unslash_error( $stackPtr );
15931594
}
1595+
15941596
return false;
15951597
}
15961598

1597-
$functionName = $function['content'];
1599+
$functionName = $this->tokens[ $functionPtr ]['content'];
15981600

15991601
// Check if wp_unslash() is being used.
16001602
if ( 'wp_unslash' === $functionName ) {
16011603

16021604
$is_unslashed = true;
1603-
$function_closer = prev( $nested_parenthesis );
1605+
$function_opener = array_pop( $nested_openers );
16041606

16051607
// If there is no other function being used, this value is unsanitized.
1606-
if ( ! $function_closer ) {
1608+
if ( ! isset( $function_opener ) ) {
16071609
return false;
16081610
}
16091611

1610-
$function_opener = key( $nested_parenthesis );
1611-
$functionName = $this->tokens[ ( $function_opener - 1 ) ]['content'];
1612+
$functionPtr = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $function_opener - 1 ), null, true, null, true );
1613+
$functionName = $this->tokens[ $functionPtr ]['content'];
16121614

16131615
} else {
1614-
16151616
$is_unslashed = false;
16161617
}
16171618

16181619
// Arrays might be sanitized via array_map().
16191620
if ( 'array_map' === $functionName ) {
16201621

16211622
// Get the first parameter.
1622-
$callback = $this->get_function_call_parameter( ( $function_opener - 1 ), 1 );
1623+
$callback = $this->get_function_call_parameter( $functionPtr, 1 );
16231624

16241625
if ( ! empty( $callback ) ) {
16251626
/*

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,5 @@ if ( $obj->array_key_exists( 'my_field4', $_POST ) ) {
209209
if ( ClassName::array_key_exists( 'my_field5', $_POST ) ) {
210210
$id = (int) $_POST['my_field5']; // Bad.
211211
}
212+
213+
echo sanitize_text_field (wp_unslash ($_GET['test'])); // OK.

0 commit comments

Comments
 (0)