false positive on Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed and compact
Describe the bug Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed is triggered when variables are used via compact in function
Code sample
public static function insertMessage(int $id, string $language, string $translation): void
{
Yii::$app->db->createCommand()->insert(
'message',
compact('id', 'language', 'translation')
)->execute();
}
Just seeing this as well. My code sample:
function get_transient_key( string $content, array $attributes, bool $is_feed, array $auto_detect_languages ): ?string {
$hash_input = wp_json_encode(
array_merge(
compact( 'content', 'attributes', 'is_feed', 'auto_detect_languages' ),
[
'version' => PLUGIN_VERSION,
]
)
);
if ( ! is_string( $hash_input ) ) {
return null;
}
return 'shcb-' . md5( $hash_input );
}
I concur this is a bug, but one which may not be that easy to fix.
To be honest, in my personal opinion, the use of compact() is a bit outdated and I'm actually surprised that PHP hasn't deprecated the function yet.
Out of curiousity: why not just declare the array ?
To take @westonruter's code sample, this could be rewritten to:
$hash_input = wp_json_encode(
array(
'content' => $content,
'attributes' => $attributes,
'is_feed' => $is_feed,
'auto_detect_languages' => $auto_detect_languages,
'version' => PLUGIN_VERSION,
)
);
This is also a micro-optimization as it gets rid of two unnecessary function calls.
I guess because it's less redundant. JavaScript supports this same syntax as part of the language, for example if the above were written in JS instead:
const hash_input = JSON.stringify(
{
content,
attributes,
is_feed,
auto_detect_languages,
version: PLUGIN_VERSION
}
);
Granted this is PHP and not JS.
Granted this is PHP and not JS.
Exactly.
In my case, i was working with a legacy app, where I had to live with compact (only had to use it for this one project and didnt like it at all, but thats another thing)