Skip to content

Commit 4e8d27f

Browse files
committed
Sniff::is_in_isset_or_empty(): allow for array_key_exists()
Just like, `isset()` and `empty()`, `array_key_exists()` is a way to validate a variable exists and should therefore be recognized by this function.
1 parent 44e5857 commit 4e8d27f

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

WordPress/Sniff.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,9 +1428,11 @@ protected function has_nonce_check( $stackPtr ) {
14281428
}
14291429

14301430
/**
1431-
* Check if a token is inside of an isset() or empty() statement.
1431+
* Check if a token is inside of an isset(), empty() or array_key_exists() statement.
14321432
*
14331433
* @since 0.5.0
1434+
* @since 2.0.1 Now checks for the token being used as the array parameter
1435+
* in function calls to array_key_exists() as well.
14341436
*
14351437
* @param int $stackPtr The index of the token in the stack.
14361438
*
@@ -1448,7 +1450,42 @@ protected function is_in_isset_or_empty( $stackPtr ) {
14481450
$open_parenthesis = key( $nested_parenthesis );
14491451

14501452
$previous_non_empty = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $open_parenthesis - 1 ), null, true, null, true );
1451-
return in_array( $this->tokens[ $previous_non_empty ]['code'], array( \T_ISSET, \T_EMPTY ), true );
1453+
if ( false === $previous_non_empty ) {
1454+
return false;
1455+
}
1456+
1457+
$previous_code = $this->tokens[ $previous_non_empty ]['code'];
1458+
if ( \T_ISSET === $previous_code || \T_EMPTY === $previous_code ) {
1459+
return true;
1460+
}
1461+
1462+
if ( \T_STRING === $previous_code && 'array_key_exists' === $this->tokens[ $previous_non_empty ]['content'] ) {
1463+
$before_function = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $previous_non_empty - 1 ), null, true, null, true );
1464+
1465+
if ( false !== $before_function ) {
1466+
if ( \T_OBJECT_OPERATOR === $this->tokens[ $before_function ]['code']
1467+
|| \T_DOUBLE_COLON === $this->tokens[ $before_function ]['code']
1468+
) {
1469+
// Method call.
1470+
return false;
1471+
}
1472+
1473+
if ( \T_NS_SEPARATOR === $this->tokens[ $before_function ]['code'] ) {
1474+
$before_before = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $before_function - 1 ), null, true, null, true );
1475+
if ( false !== $before_before && \T_STRING === $this->tokens[ $before_before ]['code'] ) {
1476+
// Namespaced function call.
1477+
return false;
1478+
}
1479+
}
1480+
}
1481+
1482+
$second_param = $this->get_function_call_parameter( $previous_non_empty, 2 );
1483+
if ( $stackPtr >= $second_param['start'] && $stackPtr <= $second_param['end'] ) {
1484+
return true;
1485+
}
1486+
}
1487+
1488+
return false;
14521489
}
14531490

14541491
/**

0 commit comments

Comments
 (0)