Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Zend/tests/interfaces/gh25759.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
GH-25759: Interface method compatibility is independent of extends order
--FILE--
<?php
interface HasOptionalValue
{
public function getValue(): ?int;
}

interface HasValue
{
public function getValue(): int;
}

interface OptionalValueFirst extends HasOptionalValue, HasValue
{
}

interface ValueFirst extends HasValue, HasOptionalValue
{
}

class OptionalValueFirstImplementation implements OptionalValueFirst
{
public function getValue(): int
{
return 42;
}
}

class ValueFirstImplementation implements ValueFirst
{
public function getValue(): int
{
return 42;
}
}

var_dump((new OptionalValueFirstImplementation())->getValue());
var_dump((new ValueFirstImplementation())->getValue());
?>
--EXPECT--
int(42)
int(42)
28 changes: 28 additions & 0 deletions Zend/tests/interfaces/gh25759_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
GH-25759: Interface method merging retains the most restrictive return type
--FILE--
<?php
interface HasOptionalValue
{
public function getValue(): ?int;
}

interface HasValue
{
public function getValue(): int;
}

interface Result extends HasOptionalValue, HasValue
{
}

class InvalidImplementation implements Result
{
public function getValue(): ?int
{
return null;
}
}
?>
--EXPECTF--
Fatal error: Declaration of InvalidImplementation::getValue(): ?int must be compatible with HasValue::getValue(): int in %s on line %d
13 changes: 13 additions & 0 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,19 @@ static void do_inherit_method(zend_string *key, zend_function *parent, zend_clas
return;
}

if (is_interface && (ce->ce_flags & ZEND_ACC_INTERFACE)
&& func->common.scope != ce
&& (func->common.fn_flags & ZEND_ACC_ABSTRACT)
&& (parent->common.fn_flags & ZEND_ACC_ABSTRACT)
&& (func->common.fn_flags & ZEND_ACC_STATIC) == (parent->common.fn_flags & ZEND_ACC_STATIC)
&& zend_do_perform_implementation_check(
parent, parent->common.scope, func, func->common.scope) == INHERITANCE_SUCCESS
&& zend_do_perform_implementation_check(
func, func->common.scope, parent, parent->common.scope) != INHERITANCE_SUCCESS) {
Z_PTR_P(child) = zend_duplicate_function(parent, ce);
return;
}

do_inheritance_check_on_method(
func, func->common.scope, parent, parent->common.scope, ce, child, flags);
} else {
Expand Down
Loading