Skip to content

Trait and interfaceface methods are not compatible. #3594

@dlsrc

Description

@dlsrc

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

Image

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions