Skip to content

Commit 68baa53

Browse files
committed
Allow integer default for float type
1 parent 2768bdb commit 68baa53

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Float type hint should allow an integer as default even with strict types
3+
--FILE--
4+
<?php
5+
6+
declare(strict_types=1);
7+
8+
function test(float $arg = 0)
9+
{
10+
var_dump($arg);
11+
}
12+
13+
test();
14+
15+
?>
16+
--EXPECT--
17+
float(0)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Float type hint should allow an integer as default
3+
--FILE--
4+
<?php
5+
6+
function test(float $arg = 0)
7+
{
8+
var_dump($arg);
9+
}
10+
11+
test();
12+
13+
?>
14+
--EXPECT--
15+
float(0)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Float type hint should not allow invalid types as default
3+
--FILE--
4+
<?php
5+
6+
function test(float $arg = true)
7+
{
8+
var_dump($arg);
9+
}
10+
11+
test();
12+
13+
?>
14+
--EXPECTF--
15+
16+
Fatal error: Default value for parameters with a float type hint can only be float, integer, or NULL in %s on line %d

Zend/zend_compile.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4397,10 +4397,21 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
43974397
if (arg_info->class_name) {
43984398
zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
43994399
"with a class type hint can only be NULL");
4400-
} else if (!ZEND_SAME_FAKE_TYPE(arg_info->type_hint, Z_TYPE(default_node.u.constant))) {
4401-
zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4402-
"with a %s type hint can only be %s or NULL",
4403-
zend_get_type_by_const(arg_info->type_hint), zend_get_type_by_const(arg_info->type_hint));
4400+
} else switch (arg_info->type_hint) {
4401+
case IS_DOUBLE:
4402+
if (Z_TYPE(default_node.u.constant) != IS_DOUBLE && Z_TYPE(default_node.u.constant) != IS_LONG) {
4403+
zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4404+
"with a float type hint can only be float, integer, or NULL");
4405+
}
4406+
break;
4407+
4408+
default:
4409+
if (!ZEND_SAME_FAKE_TYPE(arg_info->type_hint, Z_TYPE(default_node.u.constant))) {
4410+
zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4411+
"with a %s type hint can only be %s or NULL",
4412+
zend_get_type_by_const(arg_info->type_hint), zend_get_type_by_const(arg_info->type_hint));
4413+
}
4414+
break;
44044415
}
44054416
}
44064417
}

0 commit comments

Comments
 (0)