Skip to content

Incorrect behavior of RemoveNonExistingVarAnnotationRector with static variable in method #9428

@mpesari

Description

@mpesari

Bug Report

Subject Details
Rector version 2.2.2
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/b3ad4952-df3b-44c7-8823-e1b0fe090d27

<?php

namespace Rector\Tests\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;

final class DemoFile
{
    public function run(int $param): int
    {
        /**
         * Rector should not remove this @var annotation.
         *
         * @var array<int,int>
         */
        static $cached_result = [];
        
        if (!isset($cached_result[$param])) {
            $cached_result[$param] = doExpensiveCalculation($param);
        }
        
        return $cached_result[$param];
    }
}

Responsible rules

  • RemoveNonExistingVarAnnotationRector

Expected Behavior

The @var annotation of static $variable in method should not be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions