Skip to content

Add round() throw type extension - #6520

Merged
ondrejmirtes merged 1 commit into
2.2.xfrom
round-throw-type
Sep 22, 2026
Merged

ondrejmirtes merged 1 commit into
2.2.xfrom
round-throw-type

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

PhpStorm stubs now declare @throws \ValueError on round(), so every call became an explicit throw point.

round() throws ValueError (Argument #3 ($mode) must be a valid rounding mode (RoundingMode::*)) only on PHP 8.4+, and only for an int $mode outside the eight rounding modes 1–8. PHP 8.0–8.3 accept any int mode silently, and every RoundingMode case is always valid.

The extension returns VoidType (no throw point) when:

  • the analysed PHP version is below 8.4 (new PhpVersion::throwsValueErrorForInvalidRoundingMode()),
  • $mode is not passed,
  • the native type of $mode is a subtype of RoundingMode|int<1, 8>.

Otherwise it keeps the declared ValueError, e.g. for round($f, 0, 9), for a native int $mode, and for a PHPDoc-only @param 1 $mode. It also keeps it for unpacked or leftover named arguments.

Tests are in CatchWithUnthrownExceptionRuleTest, with separate expectations for PHP 8.4+ and for PHP < 8.4, where every catch is dead. Both fail without the extension; the < 8.4 expectations were verified locally on PHP 7.4.

A side note, not changed here: resources/functionMap_php80delta.php still types $mode as 1|2|3|4, so on PHP 8.4+ the valid int modes 5–8 are reported by argument.type (RoundingMode cases are accepted).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Dhb4ssXuKVWcVBcNFpsdn7

PhpStorm stubs now declare @throws \ValueError on round(), which made every
call an explicit throw point. It is thrown only on PHP 8.4+, for an int $mode
that is not one of the eight rounding modes (1-8); older versions accept any
mode. The decision reads the native type of $mode, so a PHPDoc-only valid
constant keeps the ValueError.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dhb4ssXuKVWcVBcNFpsdn7
@ondrejmirtes
ondrejmirtes merged commit 7ef0c4c into 2.2.x Sep 22, 2026
570 of 577 checks passed
@ondrejmirtes
ondrejmirtes deleted the round-throw-type branch September 22, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant