Describe the bug
If an interface declares a method with a signature that returns a union value that includes self, then implementing such a method in a trait causes an implementation incompatibility error in classes and enumerations that use such a trait.
To Reproduce
<?php
// For example
interface TestIface {
public function func1(): self|null;
}
enum TestEnum1 implements TestIface {
case Case1;
case Case2;
public function func1(): self|null {
return defined('TEST') ? $this : null;
}
}
class TestClass1 implements TestIface {
public function func1(): self|null {
return defined('TEST') ? $this : null;
}
}
// If we move the func1() method to a trait.
trait TestTrait {
public function func1(): self|null {
return defined('TEST') ? $this : null;
}
}
// The following error occurs:
// In the composition of type 'TestEnum2', methods 'TestTrait::func1()' and 'TestIface::func1()' are not compatible.
enum TestEnum2 implements TestIface { // <--
case Case1;
case Case2;
use TestTrait;
}
class TestClass2 implements TestIface { // <--
use TestTrait;
}
Screenshots
Platform and version
Intelephense pre-release 1.17.4 and release 1.16.5
Visual Studio Code 1.112.0
OS Windows_NT x64 10.0.19044
PHP 8.5
Describe the bug
If an interface declares a method with a signature that returns a union value that includes self, then implementing such a method in a trait causes an implementation incompatibility error in classes and enumerations that use such a trait.
To Reproduce
Screenshots
Platform and version
Intelephense pre-release 1.17.4 and release 1.16.5
Visual Studio Code 1.112.0
OS Windows_NT x64 10.0.19044
PHP 8.5