Inline if-clause with short-list false positive
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 😄
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.
P.S.: @sybrew Thanks for reporting this!
I have opened a PR in PHPCS upstream to address this: https://github.com/squizlabs/PHP_CodeSniffer/pull/3632
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
FYI: the upstream PR has been merged and will be included in PHPCS 3.7.2.