Skip to content

Return types #820

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Zend/tests/return_types/001.phpt
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: The function test1 was expected to return an array and returned nothing in %s on line %d
13 changes: 13 additions & 0 deletions Zend/tests/return_types/002.phpt
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: The function test1 was expected to return an array and returned null in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/return_types/003.phpt
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: The function test1 was expected to return an array and returned an integer in %s on line %d
13 changes: 13 additions & 0 deletions Zend/tests/return_types/004.phpt
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: The function test1 was expected to return an array and returned a string in %s on line %d
18 changes: 18 additions & 0 deletions Zend/tests/return_types/005.phpt
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: The function qux::foo was expected to return an object of class foo and returned an object of class qux in %s on line %d
21 changes: 21 additions & 0 deletions Zend/tests/return_types/006.phpt
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) {
}
19 changes: 19 additions & 0 deletions Zend/tests/return_types/007.phpt
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) {
}
22 changes: 22 additions & 0 deletions Zend/tests/return_types/008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--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--
object(qux)#%d (%d) {
}
19 changes: 19 additions & 0 deletions Zend/tests/return_types/009.phpt
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
14 changes: 14 additions & 0 deletions Zend/tests/return_types/010.phpt
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: The function foo was expected to return an array and returned null in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/return_types/011.phpt
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) {
}
28 changes: 28 additions & 0 deletions Zend/tests/return_types/012.phpt
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) {
}
}
19 changes: 19 additions & 0 deletions Zend/tests/return_types/013.phpt
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: The function %s was expected to return an array and returned null in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/return_types/014.phpt
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
24 changes: 24 additions & 0 deletions Zend/tests/return_types/015.phpt
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) {
}
20 changes: 20 additions & 0 deletions Zend/tests/return_types/016.phpt
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) {
}
24 changes: 24 additions & 0 deletions Zend/tests/return_types/017.phpt
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) {
}
12 changes: 12 additions & 0 deletions Zend/tests/return_types/018.phpt
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
12 changes: 12 additions & 0 deletions Zend/tests/return_types/019.phpt
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
17 changes: 17 additions & 0 deletions Zend/tests/return_types/020.phpt
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
17 changes: 17 additions & 0 deletions Zend/tests/return_types/022.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Hint on closure with lexical vars

--FILE--
<?php
$foo = "bar";
$test = function() use($foo) : Closure {
return function() use ($foo) {
return $foo;
};
};

$callable = $test();
var_dump($callable());

--EXPECTF--
string(3) "bar"
Loading