Skip to content

Narrow non-empty-string to non-falsy-string in Collection::filter() - #2335

Merged
alies-dev merged 1 commit into
4.xfrom
fix/collection-filter-non-empty-string
Nov 17, 2026
Merged

alies-dev merged 1 commit into
4.xfrom
fix/collection-filter-non-empty-string

Conversation

@alies-dev

Copy link
Copy Markdown
Collaborator

Issue to Solve

Collection::filter() without a callback removes "" and "0", but the handler narrowed only a plain string to non-falsy-string. A non-empty-string item stayed non-empty-string, even though it still admits "0".

Psalm 7.9 now infers non-empty-string from strlen($x) > N, so the common map(fn ($s) => strlen($s) > 3 ? $s : null)->filter() result regressed from non-falsy-string to non-empty-string (#2333 re-pinned the test to the weaker type).

 /** @param Collection<int, non-empty-string|null> $c */
 $c->filter();
-// Collection<int, non-empty-string>
+// Collection<int, non-falsy-string>

Related

Follow-up to #2333.

Solution Description

  • CollectionFilterHandler::narrowAtomic(): narrow exact TNonEmptyString to TNonFalsyString, like TString. Exact-class checks keep literal, lowercase, and class-string subtypes untouched.
  • CollectionFilterTest.phpt: the non-empty-string contract case and the map()->filter() case assert non-falsy-string again.
  • Moves the Nullsafe fetch on a non-null property receiver is typed nullable since 7.7.0 vimeo/psalm#13936 guard (fixed in Psalm 7.9) from HigherOrderProxyPropertyFetchItemTypeKnownLimitation.phpt into HigherOrderProxyPropertyFetchTest.phpt: it is no longer a limitation.

Checklist

  • Tests cover the change (type test in tests/Type/)

…er()

- filter() without a callback removes "0", which non-empty-string still admits; only plain string was narrowed.
- Restores the non-falsy-string result for map(...)->filter() now that Psalm 7.9 infers non-empty-string from strlen() comparisons.
- Moves the vimeo/psalm#13936 guard (fixed in Psalm 7.9) out of the known-limitation test.
@alies-dev alies-dev added the release:fix for PRs only (used by release-drafter) label Nov 17, 2026
@alies-dev alies-dev self-assigned this Nov 17, 2026
@alies-dev
alies-dev merged commit 2505648 into 4.x Nov 17, 2026
22 checks passed
@alies-dev
alies-dev deleted the fix/collection-filter-non-empty-string branch November 17, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix for PRs only (used by release-drafter)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant