phpvisual-studiostatic-analysis

Avoid "variable is assigned, but its value is never used" [PHP]


In VS with PHPTools activated I have following code part:

foreach ($row['bgcolor'] as &$colBgColor) {
    $colBgColor = Color::COLOR_HEADER;
}

In the VS Error List I get following warning:
PHP0421 The variable '$colBgColor' is assigned, but its value is never used

I know that I could avoid this warning by refactoring the code. But what makes me wonder is how I can remove this warning with an annotation, a PHPTools configuration or something else other than code refactoring.

I tried following annotations above foreach without success:


Solution

  • You can accomplish this in two ways. First, by creating a .editorconfig file in your project's base directory or the directory with the code in question, with the following contents:

    [*.php]
    php_diagnostic_php0421 = off
    

    Second, inline via @suppress in a docblock:

    /**
     * @suppress PHP0421
     */
    function foo()
    {
    }
    

    Note that the @suppress method works at the function or class level, not the specific line like PHPStan's @ignore does. See here for details.