Skip to content

Commit 8824cd7

Browse files
committed
Add true as a type
1 parent c28d1fb commit 8824cd7

18 files changed

+251
-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: 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

Zend/tests/type_declarations/union_types/standalone_false.phpt renamed to Zend/tests/type_declarations/literal_types/false_standalone.phpt

File renamed without changes.

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

File renamed without changes.
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: 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

0 commit comments

Comments
 (0)