-
Notifications
You must be signed in to change notification settings - Fork 653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Is there a way to assert that an array key exists in a given array? #8785
Comments
I found these snippets: https://psalm.dev/r/74f4a57aaa<?php
/**
* @template TKey of array-key
* @template TValue
* @param TKey $key
* @param array<array-key,TValue> $array
* @psalm-assert array<TKey,TValue> $array
*/
function assertArrayKeyExists(string|int $key, array $array): void
{}
/** @var array<array-key,non-empty-string> $foo */
$foo = [];
assertArrayKeyExists('bar', $foo);
assertArrayKeyExists('baz', $foo);
/**
* expected array{bar:non-empty-string, baz:non-empty-string}
* @psalm-trace $foo
*/
|
I remember looking for something like this for another issue, unfortunately, the internal handling of that is pretty complex in Psalm. We have an HasArrayKey assertion and also an ArrayKeyExists one. If I remember correctly, the HasArrayKey applies to a specific variable and has a $key property for the name of the key that exists, and the ArrayKeyExists directly applies to the If we want to progress on this, we would probably first need to do exactly that. And then have some sort of syntax that allows to assert dynamic keys (like in your example). |
Do you mean psalm/psalm-plugin-phpunit#114 ? |
Yep, that's the one (though we were using V4 style assertion in Psalm's internal, so my explanation there is a bit outdated) |
Hey there,
I've tried something like this but I don't get a working example:
https://psalm.dev/r/74f4a57aaa
The text was updated successfully, but these errors were encountered: