Skip to content

Commit 2702f1a

Browse files
committed
Add experimental list<T> type declarations
1 parent 0d9ff00 commit 2702f1a

24 files changed

Lines changed: 348 additions & 13 deletions
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
nullable list<T> parameter accepts null and matching object list
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
function f(?list<User> $users): void {
8+
echo $users === null ? "null\n" : count($users) . "\n";
9+
}
10+
11+
f(null);
12+
f([new User]);
13+
?>
14+
--EXPECT--
15+
null
16+
1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
list<T> parameter accepts matching object list
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
function f(list<User> $users): void {
8+
echo count($users), "\n";
9+
}
10+
11+
f([new User, new User]);
12+
?>
13+
--EXPECT--
14+
2
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
array parameter type is contravariant with list<T>
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
class A {
8+
public function setItems(list<User> $items): void {
9+
echo "A\n";
10+
}
11+
}
12+
13+
class B extends A {
14+
public function setItems(array $items): void {
15+
echo count($items), "\n";
16+
}
17+
}
18+
19+
(new B)->setItems([new User]);
20+
?>
21+
--EXPECT--
22+
1
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
list<T> parameter rejects associative array
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
function f(list<User> $users): void {
8+
echo "inside\n";
9+
}
10+
11+
try {
12+
f(['x' => new User]);
13+
} catch (TypeError $e) {
14+
echo "TypeError\n";
15+
}
16+
?>
17+
--EXPECT--
18+
TypeError
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
list<T> parameter rejects non-matching object element
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
function f(list<User> $users): void {
8+
echo "inside\n";
9+
}
10+
11+
try {
12+
f([new stdClass]);
13+
} catch (TypeError $e) {
14+
echo "TypeError\n";
15+
}
16+
?>
17+
--EXPECT--
18+
TypeError
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
list<T> property accepts matching object list and rejects bad element
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
class Box {
8+
public list<User> $users;
9+
}
10+
11+
$box = new Box;
12+
$box->users = [new User, new User];
13+
echo count($box->users), "\n";
14+
15+
try {
16+
$box->users = [new stdClass];
17+
} catch (TypeError $e) {
18+
echo "TypeError\n";
19+
}
20+
?>
21+
--EXPECT--
22+
2
23+
TypeError
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
nullable list<T> property accepts null and matching object list
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
class Box {
8+
public ?list<User> $users = null;
9+
}
10+
11+
$box = new Box;
12+
var_dump($box->users);
13+
$box->users = [new User];
14+
echo count($box->users), "\n";
15+
?>
16+
--EXPECT--
17+
NULL
18+
1
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
list<T> property reflection string
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
class Box {
8+
public list<User> $users;
9+
}
10+
11+
$prop = new ReflectionProperty(Box::class, 'users');
12+
echo (string) $prop->getType(), "\n";
13+
?>
14+
--EXPECT--
15+
list<User>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
list<T> parameter reflection string
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
function f(list<User> $users): void {}
8+
9+
$param = (new ReflectionFunction('f'))->getParameters()[0];
10+
echo (string) $param->getType(), "\n";
11+
?>
12+
--EXPECT--
13+
list<User>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
list<T> return type accepts matching object list
3+
--FILE--
4+
<?php
5+
class User {}
6+
7+
function f(): list<User> {
8+
return [new User, new User];
9+
}
10+
11+
echo count(f()), "\n";
12+
?>
13+
--EXPECT--
14+
2

0 commit comments

Comments
 (0)