-
Notifications
You must be signed in to change notification settings - Fork 7.8k
round(): Validate the rounding mode #12252
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
switch (mode) { | ||
case PHP_ROUND_HALF_UP: | ||
case PHP_ROUND_HALF_DOWN: | ||
case PHP_ROUND_HALF_EVEN: | ||
case PHP_ROUND_HALF_ODD: | ||
break; | ||
default: | ||
zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); | ||
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.
Previously an invalid rounding mode was considered to be equal to PHP_ROUND_HALF_UP
. This is no longer the case since #12220. Making this a ValueError would be correct, but possibly a little steep. Shall I update this to a E_WARNING + change mode to PHP_ROUND_HALF_UP within the switch()
? I don't want to update the rounding helper to handle unknown cases to keep the function clean.
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 think having it directly a ValueError is fine IMHO, just add an entry in UPGRADING
96eb2f4
to
7dbdb22
Compare
fwiw, this error is reported by PHPStan since phpstan/phpstan-src#1126 https://phpstan.org/r/0baee5fd-2e82-409b-812d-bff03b8a31df |
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.
Aside, the precision
arg seems to be truncating values, maybe as a follow-up this should do the usual boundary checks and throw a value error, or make the _php_math_round()
take a zend_long
instead of an int
?
switch (mode) { | ||
case PHP_ROUND_HALF_UP: | ||
case PHP_ROUND_HALF_DOWN: | ||
case PHP_ROUND_HALF_EVEN: | ||
case PHP_ROUND_HALF_ODD: | ||
break; | ||
default: | ||
zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)"); | ||
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.
I think having it directly a ValueError is fine IMHO, just add an entry in UPGRADING
Any values with more than 2 digits are useless anyway. It probably makes sense to validate this, but first we need to establish how many digits are actually meaningful. There some open PRs around this by @SakiTakamachi. |
Yes, I thought about these problems and finally found what I personally think is the best solution. |
No description provided.