Skip to content

Narrow explode() result for limit 0, 1, and -1#5993

Merged
VincentLanglet merged 1 commit into
phpstan:2.2.xfrom
paulbalandan:explode-single-element-limit
Jul 5, 2026
Merged

Narrow explode() result for limit 0, 1, and -1#5993
VincentLanglet merged 1 commit into
phpstan:2.2.xfrom
paulbalandan:explode-single-element-limit

Conversation

@paulbalandan

Copy link
Copy Markdown
Contributor

Follow-up to #5959, refining explode()'s return type for small limits, per the explode() manual:

  • Limit 0 or 1 returns the whole string unsplit (a limit of 0 is treated as 1), so the result is the single-element shape array{string} instead of non-empty-list<string>. This holds regardless of whether the delimiter is present.
  • Limit -1 with a delimiter proven present drops only the last of the (at least two) parts, leaving at least one, so non-empty-list<string> instead of list<string>. Limit -2 and beyond can still empty the result and stay list<string>.

@VincentLanglet VincentLanglet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need assertion on

explode("\n", $s, -1)

without the str_contains check

@paulbalandan paulbalandan force-pushed the explode-single-element-limit branch from 0fa6aac to cc31f25 Compare July 5, 2026 03:51
@paulbalandan

Copy link
Copy Markdown
Contributor Author

Added: explode("\n", $s, -1) without the str_contains guard now asserts list<string>.

@VincentLanglet VincentLanglet merged commit 22c9256 into phpstan:2.2.x Jul 5, 2026
669 of 672 checks passed
@VincentLanglet

Copy link
Copy Markdown
Contributor

Thanks

@paulbalandan paulbalandan deleted the explode-single-element-limit branch July 5, 2026 09:35
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.

3 participants