Add the keysExist assertion - #364
Open
Amoifr wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.It reports every missing key rather than stopping at the first one:
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
oneOfKeysExistsin 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()andisAnyOf(), 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.phpregenerated withcomposer generate-mixin,tests/static-analysis/assert-keysExist.phpwritten and itsnon-returncounterpart generated, and the README table updated fortestIsInReadme.Suite green at 4949 tests, php-cs-fixer clean, psalm reports no errors. I could not run
roave-bc-checklocally, its lock file does not resolve on PHP 8.5, so that one is on CI.