Skip to content

Commit 5c437b9

Browse files
committed
Expose tentative return types to userland
1 parent 5530f05 commit 5c437b9

17 files changed

+378
-9
lines changed

Zend/tests/type_declarations/variance/internal_parent/compatible_return_type.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test that no notice is emitted when the return type/value of the overriding method is compatible with the optional return type/value of the overridden method
2+
Test that no notice is emitted when the return type/value of the overriding method is compatible with the tentative return type/value of the overridden method
33
--FILE--
44
<?php
55
class MyDateTimeZone extends DateTimeZone

Zend/tests/type_declarations/variance/internal_parent/incompatible_return_type.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test that a notice is emitted when the return type/value of the overriding method is incompatible with the optional return type/value of the overridden method
2+
Test that a notice is emitted when the return type/value of the overriding method is incompatible with the tentative return type/value of the overridden method
33
--FILE--
44
<?php
55
class MyDateTimeZone extends DateTimeZone

Zend/tests/type_declarations/variance/internal_parent/missing_return_type.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test that a notice is emitted when the optional return type of the overridden method is omitted
2+
Test that a notice is emitted when the tentative return type of the overridden method is omitted
33
--FILE--
44
<?php
55
class MyDateTimeZone extends DateTimeZone
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test that a notice is emitted when the return type/value of the overriding method is incompatible with the tentative return type/value of the overridden method
3+
--FILE--
4+
<?php
5+
class Foo
6+
{
7+
#[TentativeReturnType]
8+
public function x(): string|false
9+
{
10+
return "x";
11+
}
12+
}
13+
14+
class Bar extends Foo
15+
{
16+
public function x(): array
17+
{
18+
return [];
19+
}
20+
}
21+
22+
$bar = new Bar();
23+
var_dump($bar->x());
24+
?>
25+
--EXPECTF--
26+
Strict Standards: Declaration of Bar::x(): array should be compatible with Foo::x(): string|false in %s on line %d
27+
array(0) {
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test that a notice is emitted when the return type/value of the overriding method is incompatible with the tentative return type/value of the overridden method
3+
--FILE--
4+
<?php
5+
class Foo
6+
{
7+
#[TentativeReturnType]
8+
public function x(): string|false
9+
{
10+
return "x";
11+
}
12+
}
13+
14+
class Bar extends Foo
15+
{
16+
public function x()
17+
{
18+
return [];
19+
}
20+
}
21+
22+
$bar = new Bar();
23+
var_dump($bar->x());
24+
?>
25+
--EXPECTF--
26+
Strict Standards: Declaration of Bar::x() should be compatible with Foo::x(): string|false in %s on line %d
27+
array(0) {
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Test that the TentativeReturnType attribute is not inherited
3+
--FILE--
4+
<?php
5+
class Foo
6+
{
7+
#[TentativeReturnType]
8+
public function x(): string|false
9+
{
10+
return "x";
11+
}
12+
}
13+
14+
class Bar extends Foo
15+
{
16+
public function x(): array
17+
{
18+
return [];
19+
}
20+
}
21+
22+
class Baz extends Bar
23+
{
24+
public function x()
25+
{
26+
return [];
27+
}
28+
}
29+
30+
?>
31+
--EXPECTF--
32+
Strict Standards: Declaration of Bar::x(): array should be compatible with Foo::x(): string|false in %s on line %d
33+
34+
Fatal error: Declaration of Baz::x() must be compatible with Bar::x(): array in %s on line %d
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Test that the TentativeReturnType attribute doesn't suppress type errors
3+
--FILE--
4+
<?php
5+
class Foo
6+
{
7+
public function x(): string
8+
{
9+
return "x";
10+
}
11+
}
12+
13+
class Bar extends Foo
14+
{
15+
#[TentativeReturnType]
16+
public function x(): array
17+
{
18+
return [];
19+
}
20+
}
21+
22+
?>
23+
--EXPECTF--
24+
Fatal error: Declaration of Bar::x(): array must be compatible with Foo::x(): string in %s on line %d
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that a compile error is emitted if the TentativeReturnType attribute is used when the method doesn't have a return type
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
#[TentativeReturnType]
9+
public function x()
10+
{
11+
return "x";
12+
}
13+
}
14+
15+
?>
16+
--EXPECTF--
17+
Fatal error: TentativeReturnType attribute cannot be used when a method doesn't have any return type in %s on line %d
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test that the notice can be suppressed when the return type/value of the overriding method is incompatible with the tentative return type/value of the overridden method
3+
--FILE--
4+
<?php
5+
class Foo
6+
{
7+
#[TentativeReturnType]
8+
public function x(): string|false
9+
{
10+
return "x";
11+
}
12+
}
13+
14+
class Bar extends Foo
15+
{
16+
#[SuppressTentativeReturnTypeNotice]
17+
public function x(): array
18+
{
19+
return [];
20+
}
21+
}
22+
23+
$bar = new Bar();
24+
var_dump($bar->x());
25+
?>
26+
--EXPECTF--
27+
array(0) {
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test that the notice can be suppressed when the return type/value of the overriding method is incompatible with the tentative return type/value of the overridden method
3+
--FILE--
4+
<?php
5+
class Foo
6+
{
7+
#[TentativeReturnType]
8+
public function x(): string|false
9+
{
10+
return "x";
11+
}
12+
}
13+
14+
class Bar extends Foo
15+
{
16+
#[SuppressTentativeReturnTypeNotice]
17+
public function x()
18+
{
19+
return [];
20+
}
21+
}
22+
23+
$bar = new Bar();
24+
var_dump($bar->x());
25+
?>
26+
--EXPECTF--
27+
array(0) {
28+
}

0 commit comments

Comments
 (0)