Skip to content

Commit 6c8d052

Browse files
committed
Sniff::is_comparison(): allow to disregard null coalesce
The null coalesce operator `??` is a special comparison operator, in the sense that it doesn't compare a variable to whatever is on the other side of the comparison operator. For this reason, it should be possible to disregard it.
1 parent 1a79eae commit 6c8d052

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

WordPress/Sniff.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,12 +2229,24 @@ protected function is_validated( $stackPtr, $array_keys = array(), $in_condition
22292229
* Also recognizes `switch ( $var )`.
22302230
*
22312231
* @since 0.5.0
2232+
* @since 2.1.0 Added the $include_coalesce parameter.
22322233
*
2233-
* @param int $stackPtr The index of this token in the stack.
2234+
* @param int $stackPtr The index of this token in the stack.
2235+
* @param bool $include_coalesce Optional. Whether or not to regard the null
2236+
* coalesce operator - ?? - as a comparison operator.
2237+
* Defaults to true.
2238+
* Null coalesce is a special comparison operator in this
2239+
* sense as it doesn't compare a variable to whatever is
2240+
* on the other side of the comparison operator.
22342241
*
22352242
* @return bool Whether this is a comparison.
22362243
*/
2237-
protected function is_comparison( $stackPtr ) {
2244+
protected function is_comparison( $stackPtr, $include_coalesce = true ) {
2245+
2246+
$comparisonTokens = Tokens::$comparisonTokens;
2247+
if ( false === $include_coalesce ) {
2248+
unset( $comparisonTokens[ \T_COALESCE ] );
2249+
}
22382250

22392251
// We first check if this is a switch statement (switch ( $var )).
22402252
if ( isset( $this->tokens[ $stackPtr ]['nested_parenthesis'] ) ) {
@@ -2258,7 +2270,7 @@ protected function is_comparison( $stackPtr ) {
22582270
true
22592271
);
22602272

2261-
if ( isset( Tokens::$comparisonTokens[ $this->tokens[ $previous_token ]['code'] ] ) ) {
2273+
if ( isset( $comparisonTokens[ $this->tokens[ $previous_token ]['code'] ] ) ) {
22622274
return true;
22632275
}
22642276

@@ -2281,7 +2293,7 @@ protected function is_comparison( $stackPtr ) {
22812293
);
22822294
}
22832295

2284-
if ( false !== $next_token && isset( Tokens::$comparisonTokens[ $this->tokens[ $next_token ]['code'] ] ) ) {
2296+
if ( false !== $next_token && isset( $comparisonTokens[ $this->tokens[ $next_token ]['code'] ] ) ) {
22852297
return true;
22862298
}
22872299

0 commit comments

Comments
 (0)