Skip to content

"static" variable in a method causes an error in PHPStormVarAnnotationRector #1494

@Aerendir

Description

@Aerendir

This simple method causes an ERROR:

/**
     * Validates the param checking if it is accepted in one of the allowed sections.
     *
     * This is useful to inform the developer that were passed a parameter that is not supported at all.
     *
     * @param string $param
     */
    private function validateParam(string $param)
    {
        static $aggregatedParams = null;

        if (null === $aggregatedParams) {
            $aggregatedParams = array_merge(self::CONFIG_ACCESS, self::CONFIG_KIND, self::CONFIG_SETTINGS);
        }

        if (false === array_key_exists($param, $aggregatedParams)) {
            throw new \InvalidArgumentException(sprintf(
                'The parameter "%s" you passed is not supported in any section of a RemoteConfig. Allowed parameters in each section are: Kind => %s; Access => %s; Settings => %s.',
                $param,
                implode(', ', self::CONFIG_KIND),
                implode(', ', self::CONFIG_ACCESS),
                implode(', ', self::CONFIG_SETTINGS)
            ));
        }
    }

The error is this:

[ERROR] Could not process
"/Users/Aerendir/Documents/JooServer/_Projects/SerendipityHQ/Bundles/bundle-my/src/Service/MyClass.php" file by "Rector\PHPStan\Rector\Assign\PHPStormVarAnnotationRector", due to:
"".

The error is caused by the use of the static keyword: if I remove it, the error disappears.

May be related to #1407

STEPS TO REPRODUCE

I've tried to add a test case in packages/PHPStan/test/Rector/Assign/PHPStormVarAnnotationRector/Fixture/fixture5.php.inc with this content:

<?php

function aFunctionWithStatisVariable() {
    static $variable = null;
}

?>
    -----
<?php

function aFunctionWithStatisVariable() {
    static $variable = null;
}

?>

The test fails with this message:

1) Rector\PHPStan\Tests\Rector\Assign\PHPStormVarAnnotationRector\PHPStormVarAnnotationRectorTest::test
Caused by /Users/Aerendir/Documents/JooServer/_ProjectsIContributeTo/rector/packages/PHPStan/tests/Rector/Assign/PHPStormVarAnnotationRector/Fixture/fixture5.php.inc
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
 }\n
 \n
 ?>\n
-'
+    '

This not seem what I'm trying to reproduce.

Another way I found to reproduce the issue is by creating a simple php file with this function inside:

<?php

function aFunctionWithStatisVariable() {
    static $variable = null;
}

This file will produce the error I get if I use the static keyword in a method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions