Skip to content

Commit 0ae6a67

Browse files
authored
Add true as a type (#8326)
RFC: https://wiki.php.net/rfc/true-type
1 parent ddc0b49 commit 0ae6a67

19 files changed

+273
-14
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
true type cannot take part in an intersection type
3+
--FILE--
4+
<?php
5+
6+
function foo(): true&Iterator {}
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Type true cannot be part of an intersection 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+
No coercion should be applied to type false
3+
--FILE--
4+
<?php
5+
6+
function test(false $v) { var_dump($v); }
7+
8+
try {
9+
test(0);
10+
} catch (\TypeError $e) {
11+
echo $e->getMessage(), \PHP_EOL;
12+
}
13+
try {
14+
test('');
15+
} catch (\TypeError $e) {
16+
echo $e->getMessage(), \PHP_EOL;
17+
}
18+
try {
19+
test([]);
20+
} catch (\TypeError $e) {
21+
echo $e->getMessage(), \PHP_EOL;
22+
}
23+
24+
?>
25+
--EXPECTF--
26+
test(): Argument #1 ($v) must be of type false, int given, called in %s on line %d
27+
test(): Argument #1 ($v) must be of type false, string given, called in %s on line %d
28+
test(): Argument #1 ($v) must be of type false, array given, called in %s on line %d
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
No coercion should be applied to type false even if it's an overide
3+
--FILE--
4+
<?php
5+
6+
class P {
7+
public function foo($v): array|bool {
8+
return $v;
9+
}
10+
}
11+
12+
class C {
13+
public function foo($v): array|false {
14+
return $v;
15+
}
16+
}
17+
18+
$p = new P();
19+
$c = new C();
20+
21+
var_dump($p->foo(0));
22+
try {
23+
var_dump($c->foo(0));
24+
} catch (\TypeError $e) {
25+
echo $e->getMessage(), \PHP_EOL;
26+
}
27+
28+
?>
29+
--EXPECT--
30+
bool(false)
31+
C::foo(): Return value must be of type array|false, int returned
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
False can be used as a standalone type
3+
--FILE--
4+
<?php
5+
6+
function test(false $v): false {
7+
return $v;
8+
}
9+
10+
var_dump(test(false));
11+
12+
?>
13+
--EXPECT--
14+
bool(false)

Zend/tests/type_declarations/union_types/standalone_false_implicit_nullability.phpt renamed to Zend/tests/type_declarations/literal_types/false_standalone_implicit_nullability.phpt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ False can be used as a standalone type even with implicit nullability
33
--FILE--
44
<?php
55

6-
function test(false $v = null) {}
6+
function test(false $v = null) { return $v; }
77

8+
var_dump(test(false));
9+
var_dump(test(null));
810
?>
9-
===DONE===
1011
--EXPECT--
11-
===DONE===
12+
bool(false)
13+
NULL
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
No coercion should be applied to type true
3+
--FILE--
4+
<?php
5+
6+
function test(true $v) { var_dump($v); }
7+
8+
try {
9+
test(1);
10+
} catch (\TypeError $e) {
11+
echo $e->getMessage(), \PHP_EOL;
12+
}
13+
try {
14+
test('1');
15+
} catch (\TypeError $e) {
16+
echo $e->getMessage(), \PHP_EOL;
17+
}
18+
try {
19+
test([1]);
20+
} catch (\TypeError $e) {
21+
echo $e->getMessage(), \PHP_EOL;
22+
}
23+
try {
24+
test(new stdClass());
25+
} catch (\TypeError $e) {
26+
echo $e->getMessage(), \PHP_EOL;
27+
}
28+
29+
?>
30+
--EXPECTF--
31+
test(): Argument #1 ($v) must be of type true, int given, called in %s on line %d
32+
test(): Argument #1 ($v) must be of type true, string given, called in %s on line %d
33+
test(): Argument #1 ($v) must be of type true, array given, called in %s on line %d
34+
test(): Argument #1 ($v) must be of type true, stdClass given, called in %s on line %d
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
No coercion should be applied to type true even if it's an overide
3+
--FILE--
4+
<?php
5+
6+
class P {
7+
public function foo($v): array|bool {
8+
return $v;
9+
}
10+
}
11+
12+
class C {
13+
public function foo($v): array|true {
14+
return $v;
15+
}
16+
}
17+
18+
$p = new P();
19+
$c = new C();
20+
21+
var_dump($p->foo(1));
22+
try {
23+
var_dump($c->foo(1));
24+
} catch (\TypeError $e) {
25+
echo $e->getMessage(), \PHP_EOL;
26+
}
27+
28+
?>
29+
--EXPECT--
30+
bool(true)
31+
C::foo(): Return value must be of type array|true, int returned
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
true can be used as a standalone type
3+
--FILE--
4+
<?php
5+
6+
function test(true $v): true {
7+
return $v;
8+
}
9+
10+
var_dump(test(true));
11+
12+
?>
13+
--EXPECT--
14+
bool(true)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
true can be used as a standalone type even with implicit nullability
3+
--FILE--
4+
<?php
5+
6+
function test(true $v = null) { return $v; }
7+
8+
var_dump(test(true));
9+
var_dump(test(null));
10+
?>
11+
--EXPECT--
12+
bool(true)
13+
NULL
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test typed properties allow true
3+
--FILE--
4+
<?php
5+
class Foo {
6+
public true $value;
7+
}
8+
9+
$foo = new Foo();
10+
11+
try {
12+
$foo->value = false;
13+
} catch (\TypeError $e) {
14+
echo $e->getMessage();
15+
}
16+
17+
?>
18+
--EXPECT--
19+
Cannot assign bool to property Foo::$value of type true

0 commit comments

Comments
 (0)