phpcs-variable-analysis icon indicating copy to clipboard operation
phpcs-variable-analysis copied to clipboard

Inline if-clause with short-list false positive

Open sybrew opened this issue 3 years ago • 3 comments

Hello!

False-positive:

if ( true )
	[ $a ] = [ 'hi' ]; // VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable

return $a ?? ''; // VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable

Fine:

if ( true ) {
	[ $a ] = [ 'hi' ];
}
return $a ?? '';

Also fine:

if ( true )
	list( $a ) = [ 'hi' ];

return $a ?? '';

I don't think there's any more info to add. Cheers 😄

sybrew avatar Jul 10 '22 06:07 sybrew

FYI: This is not a bug in the VariableAnalysis standard, but a bug in PHPCS itself as it doesn't tokenize the square bracket after a close parenthesis as T_OPEN_SHORT_ARRAY.

This is related to PHP supporting function call dereferencing which uses the same token sequence call()['key'].

This will need a fix upstream in PHPCS.

jrfnl avatar Jul 13 '22 16:07 jrfnl

P.S.: @sybrew Thanks for reporting this!

jrfnl avatar Jul 13 '22 16:07 jrfnl

I have opened a PR in PHPCS upstream to address this: https://github.com/squizlabs/PHP_CodeSniffer/pull/3632

jrfnl avatar Jul 14 '22 13:07 jrfnl

Thank you @jrfnl! Since this sniff supports old phpcs versions, even if the bug is fixed upstream, I'm going to try to enable our parser to recognize this case in https://github.com/sirbrillig/phpcs-variable-analysis/pull/268

sirbrillig avatar Aug 12 '22 19:08 sirbrillig

FYI: the upstream PR has been merged and will be included in PHPCS 3.7.2.

jrfnl avatar Sep 19 '22 14:09 jrfnl