bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.56k stars 92 forks source link

Union types with methods having different return types #2906

Closed gfucci closed 4 days ago

gfucci commented 2 weeks ago

Describe the bug When a variable is instantiated with multiple classes (using union types), and those classes have methods with the same name but different return types, accessing that variable through another class dynamically causes the plugin to consider only the first defined class.

To Reproduce Config classes

class PrincipalClass
{
     /**
     * Instance of the main Request object.
     *
     * @var CLIrequest|HttpRequest
     */
    protected $request;
}
class CLIRequest
{
        /**
     * @return array|null
     */
    public function test()
    {
        return $this->returnNullOrEmptyArray();
    }
}
class HttpRequest
{
      /**
     * @return string
     */
    public function test()
    {
        return $this->returnString();
    }
}

Usage Classes

class Controller extends PrincipalClass
{
     public function initialize()
    {
        // Detect the request type
        if ($this->isCLI()) {
            $this->request = new CLIRequest();
        } else {
            $this->request = new HttpRequest();
        }
    }
    public function firstStep()
    {
         $service = new ServiceClass()
        return $this->service->doSomething($this->request->test());
    }
}
class ServiceClass
{
    public function doSomenthing(): string
    {
         return "test";
    }
}

Error Expected type 'string'. Found 'array|null'.

Expected behavior The test method in the CLIRequest and HttpRequest should be recognized to return array, null, or string correctly based on the instance type of the $request variable, using union types.

Platform and version VSCode 1.90.0 Intelephense 1.10.14 intelephense.environment.php 8.3