Skip to content

Preserve allowed-value types in membership assertions - #372

Open
Gabrieljr42 wants to merge 2 commits into
webmozarts:masterfrom
Gabrieljr42:fix/269-psalm-membership-types
Open

Gabrieljr42 wants to merge 2 commits into
webmozarts:masterfrom
Gabrieljr42:fix/269-psalm-membership-types

Conversation

@Gabrieljr42

@Gabrieljr42 Gabrieljr42 commented Oct 24, 2026 •

Copy link
Copy Markdown

Fixes #269.

Psalm can now narrow Assert::oneOf($value, ['red', 'blue']) to 'red'|'blue'. The hook resolves value and values by parameter name, so reordered calls such as Assert::oneOf(values: $allowed, value: $candidate) narrow the candidate and return value correctly. The fixtures also cover array-valued candidates.

This applies to inArray and the nullable and collection variants as well. Collection assertions retain keys, optional fields and permitted null entries, and remove non-iterable alternatives after a successful assertion. Membership narrowing is handled by the hook, avoiding Psalm's positional handling of the previous assertion annotations. Runtime membership checks are unchanged.

Validation on PHP 8.4.22 with the locked tools:

  • Psalm 6.14.3 passes, including exact candidate and returned-value checks for named calls, nested arrays, nullable unions and Traversable inputs. The named-call regressions reproduce errors with the previous positional hook.
  • PHPUnit: 4,956 tests, 5,563 assertions, 58 skips and 2 notices; no failures.
  • Code style passes, and a second generator run leaves the generated files unchanged.

Psalm was invoked directly with one worker on Windows because the Composer wrapper's $(pwd) argument does not work in this shell.

This branch has not been deployed

No deployments
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.

Psalm support for oneOf() and allOneOf()

1 participant