rectorphp / rector

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

Incorrect behavior of RemoveParentCallWithoutParentRector #8828

Closed ItsReddi closed 1 month ago

ItsReddi commented 1 month ago

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/09564344-3758-456b-943f-783aa029f409

<?php
namespace one;
class DemoFile
{
    protected bool $test = false;
    public function init(): void
    {
        $this->test = true;
    }
}
namespace two;
class FirstChild extends one\DemoFile {
}
namespace two;
class SecondChild extends FirstChild {
    protected bool $foo = false;
    public function init(): void {
        parent::init();
        $this->foo = true;
    }
}

Responsible rules

Expected Behavior

Should not remove the parent::init call.

samsonasik commented 1 month ago

It seems due to different namespace, on same namespace, it seems skipped as expected https://getrector.com/demo/c0a2b73f-3195-4332-8a06-208df86f8f6c

samsonasik commented 1 month ago

I see, that's due to one\DemoFile locate existing two, which you should add:

use one;

without that, you will get:

Fatal error: Uncaught Error: Class "two\one\DemoFile" not found in /in/LeXsi:13

see https://3v4l.org/LeXsi

here the fixed version: https://3v4l.org/mkB72 see https://getrector.com/demo/f13d109a-1d4f-4c54-a0a3-8ce5da11cba0

ItsReddi commented 1 month ago

Thanks. Then in our case we have some issues with the rector autoloading. Have to check that. Might be because the composer autoload does not contain these classes since they are loaded via the framework / application at runtime and rector does not start the application.

But should rector not raise error if classes can not be found? How can i debug which classes are not loaded?

samsonasik commented 1 month ago

My suggestion is to have phpstan setup on your project so you're detecting autoload early by it in case of class not found, see

https://phpstan.org/r/22f92cae-92ab-46c1-8516-e8a29eeee879

ItsReddi commented 1 month ago

Thank i can confirm its an autoload issue in our end. Adding:

    "autoload": {
        "psr-4": {
            "data_hub\\": "common/modules/data_hub/src",
            "game\\": "common/modules/game/src",
            "basics\\common\\module\\": "common/modules/base/src"
        }
    },

works as expected. but if i try to not to modify composer.json and the it via rector config, not. Is that expected? Did i understood anything wrong?

That is not working:

return RectorConfig::configure()
->withBootstrapFiles([
    __DIR__ . '/vendor/yiisoft/yii2/Yii.php'
])
->withAutoloadPaths([
    __DIR__ . "/common/modules/game/src",
    __DIR__ . "/common/modules/base/src"
])