Skip to content

Add min() and max() throw type extension - #6516

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

ondrejmirtes merged 1 commit into
2.2.xfrom
min-max-throw-type

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

PhpStorm stubs now declare @throws \ValueError on min() and max(), so every call became an explicit throw point. That broke @throws void functions and custom rules that look at explicit throw points: min(max(1, $x), 5) inside an open database transaction was reported by a downstream "transaction not resolved before leaving the method" rule.

min() / max() throw ValueError only when called with a single empty array. With two or more arguments the values are just compared, and PHP 7 returns false with a warning instead of throwing.

The extension returns VoidType (no throw point) when:

  • the analysed PHP version is below 8.0,
  • the first argument (or the element type of an unpacked first argument) is not an array, or is a non-empty array,
  • the array variant of the function is not the one selected for the arguments.

Otherwise it keeps the declared ValueError, e.g. for max($maybeEmptyArray), min([]), max($mixed) or max(...$listOfLists).

All decisions read Scope::getNativeType(), so a PHPDoc-only non-empty-list keeps the ValueError while a native narrowing like if ($a === []) { return; } removes it.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01Dhb4ssXuKVWcVBcNFpsdn7

PhpStorm stubs now declare @throws \ValueError on min() and max(), which made
every call an explicit throw point. They throw it only when called with a
single empty array: multiple arguments are just compared with each other, and
PHP 7 returns false instead. The decision reads native types, so a
PHPDoc-only non-empty-array 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 9b0768a into 2.2.x Sep 22, 2026
244 of 246 checks passed
@ondrejmirtes
ondrejmirtes deleted the min-max-throw-type branch September 22, 2026 09:23
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