marcodl
marcodl

Reputation: 61

PhpStorm false inspection error by using self type hint

the inspector in PhpStorm gives me an error even though the code is valid


interface PhoneInterface
{
    public function add(): self;
}

trait PhoneTrait
{
    public function add(): self
    {
        return $this;
    }
}

class A implements PhoneInterface
{
    use PhoneTrait;
}

var_dump(new A());

I get the error:

"Declaration of 'PhoneTrait->add()' must be compatible with 'PhoneInterface->add()'"

But since PHP 7.4 this is valid. In the "Settings -> PHP level" I tried 7.4 and 8.0 but both gives me an error.

The error goes away if I remove the :self typehint. The same code works here https://3v4l.org/SJCf2 and on my Server.

Upvotes: 0

Views: 44

Answers (1)

Eugene Morozov
Eugene Morozov

Reputation: 3043

It's a corner case that hasn't been fixed along with the general case.

Upvotes: 1

Related Questions