Skip to content

Commit 9310e7c

Browse files
committed
Add true as a type
1 parent 7061c40 commit 9310e7c

16 files changed

+189
-0
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: 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
===DONE===
14+
--EXPECT--
15+
bool(true)
16+
===DONE===
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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) {}
7+
8+
?>
9+
===DONE===
10+
--EXPECT--
11+
===DONE===
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
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Using both bool and true in a union
3+
--FILE--
4+
<?php
5+
6+
function test(): bool|true {
7+
}
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Duplicate type true is redundant in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Using both false and true in a union instead of bool
3+
--FILE--
4+
<?php
5+
6+
function test(): false|true {
7+
}
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Type contains both true and false, bool should be used instead in %s on line %d

0 commit comments

Comments
 (0)