Skip to content

Incorrect behavior of RemoveUnusedVariableAssignRector #9397

@ruudk

Description

@ruudk

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/ea865936-8c1a-4759-92f7-6cf4fa4a94b8

<?php

class Test {
    function method() {
        $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler, $signalingException) {
            if ($file === __FILE__) {
                throw $signalingException;
            }

            return $prevErrorHandler !== null ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
        });
    }
}

Responsible rules

  • RemoveUnusedVariableAssignRector

Expected Behavior

As you can see, the variable $prevErrorHandler is used by reference inside the closure.

This is supported by PHP.

The variable should not be removed.

/cc @samsonasik

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