Skip to content

AddDefaultValueForUndefinedVariableRector is re-assigning $this #2422

@JoyceBabu

Description

@JoyceBabu
Subject Details
Rector version v0.6.0
PHP version PHP 7.3.12
Full Command composer/bin/rector process rector/test.php -vvv

Current Behaviour

AddDefaultValueForUndefinedVariableRector rector is re-assigning $this inside a bound closure.

Rector v0.6.0
Config file: rector.yaml

[parsing] rector/test.php
[refactoring] rector/test.php
    [applying] Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector
[printing] rector/test.php


1 file with changes
===================

1) rector/test.php

    ---------- begin diff ----------
--- Original
+++ New
@@ -3,6 +3,7 @@
     private $val = 2;
 }
 $fn = function() {
+    $this = null;
     return $this->val;
 };
    ----------- end diff -----------

Applied rules:

 * Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector

Minimal PHP Code Causing Issue

<?php
class A {
    private $val = 2;
}
$fn = function() {
    return $this->val;
};

$closure = Closure::bind($fn, new A(), 'A');
echo $closure(), "\n";

Expected Behaviour

Rector should not attempt to initialize$this

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