Skip to content

Add the keysExist assertion - #364

Merged
shadowhand merged 1 commit into
webmozarts:masterfrom
Amoifr:feat-258-keys-exist
Oct 5, 2026
Merged

shadowhand merged 1 commit into
webmozarts:masterfrom
Amoifr:feat-258-keys-exist

Conversation

@Amoifr

@Amoifr Amoifr commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Closes #258, or at least the half of it that seems uncontroversial.

keyExists() only takes one key, so checking a set of them means a loop at the call site, which is the kind of loop this library exists to replace. keysExist() takes an iterable of keys and asserts that all of them are present.

Assert::keysExist($payload, ['name', 'email', 'password']);

It reports every missing key rather than stopping at the first one:

Expected the keys "email", "password" to exist.

That is deliberate. The use case in the issue is validating a request payload, where knowing the full list in one go is the point. A caller who wants to stop at the first miss still has keyExists().

The name is the plural of the existing keyExists(), and the argument order matches it.

The other half

@gordinskiy also asked for an "at least one of these keys" assertion, named oneOfKeysExists in the issue. I have left it out rather than pick a name on my own: the library already spells that idea two different ways, isInstanceOfAny() and isAnyOf(), and settling which one applies here is your call. Happy to add it in this pull request or a separate one once you say which shape you prefer.

Checks

The suite pointed me at three conventions I would otherwise have missed, and all are covered: src/Mixin.php regenerated with composer generate-mixin, tests/static-analysis/assert-keysExist.php written and its non-return counterpart generated, and the README table updated for testIsInReadme.

Suite green at 4949 tests, php-cs-fixer clean, psalm reports no errors. I could not run roave-bc-check locally, its lock file does not resolve on PHP 8.5, so that one is on CI.

@Amoifr

Amoifr commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

Hi @shadowhand, thanks again for merging #368! This one adds keysExist() for #258. Its workflow has never run, it looks like it is waiting for an approval: could you approve it so CI gets a chance? Glad to change the name if you prefer another one.

@Amoifr
Amoifr force-pushed the feat-258-keys-exist branch from e899c0c to fbca280 Compare October 3, 2026 16:12
@shadowhand
shadowhand merged commit 497bee0 into webmozarts:master Oct 5, 2026
9 checks passed
@shadowhand

Copy link
Copy Markdown
Collaborator

Thank you!

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.

Feature Request: Check that multiple specific keys exist in an array

2 participants