PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

False positive Generic.ControlStructures.DisallowYodaConditions

Open jrfnl opened this issue 9 months ago • 4 comments

Repost from https://github.com/squizlabs/PHP_CodeSniffer/issues/2962:

The Generic.ControlStructures.DisallowYodaConditions reports a Usage of Yoda conditions is not allowed; switch the expression order error for the below code, which IMO is incorrect (the error, not the code).

$var = ( (string) function_call( 'text' ) ) === '0';

@umherirrender provided a second code sample:

Another one:

					return ( $group && $pos ) ?
						( $pos % $group ) === 0 && ( $pos < 0 === $group < 0 ) :
						!$pos;

It seems that a boolean condition with two < 0 is not handled correctly by the sniff.

Work around: Add () around each part of the condition


@jlherren provided a third:

I'm also experiencing this, here's a very short example:

$a = 1 * (1 + 1) !== 0;

jrfnl avatar Nov 08 '23 09:11 jrfnl