Skip to content

Incorrect behavior of ChangeReadOnlyVariableWithDefaultValueToConstantRector #7900

@plenka

Description

@plenka

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/a8c555df-13ab-46b2-abfd-d725cbe72a94

<?php

final class DemoFile
{
    /**
     * @var string[]
     */
    private const VALUES = ['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e'];
    
    public function run()
    {
        $arr = [];
        foreach (self::VALUES as $key => $value) {
            $arr[$value] ??= 0;
            $arr[$value] += $key;
        }
        return $arr;
    }
}

Responsible rules

  • ChangeReadOnlyVariableWithDefaultValueToConstantRector

Expected Behavior

Rector shouldn't suggest to convert a local variable into a private const when the local variable is just a temporary one. When the run method is executed twice, the result is not the same.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions