vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Support for Interface/Trait return self type

Open Grldk opened this issue 2 years ago • 2 comments

Describe the bug I suppose this is related to issue 1714, which was fixed recently. We have a similar problem, but are also using an interface. In this case the problem still persists.

To Reproduce

<?php

interface TestInterface
{
    public function test(): self;
}

trait TestTrait
{
    public function test(): self
    {
        return $this;
    }
}

class TestClass implements TestInterface
{
    use TestTrait;

    public function save(): void
    {
    }
}

$test = new TestClass();

$test->test()
    ->save()
;

Screenshots image

Platform and version Using Intelephense v1.10.2

Grldk avatar Dec 21 '23 12:12 Grldk

This is how you can get rid of the error

<?php

declare(strict_types=1);

interface TestInterface
{
	public function test(): static;
}

trait TestTrait
{
	final public function test(): static
	{
		return $this;
	}
}

final class TestClass implements TestInterface
{
	use TestTrait;

	public function save(): void
	{
	}
}

$test = new TestClass();

$test->test()
	->save();

Nikolay-Ivanchenko avatar Dec 22 '23 07:12 Nikolay-Ivanchenko

Thanks for your reply. Unfortunately marking affected classes as final is not an option in all cases. I still think this is a bug.

Grldk avatar Jan 08 '24 12:01 Grldk