forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request php#1 from morrisonlevi/returntypehinting
Added bad examples from RFC
- Loading branch information
Showing
5 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |