rector icon indicating copy to clipboard operation
rector copied to clipboard

Incorrect behavior of CompleteDynamicPropertiesRector

Open zoka123 opened this issue 5 years ago • 3 comments

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.org/demo/1ebfc2bc-a0ad-6252-988e-97f3c77f8863

<?php

final class A
{
    private string $value = '123';
}

final class B {
    public string $v = '123';

    public function doSomething(A $a){
        $this->v = (fn() => $this->value)->call($a);
    }
}
$a = new A();
$b = new B();
$b->doSomething($a);
var_dump($b->v);

Responsible rules

  • CompleteDynamicPropertiesRector

Expected Behavior

Not sure if that is possible, but ideally Rector should recognize that call temporarily binds closure to newThis and that $this references to another object (instance of A) and not the current one (instance of B)

https://www.php.net/manual/en/closure.call.php

When explicitly using Closure::bind or Closure::bindTo, Rector seem to identify the context correctly (https://getrector.org/demo/1ebfc2cc-8c3a-60a8-9015-b5f828e068bd) but with the short version it doesn't seem to work.

zoka123 avatar Aug 13 '21 11:08 zoka123

Thank you for your report and demo link!

Could you send a failing test case in a pull-request, so we have it covered in Rector? The class is CompleteDynamicPropertiesRectorTest, with /Fixtures directory next to it.

Here is step by step tutorial how to add it: https://github.com/rectorphp/rector/blob/master/docs/how_to_add_test_for_rector_rule.md

TomasVotruba avatar Aug 13 '21 11:08 TomasVotruba

Thank you guys!

zoka123 avatar Aug 16 '21 08:08 zoka123

Thank you @samsonasik for fixing this :+1:

TomasVotruba avatar Aug 16 '21 09:08 TomasVotruba