DEVSENSE / phptools-docs

PHP Tools public content
Apache License 2.0
80 stars 10 forks source link

Issue: Abstract Class Implementing Interface Not Recognized #671

Open DSchuppelius opened 2 weeks ago

DSchuppelius commented 2 weeks ago

Hi,

I’ve encountered an issue with your extension while working with abstract classes and interfaces in PHP. Specifically, when an abstract class implements an interface, and I extend that abstract class in another class, the interface is not recognized by the extension.

Here is a simplified example to illustrate the issue:

interface MyInterface {
    public function myMethod();
}

abstract class MyAbstractClass implements MyInterface {
    public function myMethod() {
        // Implementation here.
    }
}

class MyClass extends MyAbstractClass {
    // Additional methods or overrides here.
}
class TestAPIClientFactory {
    private static ?MyInterface $client = null;

    public static function getClient(): MyInterface {
        if (self::$client === null) {
            $config = new Config();
            self::$client = new MyAbstractClass();
        }
        return self::$client;
    }
}
class xy {
    protected ?MyClass $client;
    public function __construct($name) {
        parent::__construct($name);
        $this->client = TestAPIClientFactory::getClient(); // Error: Cannot implicitly convert ....
    }
}

In this case, the interface methods should be recognized in both the abstract class and the class that extends it. However, the extension seems to fail in recognizing the interface in the extended class.

Interestingly, the issue disappears if I explicitly implement the interface in the extended class, like this: class MyClass extends MyAbstractClass implements MyInterface { // Additional methods or overrides here. } I've tried restarting VS Code and ensured the latest version of the extension is installed, but the issue persists. Let me know if you need any further details.

jakubmisek commented 2 weeks ago

Thank you for the test case.

Isn't it correct though? getClient returns MyInterface, and MyInterface is not MyClass

DSchuppelius commented 1 week ago

Sorry... i mean

self::$client = new MyClass();

jakubmisek commented 5 days ago

$this->client = TestAPIClientFactory::getClient() is not correct, because getClient(): MyInterface but $this->client is expecting an instance of MyClass.