Skip to content

Commit 1ad7743

Browse files
authored
Zend: Resolve self and parent types at compile time (php#17755)
This does not apply to traits.
1 parent 15d7b83 commit 1ad7743

20 files changed

+230
-29
lines changed

Zend/tests/magic_methods/magic_methods_021.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Foo2 {
1212
}
1313

1414
class Foo3 {
15-
public static function __set_state(array $data): Foo3|self {}
15+
public static function __set_state(array $data): Foo3|Foo2 {}
1616
}
1717

1818
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
parent type can take part in an intersection type is resolvable at compile time
3+
--FILE--
4+
<?php
5+
6+
class A {}
7+
8+
class B extends A {
9+
public function foo(): parent&Iterator {}
10+
}
11+
12+
?>
13+
DONE
14+
--EXPECT--
15+
DONE
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
--TEST--
2-
parent type cannot take part in an intersection type
2+
parent type cannot take part in an intersection type if not resolvable at compile time
33
--FILE--
44
<?php
55

6-
class A {}
7-
8-
class B extends A {
6+
trait T {
97
public function foo(): parent&Iterator {}
108
}
119

1210
?>
11+
DONE
1312
--EXPECTF--
1413
Fatal error: Type parent cannot be part of an intersection type in %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
parent type cannot take part in an intersection type if not resolvable at compile time
3+
--FILE--
4+
<?php
5+
6+
trait T {
7+
public function foo(): PARENT&Iterator {}
8+
}
9+
10+
?>
11+
DONE
12+
--EXPECTF--
13+
Fatal error: Type PARENT cannot be part of an intersection type in %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
self type can take part in an intersection type is resolvable at compile time
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
public function foo(): self&Iterator {}
8+
}
9+
10+
?>
11+
DONE
12+
--EXPECT--
13+
DONE
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
--TEST--
2-
self type cannot take part in an intersection type
2+
self type cannot take part in an intersection type if not resolvable at compile time
33
--FILE--
44
<?php
55

6-
class A {
6+
trait T {
77
public function foo(): self&Iterator {}
88
}
99

1010
?>
11+
DONE
1112
--EXPECTF--
1213
Fatal error: Type self cannot be part of an intersection type in %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
self type cannot take part in an intersection type if not resolvable at compile time
3+
--FILE--
4+
<?php
5+
6+
trait T {
7+
public function foo(): SELF&Iterator {}
8+
}
9+
10+
?>
11+
DONE
12+
--EXPECTF--
13+
Fatal error: Type SELF cannot be part of an intersection type in %s on line %d
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Duplicate parent type in different cases
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public function method(array $data) {}
8+
}
9+
class Bar extends Foo {
10+
public function method(array $data): parent|PARENT {}
11+
}
12+
13+
?>
14+
--EXPECTF--
15+
Fatal error: Duplicate type Foo is redundant in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Duplicate self type in different cases
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public function method(array $data): self|SELF {}
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Duplicate type Foo is redundant in %s on line %d
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Duplicate parent type
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public function method(array $data) {}
8+
}
9+
class Bar extends Foo {
10+
public function method(array $data): parent|parent {}
11+
}
12+
13+
?>
14+
--EXPECTF--
15+
Fatal error: Duplicate type Foo is redundant in %s on line %d

0 commit comments

Comments
 (0)