Skip to content

CompleteDynamicPropertiesRector is adding type in parents but not in children (and in the reverse order too) #7687

@Neila142

Description

@Neila142

Bug Report

Subject Details
Rector version 0.15.1
PHP version 8.2.0

The rule CompleteDynamicPropertiesRector is adding the type when it's finding it but this may break code when the type is not specified in childrens or in parents class

Minimal PHP Code Causing Issue

https://getrector.org/demo/cf6e1124-bbdd-448c-b0e2-0f51fea5625b

class Hi {
    public function hi() {
        $this->message = "hi";
    }
}

final class Hello extends Hi
{
    public function hello()
    {
        return $this->message;
    }
}

Expected Behaviour

I expect rector to add the type everywhere in all class inheritance tree, if it's the same attribute, but it doesn't...

This kind of behaviour seems like a problems with the phpstan's scope/context like explained in this comment:

  1. Due to PHPStan design that we inherit here, we can only work with current context. That means PHPStan does not know about all use cases, just about current class. E.g. dead public methods cannot be reported etc. This might change in the future, but so far there is nothing we can do about it.

So in this case, could it be possible to add a configure option to prevent rector to add the type everywhere? (or even just in a phpdoc ?)
It's not perfect but could still help a lot in code maintenance and prevent E_COMPILE_ERROR 😄

https://3v4l.org/XDWWQ

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