rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.71k stars 687 forks source link

NullsafeOperatorRector tries to fix code that should be left alone #6680

Closed echo511 closed 3 years ago

echo511 commented 3 years ago

Bug Report

Subject Details
Rector version v0.11.52 130b634b5e348554c3349ed3b3f52fa3d08bfb6e

NullsafeOperatorRector tries to fix code that should be left alone. Example below.

Minimal PHP Code Causing Issue

public function getValue(\Entity $entity)
{
    $value = $this->baseField->getValue($entity);

    if ($value === null) {
        return null;
    }

    return $this->baseField->select($value);
}

Current Behaviour

public function getValue(\Entity $entity)
{
    return $this->baseField->getValue($entity)?->select($value);
}

Expected Behaviour

Should be left as is.

samsonasik commented 3 years ago

Could you create failing test case for it, you can create under https://github.com/rectorphp/rector-src/tree/479df50e71997ffcc081fa4818a859ee4712ac4f/rules-tests/Php74/Rector/Assign/NullCoalescingOperatorRector/Fixture

echo511 commented 3 years ago

Test case and fix created in enclosed PR.