Skip to content

Check abstract method signatures coming from traits #5068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
18 changes: 18 additions & 0 deletions Zend/tests/traits/abstract_method_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Abstract method from trait enforced in class
--FILE--
<?php

trait T {
abstract public function neededByTheTrait(int $a, string $b);
}

class C {
use T;

public function neededByTheTrait(array $a, object $b) {}
}

?>
--EXPECTF--
Fatal error: Declaration of C::neededByTheTrait(array $a, object $b) must be compatible with T::neededByTheTrait(int $a, string $b) in %s on line %d
22 changes: 22 additions & 0 deletions Zend/tests/traits/abstract_method_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Mutually incompatible methods from traits are fine as long as the final method is compatible
--FILE--
<?php

trait T1 {
abstract public function test();
}
trait T2 {
abstract public function test(): int;
}

class C {
use T1, T2;

public function test(): int {}
}

?>
===DONE===
--EXPECT--
===DONE===
18 changes: 18 additions & 0 deletions Zend/tests/traits/abstract_method_3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Private abstract method from trait enforced in class
--FILE--
<?php

trait T {
abstract private function neededByTheTrait(int $a, string $b);
}

class C {
use T;

private function neededByTheTrait(array $a, object $b) {}
}

?>
--EXPECTF--
Fatal error: Declaration of C::neededByTheTrait(array $a, object $b) must be compatible with T::neededByTheTrait(int $a, string $b) in %s on line %d
20 changes: 20 additions & 0 deletions Zend/tests/traits/abstract_method_4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Visibility enforcement on abstract trait methods
--FILE--
<?php

trait T {
abstract public function method(int $a, string $b);
}

class C {
use T;

/* For backwards-compatiblity reasons, visibility is not enforced here. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* For backwards-compatiblity reasons, visibility is not enforced here. */
/* For backwards-compatibility reasons, visibility is not enforced here. */

private function method(int $a, string $b) {}
}

?>
===DONE===
--EXPECT--
===DONE===
18 changes: 18 additions & 0 deletions Zend/tests/traits/abstract_method_5.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Staticness enforcement on abstract trait methods
--FILE--
<?php

trait T {
abstract static public function method(int $a, string $b);
}

class C {
use T;

public function method(int $a, string $b) {}
}

?>
--EXPECTF--
Fatal error: Cannot make static method T::method() non static in class C in %s on line %d
20 changes: 20 additions & 0 deletions Zend/tests/traits/abstract_method_6.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Abstract private trait method not implemented
--FILE--
<?php

trait T {
abstract private function method(int $a, string $b);
}

abstract class C {
use T;
}

class D extends C {
private function method(int $a, string $b) {}
}

?>
--EXPECTF--
Fatal error: Class C must implement 1 abstract private method (C::method) in %s on line %d
23 changes: 23 additions & 0 deletions Zend/tests/traits/abstract_method_7.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Abstract private trait method forwarded as abstract protected method
--FILE--
<?php

trait T {
abstract private function method(int $a, string $b);
}

abstract class C {
use T;

abstract protected function method(int $a, string $b);
}

class D extends C {
protected function method(int $a, string $b) {}
}

?>
===DONE===
--EXPECT--
===DONE===
2 changes: 1 addition & 1 deletion Zend/tests/traits/bug60217b.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ class CBroken {
$o = new CBroken;
$o->foo(1);
--EXPECTF--
Fatal error: Declaration of TBroken1::foo($a) must be compatible with TBroken2::foo($a, $b = 0) in %s
Fatal error: Declaration of CBroken::foo($a) must be compatible with TBroken2::foo($a, $b = 0) in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/traits/bug60217c.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ class CBroken {
$o = new CBroken;
$o->foo(1);
--EXPECTF--
Fatal error: Declaration of TBroken2::foo($a) must be compatible with TBroken1::foo($a, $b = 0) in %s on line %d
Fatal error: Declaration of CBroken::foo($a) must be compatible with TBroken1::foo($a, $b = 0) in %s on line %d
2 changes: 1 addition & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6101,7 +6101,7 @@ void zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_boo
}

if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
if (op_array->fn_flags & ZEND_ACC_PRIVATE) {
if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
}
Expand Down
Loading