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

Version 6.7.5 (build 38563)
Example: In this example, it will mark $return = “
\n”; as an unused variable.

function libxml_display_error($error)
{
    $return = "<br/>\n";
    switch ($error->level) {
        case LIBXML_ERR_WARNING:
            $return .= "<b>Warning $error->code</b>: ";
            break;
        case LIBXML_ERR_ERROR:
            $return .= "<b>Error $error->code</b>: ";
            break;
        case LIBXML_ERR_FATAL:
            $return .= "<b>Fatal Error $error->code</b>: ";
            break;
    }
    $return .= trim($error->message);
    if ($error->file) {
        $return .=    " in <b>$error->file</b>";
    }
    return $return .= " on line <b>$error->line</b>\n";
}

Indeed, this is a false positive.
I created a ticket to track it: SONARPHP-831
We should fix it in the next version.

Thanks for the feedback!
Pierre-Yves

That’s great. Thank you.