Skip to content

Scope not available error on dynamic variable instantiation #8007

@jakobbuis

Description

@jakobbuis

Bug Report

Subject Details
Rector version Rector 0.17.1
PHP version PHP 8.2.7 (cli) (built: Jun 8 2023 15:27:40) (NTS)

Ran Rector on our codebase, which crashed on a controller with the error:

[file] app/Http/Controllers/DummyController.php
[rule] Rector\Php55\Rector\String_\StringClassNameToClassConstantRector

[ERROR] Could not process "app/Http/Controllers/DummyController.php" file, due to:        
         "System error: "Scope not available on "PhpParser\Node\Scalar\String_" node with  
         parent node of "PhpParser\Node\Expr\BinaryOp\Concat", but is required by a        
         refactorWithScope() method of                                                     
         "Rector\Php55\Rector\String_\StringClassNameToClassConstantRector" rule. Fix scope
         refresh on changed nodes first"

Minimal PHP Code Causing Issue

It crashes on line 10 in this example, I preserved some of the lines to have some minimal context:

<?php

class DummyController
{
    public function foo()
    {
        $employee = (object) ['data' => ['field1' => 1, 'field' => 2]];

        for ($i=1; $i <= 2; $i++) {
            ${'field'.$f} = $employee->data['field'.$f];
        }
    }
}

Reproduced in the demo using only this rule.
https://getrector.com/demo/59cb8021-8fdb-44c6-b596-ae054c4fd8b7

Expected Behaviour

Not crashing, leaving the code as-is

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