Commit fc2de57f151 for php
commit fc2de57f151e404ad78c22bd41f83feccfd33d91
Author: Weilin Du <weilindu@php.net>
Date: Wed Dec 2 00:25:54 2026 +0800
Suggest the correct casing for unknown named parameters (#25891)
Now, when a named argument differs only in ASCII case from
exactly one declared parameter, suggest the correct spelling if
that parameter has yet to receive an argument.
diff --git a/NEWS b/NEWS
index f15a60cedeb..e4c6af5315d 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,8 @@ PHP NEWS
__clone() keeps a reference and the result is discarded). (Tomasz Kowalczyk)
. Updated dtoa.c to the latest version, improving float conversion
performance. (Alexander Danilov)
+ . Added capitalization hints to "Unknown named parameter" errors.
+ (Weilin Du)
- Curl:
. Negative values for CURLOPT_POSTREDIR are now rejected, causing
diff --git a/UPGRADING b/UPGRADING
index 7c36597353b..37b3c87966b 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -97,6 +97,11 @@ PHP 8.7 UPGRADE NOTES
2. New Features
========================================
+- Core:
+ . The "Unknown named parameter" error now includes the declared name when
+ the supplied name has a unique ASCII case-insensitive match and the
+ matching parameter has yet to receive an argument.
+
- Curl:
. Multiple files can now be uploaded with the same field name by passing an
array of CURLFile or CURLStringFile objects to CURLOPT_POSTFIELDS.
diff --git a/Zend/tests/named_params/unknown_named_param_case.phpt b/Zend/tests/named_params/unknown_named_param_case.phpt
new file mode 100644
index 00000000000..23fc6e7ef5e
--- /dev/null
+++ b/Zend/tests/named_params/unknown_named_param_case.phpt
@@ -0,0 +1,48 @@
+--TEST--
+Suggest the correct case of an unknown named parameter
+--FILE--
+<?php
+function example($timeout, $other = null) {}
+function ambiguous($timeout, $TIMEOUT) {}
+function variadic($timeout = 0, ...$options) {
+ var_dump($options);
+}
+
+foreach ([
+ fn() => example(TimeOut: 10),
+ fn() => example(tiemout: 10),
+ fn() => example(time_out: 10),
+ fn() => ambiguous(TimeOut: 10),
+ fn() => ambiguous(timeout: 10, TimeOut: 20),
+ fn() => example(10, TimeOut: 20),
+ fn() => example(timeout: 10, TimeOut: 20),
+ fn() => example(other: 1, TimeOut: 10),
+ fn() => example(...['TimeOut' => 10]),
+ fn() => (new ReflectionFunction('example'))->invokeArgs(['TimeOut' => 10]),
+ fn() => strlen(String: 'value'),
+] as $call) {
+ try {
+ $call();
+ } catch (Error $e) {
+ echo $e->getMessage(), "\n";
+ }
+}
+
+variadic(TimeOut: 10);
+?>
+--EXPECT--
+Unknown named parameter $TimeOut. The parameter $timeout differs only in case.
+Unknown named parameter $tiemout
+Unknown named parameter $time_out
+Unknown named parameter $TimeOut
+Unknown named parameter $TimeOut
+Unknown named parameter $TimeOut
+Unknown named parameter $TimeOut
+Unknown named parameter $TimeOut. The parameter $timeout differs only in case.
+Unknown named parameter $TimeOut. The parameter $timeout differs only in case.
+Unknown named parameter $TimeOut. The parameter $timeout differs only in case.
+Unknown named parameter $String. The parameter $string differs only in case.
+array(1) {
+ ["TimeOut"]=>
+ int(10)
+}
diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c
index 7d270c3e249..2157217b9a7 100644
--- a/Zend/zend_execute.c
+++ b/Zend/zend_execute.c
@@ -5558,6 +5558,34 @@ static zend_always_inline uint32_t zend_get_arg_offset_by_name(
return (uint32_t) -1;
}
+static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_unknown_named_arg(
+ const zend_execute_data *call, const zend_string *arg_name)
+{
+ const zend_function *fbc = call->func;
+ const zend_string *suggestion = NULL;
+
+ for (uint32_t i = 0; i < fbc->common.num_args; i++) {
+ const zend_string *param_name = fbc->common.arg_info[i].name;
+ if (!zend_string_equals_ci(arg_name, param_name)) {
+ continue;
+ }
+ if (suggestion != NULL
+ || (i < ZEND_CALL_NUM_ARGS(call) && !Z_ISUNDEF_P(ZEND_CALL_VAR_NUM(call, i)))) {
+ suggestion = NULL;
+ break;
+ }
+ suggestion = param_name;
+ }
+
+ if (suggestion != NULL) {
+ zend_throw_error(NULL,
+ "Unknown named parameter $%pS. The parameter $%pS differs only in case.",
+ arg_name, suggestion);
+ } else {
+ zend_throw_error(NULL, "Unknown named parameter $%pS", arg_name);
+ }
+}
+
zval * ZEND_FASTCALL zend_handle_named_arg(
zend_execute_data **call_ptr, zend_string *arg_name,
uint32_t *arg_num_ptr, void **cache_slot) {
@@ -5565,7 +5593,7 @@ zval * ZEND_FASTCALL zend_handle_named_arg(
const zend_function *fbc = call->func;
uint32_t arg_offset = zend_get_arg_offset_by_name(fbc, arg_name, cache_slot);
if (UNEXPECTED(arg_offset == (uint32_t) -1)) {
- zend_throw_error(NULL, "Unknown named parameter $%pS", arg_name);
+ zend_unknown_named_arg(call, arg_name);
return NULL;
}