-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Fix implode() function signature #11991
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
Conversation
ext/standard/string.c
Outdated
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
if (pieces == NULL) { | ||
if (arg1_array == NULL) { | ||
zend_type_error("%s(): Argument #1 ($pieces) must be of type array, string given", get_active_function_name()); | ||
zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); | |
zend_type_error("%s(): Argument #2 ($array) must be of type array, string given", get_active_function_name()); |
Isn't it argument #2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an argument number 2 only when it's not an argument number 1. In this case, it is an argument number 1.
First of all, the parameter name in the error message is plain wrong indeed, it uses the old name which we renamed for PHP 8.0.
P.S. To be honest, this function should have been included to my overloaded signature RFC, but somehow I didn't find it. :/ |
We can also do that, but my main goal here was to disallow passing Also, the documentation doesn't help in this case because in the documentation, it's said that the signature is Do you see any disadvantages or errors in my bug fix? |
Hi, Maybe the function implementation should switch on This would have the benefit to make the logic easier to follow (I find it currently confusing - as shown by @jorgsowa's comment). switch ZEND_NUM_ARGS() {
case 1:
// this is the 1-argument signature,
// argument #1 ($array) must be of type array
break;
case 2:
// this is the 2-arguments signature,
// argument #1 ($separator) must be of type string
// argument #2 ($array) must be of type array
break;
default:
// already checked by ZPP
ZEND_UNREACHABLE();
} |
I just want to add that whichever way we do it, the error message will be inaccurate. The main thing we should fix is to disallow passing null as the second param. |
if (arg1_array == NULL) { | ||
zend_type_error("%s(): Argument #1 ($array) must be of type array, string given", get_active_function_name()); | ||
RETURN_THROWS(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | |
} | |
arg1_str = ZSTR_EMPTY_ALLOC(); | |
pieces = arg1_array; |
ee0be5f
to
6bc6a84
Compare
TBH, I can't remember having ever used the 1-argument form of this function. I agree with @kocsismate that it should just be deprecated. |
I disagree with deprecating it - it's a bit unfortunate that the order is |
What's even more unfortunate is that we deprecated the latter form... :(
I don't have a good idea, only a somewhat unorthodox one: let's reuse
Relying on the number of parameters passed would make the default value of the 2nd parameter |
Well, that's highly subjective... In functional languages for example, the collection to operate on is always passed last (because of partial application). So from my POV, it's rather fortunate that for once PHP chose the correct order instead of having it backwards like in But anyway, consistency and correct error messages matter more than style preferences.
Just to clarify, my comment did not concern the function signature, just how the internal logic was implemented - which was initially so confusing to follow that several people initially thought that the patch was doing something wrong WRT the argument number. Unless I'm missing something, switching on |
I don't think we can disallow this in the stable version as it might potentially break some apps that are using |
6bc6a84
to
37c979e
Compare
I rebased this PR against master now. For PHP 8.1, I will only make the tiny change to the text of the error message to use |
37c979e
to
9cea77d
Compare
Superceded by #12683 |
@kocsismate Please review this and tell me if we can do it like this.
This function cannot take null as the second parameter. Allowing it to do so leads to a very confusing error message.
Looking at the documentation, there is no argument called
$pieces
. If someone passes a string as the first argument and null as the second, it's more likely that the second argument is the wrong one. In my opinion the stub should say that the second parameter is optional and it's an empty array by default. After reading arguments, we check if pieces is null anyway. This fixes all confusing error messages to reflect reality.FWIW, I just got this error in real life and was VERY confused. The real error was that the second argument was null when it should have been an array, but this error message didn't tell me that.