Skip to content

Deprecate required param after optional #5067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Zend/tests/call_user_func_005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var_dump(call_user_func(array('foo', 'teste')));

?>
--EXPECTF--
Deprecated: Required parameter $b follows optional parameter $a in %s on line %d
string(1) "x"
array(1) {
[0]=>
Expand Down
14 changes: 14 additions & 0 deletions Zend/tests/required_param_after_optional.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Required parameter after optional is deprecated
--FILE--
<?php

function test($testA = null, $testB = null, $testC) {}
function test2(Type $test2A = null, $test2B = null, $test2C) {}
function test3(Type $test3A = null, Type2 $test3B = null, $test3C) {}

?>
--EXPECTF--
Deprecated: Required parameter $testC follows optional parameter $testA in %s on line %d

Deprecated: Required parameter $test2C follows optional parameter $test2B in %s on line %d
4 changes: 2 additions & 2 deletions Zend/tests/traits/bug60717.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace HTML
{
function text($text);
function attributes(array $attributes = null);
function textArea(array $attributes = null, $value);
function textArea(?array $attributes, $value);
}

trait TextUTF8
Expand All @@ -19,7 +19,7 @@ namespace HTML

trait TextArea
{
function textArea(array $attributes = null, $value) {}
function textArea(?array $attributes, $value) {}
abstract function attributes(array $attributes = null);
abstract function text($text);
}
Expand Down
15 changes: 15 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5679,6 +5679,7 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
uint32_t i;
zend_op_array *op_array = CG(active_op_array);
zend_arg_info *arg_infos;
zend_string *optional_param = NULL;

if (return_type_ast) {
/* Use op_array->arg_info[-1] for return type */
Expand Down Expand Up @@ -5747,10 +5748,24 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
default_node.op_type = IS_CONST;
zend_const_expr_to_zval(&default_node.u.constant, default_ast);
CG(compiler_options) = cops;

if (!optional_param) {
/* Ignore parameters of the form "Type $param = null".
* This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
zend_bool is_implicit_nullable =
type_ast && Z_TYPE(default_node.u.constant) == IS_NULL;
if (!is_implicit_nullable) {
optional_param = name;
}
}
} else {
opcode = ZEND_RECV;
default_node.op_type = IS_UNUSED;
op_array->required_num_args = i + 1;
if (optional_param) {
zend_error(E_DEPRECATED, "Required parameter $%s follows optional parameter $%s",
ZSTR_VAL(name), ZSTR_VAL(optional_param));
}
}

arg_info = &arg_infos[i];
Expand Down
8 changes: 2 additions & 6 deletions ext/reflection/tests/ReflectionClass_isArray.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public bool ReflectionParameter::isArray ( void );
marcosptf - <[email protected]> - @phpsp - sao paulo - br
--FILE--
<?php
function testReflectionIsArray($a = null, $b = 0, array $c, $d=true, array $e, $f=1.5, $g="", array $h, $i="#F989898") {}

function testReflectionIsArray(array $a, ?array $b, iterable $c, array|string $d) {}

$reflection = new ReflectionFunction('testReflectionIsArray');

Expand All @@ -13,12 +14,7 @@ foreach ($reflection->getParameters() as $parameter) {
}
?>
--EXPECT--
bool(false)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
24 changes: 12 additions & 12 deletions ext/reflection/tests/ReflectionType_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ReflectionParameter::get/hasType and ReflectionType tests
--FILE--
<?php
function foo(stdClass $a, array $b, callable $c, stdClass $d = null, $e = null, string $f, bool $g, int $h, float $i, NotExisting $j) { }
function foo(stdClass $a, array $b, callable $c, string $f, bool $g, int $h, float $i, NotExisting $j, stdClass $d = null, $e = null) { }

function bar(): stdClass { return new stdClass; }

Expand Down Expand Up @@ -120,36 +120,36 @@ bool(true)
string(8) "callable"
** Function 0 - Parameter 3
bool(true)
bool(true)
bool(false)
string(8) "stdClass"
** Function 0 - Parameter 4
bool(false)
** Function 0 - Parameter 5
bool(true)
bool(false)
bool(true)
string(6) "string"
** Function 0 - Parameter 6
** Function 0 - Parameter 4
bool(true)
bool(false)
bool(true)
string(4) "bool"
** Function 0 - Parameter 7
** Function 0 - Parameter 5
bool(true)
bool(false)
bool(true)
string(3) "int"
** Function 0 - Parameter 8
** Function 0 - Parameter 6
bool(true)
bool(false)
bool(true)
string(5) "float"
** Function 0 - Parameter 9
** Function 0 - Parameter 7
bool(true)
bool(false)
bool(false)
string(11) "NotExisting"
** Function 0 - Parameter 8
bool(true)
bool(true)
bool(false)
string(8) "stdClass"
** Function 0 - Parameter 9
bool(false)
** Function 1 - Parameter 0
bool(true)
bool(false)
Expand Down
3 changes: 2 additions & 1 deletion ext/reflection/tests/bug62715.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ foreach ($r->getParameters() as $p) {
}
}
?>
--EXPECT--
--EXPECTF--
Deprecated: Required parameter $c follows optional parameter $b in %s on line %d
bool(true)
bool(true)
bool(false)
Expand Down