-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement return types #997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 @@ | ||
--TEST-- | ||
Returned nothing, expected array | ||
|
||
--FILE-- | ||
<?php | ||
function test1() : array { | ||
} | ||
|
||
test1(); | ||
|
||
--EXPECTF-- | ||
Catchable fatal error: Return value of test1() must be of the type array, none returned in %s on line %d |
This file contains hidden or 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,13 @@ | ||
--TEST-- | ||
Returned null, expected array | ||
|
||
--FILE-- | ||
<?php | ||
function test1() : array { | ||
return null; | ||
} | ||
|
||
test1(); | ||
|
||
--EXPECTF-- | ||
Catchable fatal error: Return value of test1() must be of the type array, null returned in %s on line %d |
This file contains hidden or 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,12 @@ | ||
--TEST-- | ||
Returned 1, expected array | ||
|
||
--FILE-- | ||
<?php | ||
function test1() : array { | ||
return 1; | ||
} | ||
test1(); | ||
|
||
--EXPECTF-- | ||
Catchable fatal error: Return value of test1() must be of the type array, integer returned in %s on line %d |
This file contains hidden or 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,13 @@ | ||
--TEST-- | ||
Returned string, expected array | ||
|
||
--FILE-- | ||
<?php | ||
function test1() : array { | ||
return "hello"; | ||
} | ||
|
||
test1(); | ||
|
||
--EXPECTF-- | ||
Catchable fatal error: Return value of test1() must be of the type array, string returned in %s on line %d |
This file contains hidden or 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,18 @@ | ||
--TEST-- | ||
Return value fails inheritance check in method | ||
|
||
--FILE-- | ||
<?php | ||
class foo {} | ||
|
||
class qux { | ||
public function foo() : foo { | ||
return $this; | ||
} | ||
} | ||
|
||
$qux = new qux(); | ||
$qux->foo(); | ||
|
||
--EXPECTF-- | ||
Catchable fatal error: Return value of qux::foo() must be an instance of foo, instance of qux returned in %s on line %d |
This file contains hidden or 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-- | ||
Return type allowed in child when parent does not have return type | ||
|
||
--FILE-- | ||
<?php | ||
class Comment {} | ||
|
||
class CommentsIterator extends ArrayIterator implements Iterator { | ||
function current() : Comment { | ||
return parent::current(); | ||
} | ||
} | ||
|
||
$comments = new CommentsIterator([new Comment]); | ||
foreach ($comments as $comment) { | ||
var_dump($comment); | ||
} | ||
|
||
--EXPECTF-- | ||
object(Comment)#%d (%d) { | ||
} |
This file contains hidden or 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,19 @@ | ||
--TEST-- | ||
Return value is subclass of return type | ||
|
||
--FILE-- | ||
<?php | ||
class foo {} | ||
|
||
class qux extends foo { | ||
public function foo() : foo { | ||
return $this; | ||
} | ||
} | ||
|
||
$qux = new qux(); | ||
var_dump($qux->foo()); | ||
|
||
--EXPECTF-- | ||
object(qux)#%d (%d) { | ||
} |
This file contains hidden or 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-- | ||
Return type covariance in interface implementation | ||
|
||
--FILE-- | ||
<?php | ||
interface foo { | ||
public function bar() : foo; | ||
} | ||
|
||
|
||
class qux implements foo { | ||
public function bar() : qux { | ||
return $this; | ||
} | ||
} | ||
|
||
$qux = new qux(); | ||
var_dump($qux->bar()); | ||
|
||
--EXPECTF-- | ||
Fatal error: Declaration of qux::bar() must be compatible with foo::bar(): foo in %s008.php on line 7 |
This file contains hidden or 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,19 @@ | ||
--TEST-- | ||
Return type covariance error | ||
|
||
--FILE-- | ||
<?php | ||
interface foo { | ||
public function bar() : foo; | ||
} | ||
|
||
interface biz {} | ||
|
||
class qux implements foo { | ||
public function bar() : biz { | ||
return $this; | ||
} | ||
} | ||
|
||
--EXPECTF-- | ||
Fatal error: Declaration of qux::bar() must be compatible with foo::bar(): foo in %s on line %d |
This file contains hidden or 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-- | ||
Returned null, expected array reference | ||
|
||
--FILE-- | ||
<?php | ||
function &foo(array &$in) : array { | ||
return null; | ||
} | ||
|
||
$array = [1, 2, 3]; | ||
var_dump(foo($array)); | ||
|
||
--EXPECTF-- | ||
Catchable fatal error: Return value of foo() must be of the type array, null returned in %s on line %d |
This file contains hidden or 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-- | ||
Function returned callable, expected callable | ||
|
||
--FILE-- | ||
<?php | ||
function foo() : callable { | ||
return function() {}; | ||
} | ||
|
||
var_dump(foo()); | ||
|
||
--EXPECTF-- | ||
object(Closure)#%d (%d) { | ||
} |
This file contains hidden or 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,28 @@ | ||
--TEST-- | ||
Method returned callable, expected callable | ||
|
||
--FILE-- | ||
<?php | ||
class foo { | ||
public function bar() : callable { | ||
$test = "one"; | ||
return function() use($test) : array { | ||
return array($test); | ||
}; | ||
} | ||
} | ||
|
||
$baz = new foo(); | ||
var_dump($baz->bar()); | ||
|
||
--EXPECT-- | ||
object(Closure)#2 (2) { | ||
["static"]=> | ||
array(1) { | ||
["test"]=> | ||
string(3) "one" | ||
} | ||
["this"]=> | ||
object(foo)#1 (0) { | ||
} | ||
} |
This file contains hidden or 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,19 @@ | ||
--TEST-- | ||
Closure inside method returned null, expected array | ||
|
||
--FILE-- | ||
<?php | ||
class foo { | ||
public function bar() : callable { | ||
$test = "one"; | ||
return function() use($test) : array { | ||
return null; | ||
}; | ||
} | ||
} | ||
|
||
$baz = new foo(); | ||
var_dump($func=$baz->bar(), $func()); | ||
|
||
--EXPECTF-- | ||
Catchable fatal error: Return value of foo::{closure}() must be of the type array, null returned in %s on line %d |
This file contains hidden or 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,12 @@ | ||
--TEST-- | ||
Constructors cannot declare a return type | ||
|
||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
function __construct() : Foo {} | ||
} | ||
|
||
--EXPECTF-- | ||
Fatal error: Constructor %s::%s() cannot declare a return type in %s on line %s |
This file contains hidden or 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,24 @@ | ||
--TEST-- | ||
Return types allowed in namespace | ||
|
||
--FILE-- | ||
<?php | ||
|
||
namespace Collections; | ||
|
||
interface Collection { | ||
function values(): Collection; | ||
} | ||
|
||
class Vector implements Collection { | ||
function values(): Collection { | ||
return $this; | ||
} | ||
} | ||
|
||
$v = new Vector; | ||
var_dump($v->values()); | ||
|
||
--EXPECTF-- | ||
object(Collections\Vector)#%d (%d) { | ||
} |
This file contains hidden or 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,20 @@ | ||
--TEST-- | ||
Fully qualified classes are allowed in return types | ||
|
||
--FILE-- | ||
<?php | ||
|
||
namespace Collections; | ||
|
||
class Foo { | ||
function foo(\Iterator $i): \Iterator { | ||
return $i; | ||
} | ||
} | ||
|
||
$foo = new Foo; | ||
var_dump($foo->foo(new \EmptyIterator())); | ||
|
||
--EXPECTF-- | ||
object(EmptyIterator)#%d (0) { | ||
} |
This file contains hidden or 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,24 @@ | ||
--TEST-- | ||
Fully qualified classes in trait return types | ||
|
||
--FILE-- | ||
<?php | ||
|
||
namespace FooSpace; | ||
|
||
trait Fooable { | ||
function foo(): \Iterator { | ||
return new \EmptyIterator(); | ||
} | ||
} | ||
|
||
class Foo { | ||
use Fooable; | ||
} | ||
|
||
$foo = new Foo; | ||
var_dump($foo->foo([])); | ||
|
||
--EXPECTF-- | ||
object(EmptyIterator)#%d (%d) { | ||
} |
This file contains hidden or 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,12 @@ | ||
--TEST-- | ||
Destructors cannot declare a return type | ||
|
||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
function __destruct() : Foo {} | ||
} | ||
|
||
--EXPECTF-- | ||
Fatal error: Destructor %s::%s() cannot declare a return type in %s on line %s |
This file contains hidden or 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,12 @@ | ||
--TEST-- | ||
__clone cannot declare a return type | ||
|
||
--FILE-- | ||
<?php | ||
|
||
class Foo { | ||
function __clone() : Foo {} | ||
} | ||
|
||
--EXPECTF-- | ||
Fatal error: %s::%s() cannot declare a return type in %s on line %s |
This file contains hidden or 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,17 @@ | ||
--TEST-- | ||
Exception thrown from function with return type | ||
|
||
--FILE-- | ||
<?php | ||
function test() : array { | ||
throw new Exception(); | ||
} | ||
|
||
test(); | ||
|
||
--EXPECTF-- | ||
Fatal error: Uncaught exception 'Exception' in %s:%d | ||
Stack trace: | ||
#0 %s(%d): test() | ||
#1 {main} | ||
thrown in %s on line %d |
This file contains hidden or 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-- | ||
Return type allows self | ||
|
||
--FILE-- | ||
<?php | ||
class Foo { | ||
public static function getInstance() : self { | ||
return new static(); | ||
} | ||
} | ||
|
||
class Bar extends Foo {} | ||
|
||
var_dump(Foo::getInstance()); | ||
var_dump(Bar::getInstance()); | ||
|
||
--EXPECTF-- | ||
object(Foo)#%d (%d) { | ||
} | ||
object(Bar)#%d (%d) { | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong test name, copied from 011.phpt