Skip to content

Preserve allowed-value types in membership assertions - #372

Merged
shadowhand merged 3 commits into
webmozarts:masterfrom
Gabrieljr42:fix/269-psalm-membership-types
Nov 17, 2026
Merged

shadowhand merged 3 commits into
webmozarts:masterfrom
Gabrieljr42:fix/269-psalm-membership-types

Conversation

@Gabrieljr42

@Gabrieljr42 Gabrieljr42 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

@shadowhand

Copy link
Copy Markdown
Collaborator

@Gabrieljr42 this has some merge conflicts.

@Gabrieljr42

Copy link
Copy Markdown
Contributor Author

Thanks @shadowhand. I merged master and regenerated src/Mixin.php with bin/generate.php, so the new @psalm-flow annotations and the membership templates are both there. PHPUnit, Psalm and php-cs-fixer pass locally.

@shadowhand
shadowhand merged commit 2cc8f53 into webmozarts:master Nov 17, 2026
11 checks passed
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()

2 participants