Skip to content

Add array_chunk() throw type extension - #6524

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

ondrejmirtes merged 1 commit into
2.2.xfrom
array-chunk-throw-type

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

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

array_chunk() throws ValueError (Argument #2 ($length) must be greater than 0) only when $length is less than 1. This happens even for an empty array. It is the same on PHP 8.0–8.5, and PHP 7 only warns.

The extension returns VoidType when the analysed PHP version is below 8.0, or when the native type of $length is a subtype of int<1, max>. Otherwise it keeps the ValueError. That covers array_chunk($a, 0), a native int $length, and a PHPDoc-only positive-int. A native narrowing like if ($length > 0) 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 < 8.0 expectations were verified locally on PHP 7.4.

The PRs for array_chunk(), get_class(), hash(), array_fill() and unserialize() are all based on 2.2.x. They add their test methods at different places in CatchWithUnthrownExceptionRuleTest, so they merge cleanly in any order (checked).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Dhb4ssXuKVWcVBcNFpsdn7

PhpStorm stubs now declare @throws \ValueError on array_chunk(), which made
every call an explicit throw point. It is thrown only when $length is less
than 1, even for an empty array, and PHP 7 only warns. The decision reads the
native type of $length, so a PHPDoc-only positive-int 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 9260927 into 2.2.x Sep 22, 2026
872 of 890 checks passed
@ondrejmirtes
ondrejmirtes deleted the array-chunk-throw-type branch September 22, 2026 11:07
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