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
Add support for assertArrayHasKey
and assertCount
#114
Comments
I found these snippets: https://psalm.dev/r/e1d61bda01<?php
/**
* @return array<string, list<string>>
*/
function foo() {
return [];
}
function foo2(): string
{
$a = foo();
assert(array_key_exists('foo', $a));
$foo = $a['foo'];
assert(count($foo) === 1);
return $foo[0];
}
|
I'm 99% sure it's impossible to implement with just |
@orklah maybe it's a good opportunity for some new features about |
I think you should already be able to use count() with greater or less than comparison would be harder without a plugin because the assertion is technically Same with equal comparison, the assertion is The harder will probably be key existence. Psalm has specific code for that because it implies to recursively change the type of different array dimensions. (Think You'd need to recheck but there are actually two set of assertions depending on what you want to change If you want to progress on that, you'd have to find a syntax to use a param value to complete the assertion. (Something like This might be easier with a plugin, but I'm not actually sure on which hook you should code for adding assertions into Psalm though... |
What I said above is partially true. I just learned that internal assertions are not available automatically to docblock assertions. Given the changes to be made, it's only a small hiccup more, but it's important to note. |
I again encounter issue with assertArrayHasKey. Phpstan has somehthing like a TypeSpecifier https://github.com/phpstan/phpstan-phpunit/blob/15dc3e0d81b711c38d02fb33fe4482dfa570951c/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php Is there something equivalent in psalm ? |
This is the method that handles You could use some variation of this in a plugin hooked to assertArrayHasKey and assertCount to have the same effect as if they were simple asserts I guess. I hoped to find another entry point to our internal assertion module but I couldn't find one. It could be cool to be able to add an arbitrary assertion to a variable from plugins, especially with Psalm 5 that's coming with the brand new system :) @muglug, maybe I missed something? How would you add an arbitrary assertion from a plugin? |
I would really like to add the |
I'm not sure it's even required that you do. I think you could just add this function somewhere in this plugin and call it. Which means, all it would take is to send the correct value here: https://github.com/vimeo/psalm/blob/11e60fa261c682627e6b85300dd6f424b87520b1/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php#L929 Those are nodes from PHParser that need to be simulated (i.e. replacing the |
Best would be to be able to support the syntax
It would also be useful for |
it would be horrible: it addresses one specific use case by creating a special unique syntax that you (as a user) now need to learn and maintain (as psalm developer). And that knowledge would not scale, eg: |
psalm supports
the syntax
could be translated to
so it would be a generic feature, and not a specific one. In your example,
|
what would it mean type-wise? What assertion would it establish? |
This code is valid https://psalm.dev/r/e1d61bda01
But when I use
assertArrayHasKey
andassertCount
, the offset are reported as possibly undefined.It would be great to tell psalm that
I'm not familiar with, but I'm not sure it can be done with psalm assertion https://psalm.dev/docs/annotating_code/assertion_syntax/
The text was updated successfully, but these errors were encountered: