Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect behavior of ChangeReadOnlyVariableWithDefaultValueToConstantRector #7900

Closed
plenka opened this issue Apr 24, 2023 · 2 comments · Fixed by rectorphp/rector-src#3687

Comments

@plenka
Copy link

plenka commented Apr 24, 2023

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.

@plenka plenka added the bug label Apr 24, 2023
@samsonasik
Copy link
Member

@plenka could you try provide a patch? this should can be checked if the variable in ArrayDimFetch with append to PreInc or PostInc or PostDec or PreDec

@plenka
Copy link
Author

plenka commented Apr 26, 2023

I've tried to set up a working copy of the rector-src repo, but composer fails on the patching (times out after 300 seconds for each patch), so I can't run any tests. I've also scrolled through the source code, but it would take a lot of time to get up to speed before I could even try to provide a patch. Unfortunately I don't have a lot of time to spare...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants