diff options
author | Aaron Piotrowski <aaron@trowski.com> | 2015-07-02 17:46:51 -0500 |
---|---|---|
committer | Aaron Piotrowski <aaron@trowski.com> | 2015-07-02 18:07:24 -0500 |
commit | 68baa539dc1e42a904617fcb799b58a5705b87ba (patch) | |
tree | 54864a186088a42262243b64314bcf3a9f933762 | |
parent | 2768bdbce08646ded80bc472a8b19631a23c2da7 (diff) | |
download | php-git-68baa539dc1e42a904617fcb799b58a5705b87ba.tar.gz |
Allow integer default for float type
4 files changed, 63 insertions, 4 deletions
diff --git a/Zend/tests/typehints/scalar_float_with_integer_default_strict.phpt b/Zend/tests/typehints/scalar_float_with_integer_default_strict.phpt new file mode 100644 index 0000000000..b1aab433f9 --- /dev/null +++ b/Zend/tests/typehints/scalar_float_with_integer_default_strict.phpt @@ -0,0 +1,17 @@ +--TEST-- +Float type hint should allow an integer as default even with strict types +--FILE-- +<?php + +declare(strict_types=1); + +function test(float $arg = 0) +{ + var_dump($arg); +} + +test(); + +?> +--EXPECT-- +float(0) diff --git a/Zend/tests/typehints/scalar_float_with_integer_default_weak.phpt b/Zend/tests/typehints/scalar_float_with_integer_default_weak.phpt new file mode 100644 index 0000000000..ab3206691a --- /dev/null +++ b/Zend/tests/typehints/scalar_float_with_integer_default_weak.phpt @@ -0,0 +1,15 @@ +--TEST-- +Float type hint should allow an integer as default +--FILE-- +<?php + +function test(float $arg = 0) +{ + var_dump($arg); +} + +test(); + +?> +--EXPECT-- +float(0) diff --git a/Zend/tests/typehints/scalar_float_with_invalid_default.phpt b/Zend/tests/typehints/scalar_float_with_invalid_default.phpt new file mode 100644 index 0000000000..6b67985fd1 --- /dev/null +++ b/Zend/tests/typehints/scalar_float_with_invalid_default.phpt @@ -0,0 +1,16 @@ +--TEST-- +Float type hint should not allow invalid types as default +--FILE-- +<?php + +function test(float $arg = true) +{ + var_dump($arg); +} + +test(); + +?> +--EXPECTF-- + +Fatal error: Default value for parameters with a float type hint can only be float, integer, or NULL in %s on line %d diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index df19f93be2..5a15392fe9 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -4397,10 +4397,21 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */ if (arg_info->class_name) { zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " "with a class type hint can only be NULL"); - } else if (!ZEND_SAME_FAKE_TYPE(arg_info->type_hint, Z_TYPE(default_node.u.constant))) { - zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " - "with a %s type hint can only be %s or NULL", - zend_get_type_by_const(arg_info->type_hint), zend_get_type_by_const(arg_info->type_hint)); + } else switch (arg_info->type_hint) { + case IS_DOUBLE: + if (Z_TYPE(default_node.u.constant) != IS_DOUBLE && Z_TYPE(default_node.u.constant) != IS_LONG) { + zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " + "with a float type hint can only be float, integer, or NULL"); + } + break; + + default: + if (!ZEND_SAME_FAKE_TYPE(arg_info->type_hint, Z_TYPE(default_node.u.constant))) { + zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " + "with a %s type hint can only be %s or NULL", + zend_get_type_by_const(arg_info->type_hint), zend_get_type_by_const(arg_info->type_hint)); + } + break; } } } |