Skip to content

Commit

Permalink
Merge pull request php#1 from morrisonlevi/returntypehinting
Browse files Browse the repository at this point in the history
Added bad examples from RFC
  • Loading branch information
krakjoe committed Apr 17, 2014
2 parents 507e38f + 4ef1e42 commit fb9f51b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Zend/tests/return_hint/rfc001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Description: returned type does not match the type declaration

--FILE--
<?php

function get_config(): array {
return 42;
}


--EXPECTF--
Fatal error: the function get_config was expected to return an array and returned an integer in %s on line 4



14 changes: 14 additions & 0 deletions Zend/tests/return_hint/rfc002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Int is not a valid type declaration

--FILE--
<?php

function answer(): int {
return 42;
}

--EXPECTF--
Fatal error: the function answer was expected to return an object of class int and returned an integer in %s on line 4


14 changes: 14 additions & 0 deletions Zend/tests/return_hint/rfc003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Cannot return null with a return type declaration

--FILE--
<?php

function foo(): bar {
return null;
}

--EXPECTF--
Fatal error: the function foo was expected to return an object of class bar and returned null in %s on line 4


21 changes: 21 additions & 0 deletions Zend/tests/return_hint/rfc004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Missing return type on override

--FILE--
<?php

class User {}

interface UserGateway {
function find($id) : User;
}

class UserGateway_MySql implements UserGateway {
// must return User or subtype of User
function find($id) {
return new User;
}
}

--EXPECTF--
Fatal error: Delcaration of UserGateway_MySql::find should be compatible with UserGateway::find($id) : User, return type missing in %s on line 9
16 changes: 16 additions & 0 deletions Zend/tests/return_hint/rfc005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Cannot define a return type on a Generator

--FILE--
<?php

function filter(Traversable $in, callable $filter): array {
foreach ($in as $key => $value) {
if ($filter($key, $value)) {
yield $key => $value;
}
}
}

--EXPECTF--
Fatal error: The "yield" expression can not be used inside a function with a return type hint in %s on line 6

0 comments on commit fb9f51b

Please sign in to comment.