Skip to content

Commit 4ef1e42

Browse files
committed
Added bad examples from RFC
1 parent db8d8e2 commit 4ef1e42

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

Zend/tests/return_hint/rfc001.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Description: returned type does not match the type declaration
3+
4+
--FILE--
5+
<?php
6+
7+
function get_config(): array {
8+
return 42;
9+
}
10+
11+
12+
--EXPECTF--
13+
Fatal error: the function get_config was expected to return an array and returned an integer in %s on line 4
14+
15+
16+

Zend/tests/return_hint/rfc002.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Int is not a valid type declaration
3+
4+
--FILE--
5+
<?php
6+
7+
function answer(): int {
8+
return 42;
9+
}
10+
11+
--EXPECTF--
12+
Fatal error: the function answer was expected to return an object of class int and returned an integer in %s on line 4
13+
14+

Zend/tests/return_hint/rfc003.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Cannot return null with a return type declaration
3+
4+
--FILE--
5+
<?php
6+
7+
function foo(): bar {
8+
return null;
9+
}
10+
11+
--EXPECTF--
12+
Fatal error: the function foo was expected to return an object of class bar and returned null in %s on line 4
13+
14+

Zend/tests/return_hint/rfc004.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Missing return type on override
3+
4+
--FILE--
5+
<?php
6+
7+
class User {}
8+
9+
interface UserGateway {
10+
function find($id) : User;
11+
}
12+
13+
class UserGateway_MySql implements UserGateway {
14+
// must return User or subtype of User
15+
function find($id) {
16+
return new User;
17+
}
18+
}
19+
20+
--EXPECTF--
21+
Fatal error: Delcaration of UserGateway_MySql::find should be compatible with UserGateway::find($id) : User, return type missing in %s on line 9

Zend/tests/return_hint/rfc005.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Cannot define a return type on a Generator
3+
4+
--FILE--
5+
<?php
6+
7+
function filter(Traversable $in, callable $filter): array {
8+
foreach ($in as $key => $value) {
9+
if ($filter($key, $value)) {
10+
yield $key => $value;
11+
}
12+
}
13+
}
14+
15+
--EXPECTF--
16+
Fatal error: The "yield" expression can not be used inside a function with a return type hint in %s on line 6

0 commit comments

Comments
 (0)