php:S1481 - Code Smell: Unused Local Variables should be removed - False Positive with Compact

intellij
php
sonarlint

(Spenser Hale) #1

Version: Intellij SonarLint v3.4.2.2586
Error: php:S1481 does not understand compact’s use of variable
Example: In this example, it will mark $label as an unused variable.

function get_choices(array $items): array
{
    $choices = [];
    foreach ($items as $label => $value) {
        $choices[] = \compact('label', 'value');
    }

    return $choices;
}

Another Example: In this example it will mark $var as unused;

function example():array
{
    $var = true;
    return \compact('var');
}

To Resolve: Check for compact variable use.


(Alexandre Gigleux) #2

Hello Spenser,

Thanks for reporting. Here is the ticket I created to fix this FP: https://jira.sonarsource.com/browse/SONARPHP-809

Regards