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
Change E_WARNING to ValueError in sprintf functions #4837
Conversation
started injecting error throws into sprint php functions
We're generally okay with these warning -> exception conversions in library functions without an RFC as long as they meet certain requirements (which seems to be the case here from a quick look). |
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.
Added some suggested changes to use the new ValueError exception introduced with PHP 8
@moufmouf We've been already changing some of the warnings to exceptions for warnings which are triggered on programming errors. Edit: see for example the array functions (which I still need to update to use |
Excellent! I was under the impression a RFC was needed.
Are those requirements documented somewhere? |
As it is a recent thing, not really:
is the general guideline |
Hey @Girgias , Thanks a lot for the detailed comments and for the explanation. |
ext/standard/formatted_print.c
Outdated
@@ -524,7 +524,7 @@ php_formatted_print(zval *z_format, zval *args, int argc) | |||
|
|||
if (argnum >= argc) { | |||
efree(result); | |||
php_error_docref(NULL, E_WARNING, "Too few arguments"); | |||
zend_value_error("Too few arguments"); |
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.
I apologise I forgot we had zend_argument_count_error()
which probably makes more sense in this case.
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.
I started working on replacing the error message with something like:
zend_argument_count_error("%d parameters are required, %d given", argnum + 2, argc + 1);
It makes a lot of sense most of the time, but it turns out I'm running in an issue with "vprintf".
vprintf takes an array of placeholders as the second argument:
vprintf ( string $format , array $args ) : int
The message generated would be something like:
5 parameters are required, 3 given
which is blatantly misleading.
I was thinking about replacing the message with something like:
4 placeholders are required, 2 given
My question: does it still qualifies as a ArgumentCountError
? Or should I stick to the ValueError
?
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.
I'm not familiar with this piece of code, but if the v*print* family of functions use the same handler it's probably best to keep a value error, (splitting the function in two seems a bit pointless here), because when using an array you are passing the correct number of arguments but the array has the wrong "shape", thus I'd use a ValueError in that case, if this means using it for all other cases, I would suppose this to be alright.
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.
I added a new arguments to php_formatted_print
to specify the kind of error message to be thrown. That way, I can throw a ValueError
for vprint functions and a ArgumentCountError
otherwise, with appropriate error messages.
I'm not completely sure if this make sense, and the error message ("The arguments array must contain %d items, %d given"
) might need to be worked upon.
Let me know!
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.
I'm not sure either, tbh, @nikic any opinion? I don't feel like this complication is worth it, but the error messages being more descriptive is quite nice ...
32 bits test failure is legit on Azure Pipelines. |
I like the promotions to Exceptions for most of the PHP internal functions. |
@drealecs This change is for PHP 8. Of course, we cannot make such a change in 7.4. |
Just in case it got unnoticed, |
I'm considering writing an RFC to turn some warning into exceptions in a number of PHP functions.
This PR is just a support for the discussion I'm opening on internals. Do not merge.
TODO: