Skip to content

Commit 3fcf6ff

Browse files
committed
Merge branch 'PHP-8.3'
* PHP-8.3: Fix prototype for trait methods (#14148)
2 parents 79af2ad + c42f48d commit 3fcf6ff

7 files changed

+248
-69
lines changed

Zend/tests/gh14009_001.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
GH-14009: Traits inherit prototype
3+
--FILE--
4+
<?php
5+
6+
class P {
7+
protected function common() {
8+
throw new Exception('Unreachable');
9+
}
10+
}
11+
12+
class A extends P {
13+
public function test(P $sibling) {
14+
$sibling->common();
15+
}
16+
}
17+
18+
class B extends P {
19+
protected function common() {
20+
echo __METHOD__, "\n";
21+
}
22+
}
23+
24+
trait T {
25+
protected function common() {
26+
echo __METHOD__, "\n";
27+
}
28+
}
29+
30+
class C extends P {
31+
use T;
32+
}
33+
34+
$a = new A();
35+
$a->test(new B());
36+
$a->test(new C());
37+
38+
?>
39+
--EXPECT--
40+
B::common
41+
T::common

Zend/tests/gh14009_002.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-14009: Traits inherit prototype
3+
--FILE--
4+
<?php
5+
6+
class P {
7+
protected function common() {}
8+
}
9+
10+
class A extends P {}
11+
12+
trait T {
13+
private abstract function common(int $param);
14+
}
15+
16+
class B extends P {
17+
use T;
18+
}
19+
20+
?>
21+
--EXPECTF--
22+
Fatal error: Declaration of P::common() must be compatible with T::common(int $param) in %s on line %d

Zend/tests/gh14009_003.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-14009: Traits inherit prototype
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
private function test() {}
8+
}
9+
10+
trait T {
11+
private function test() {}
12+
}
13+
14+
class B extends A {
15+
use T;
16+
}
17+
18+
?>
19+
--EXPECT--

Zend/tests/gh14009_004.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
GH-14009: Traits inherit prototype
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
protected function test() {
8+
throw new Exception('Unreachable');
9+
}
10+
}
11+
12+
class B extends A {
13+
// Prototype is A::test().
14+
protected function test() {
15+
echo __METHOD__, "\n";
16+
}
17+
}
18+
19+
trait T {
20+
protected abstract function test();
21+
}
22+
23+
class C extends B {
24+
use T;
25+
}
26+
27+
class D extends A {
28+
public static function callTest($sibling) {
29+
$sibling->test();
30+
}
31+
}
32+
33+
D::callTest(new C());
34+
35+
?>
36+
--EXPECT--
37+
B::test
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Interfaces don't set prototypes to their parent method
3+
--FILE--
4+
<?php
5+
6+
interface A {
7+
public function __construct(int $param);
8+
}
9+
interface B extends A {
10+
public function __construct(int|float $param);
11+
}
12+
class Test implements B {
13+
public function __construct(int $param) {}
14+
}
15+
new Test(42);
16+
17+
?>
18+
--EXPECTF--
19+
Fatal error: Declaration of Test::__construct(int $param) must be compatible with B::__construct(int|float $param) 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+
Interfaces don't set prototypes to their parent method
3+
--XFAIL--
4+
X::__constructor()'s prototype is set to B::__construct(). Y::__construct() then
5+
uses prototype to verify LSP, but misses A::__construct() which has a stricter
6+
signature.
7+
--FILE--
8+
<?php
9+
10+
interface A {
11+
public function __construct(int|float $param);
12+
}
13+
interface B {
14+
public function __construct(int $param);
15+
}
16+
class X implements A, B {
17+
public function __construct(int|float $param) {}
18+
}
19+
class Y extends X {
20+
public function __construct(int $param) {}
21+
}
22+
new Y(42);
23+
24+
?>
25+
--EXPECTF--
26+
Fatal error: Declaration of Y::__construct(int $param) must be compatible with A::__construct(int|float $param) in %s on line %d

0 commit comments

Comments
 (0)