Skip to content

Commit 9b0d73a

Browse files
author
Stefan Marr
committed
Added missing consistency check for abstract methods required by one trait and implemented by another.
1 parent ceac9dc commit 9b0d73a

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
The compatibility with the signature of abstract methods should be checked.
3+
--FILE--
4+
<?php
5+
error_reporting(E_ALL);
6+
7+
trait THelloB {
8+
public function hello() {
9+
echo 'Hello';
10+
}
11+
}
12+
13+
trait THelloA {
14+
public abstract function hello($a);
15+
}
16+
17+
class TraitsTest1 {
18+
use THelloB;
19+
use THelloA;
20+
}
21+
22+
23+
?>
24+
--EXPECTF--
25+
Fatal error: Declaration of THelloB::hello() must be compatible with THelloA::hello($a) in %s on line %d
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
The compatibility with the signature of abstract methods should be checked. (also checking the second possible implementation branch)
3+
--FILE--
4+
<?php
5+
error_reporting(E_ALL);
6+
7+
trait THelloB {
8+
public function hello() {
9+
echo 'Hello';
10+
}
11+
}
12+
13+
trait THelloA {
14+
public abstract function hello($a);
15+
}
16+
17+
class TraitsTest1 {
18+
use THelloA;
19+
use THelloB;
20+
}
21+
22+
23+
24+
?>
25+
--EXPECTF--
26+
Fatal error: Declaration of THelloB::hello() must be compatible with THelloA::hello($a) in %s on line %d

Zend/zend_compile.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3616,13 +3616,19 @@ static int zend_traits_merge_functions(zend_function *fn TSRMLS_DC, int num_args
36163616
if (zend_hash_quick_find(function_tables[i], hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void **)&other_trait_fn) == SUCCESS) {
36173617
/* if it is an abstract method, there is no collision */
36183618
if (other_trait_fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
3619+
/* Make sure they are compatible */
3620+
do_inheritance_check_on_method(fn, other_trait_fn TSRMLS_CC);
3621+
36193622
/* we can savely free and remove it from other table */
36203623
zend_function_dtor(other_trait_fn);
36213624
zend_hash_quick_del(function_tables[i], hash_key->arKey, hash_key->nKeyLength, hash_key->h);
36223625
} else {
36233626
/* if it is not an abstract method, there is still no collision */
36243627
/* if fn is an abstract method */
36253628
if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
3629+
/* Make sure they are compatible */
3630+
do_inheritance_check_on_method(other_trait_fn, fn TSRMLS_CC);
3631+
36263632
/* just mark as solved, will be added if its own trait is processed */
36273633
abstract_solved = 1;
36283634
} else {

0 commit comments

Comments
 (0)