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
InvalidArgument doesn't work for callable args count #8593
Comments
I found these snippets: https://psalm.dev/r/1b69254ca2<?php
/**
* @param callable(string, string):void $callback
* @return void
*/
function caller($callback) {}
/**
* @param int $bar
* @return void
*/
function foo( $bar ) {}
caller('foo');
/**
* @param string $bar
* @return void
*/
function bar( $bar ) {}
caller('bar');
|
I don't agree that this should have been fixed. It is valid to pass a callable with fewer parameters. |
It's wrong only when passing a built-in function https://3v4l.org/GT86Q. |
I'm open to arguments on that, but saying that php doesn't crash is not the most convincing. Do you have use cases that require you to call the callback with more param than it requires? Then it's a balance between:
|
The fix makes it consistent with psalm default behavior of TooFewArguments/TooManyArguments https://psalm.dev/r/748342d72e Additionally, for me this was mostly an issue of stubs, where we only have the phpdoc & function declaration, not any code inside the function. Therefore it relies on the phpdoc. Feel free to add a PR though, that fixes the behavior in cases where you pass x number of arguments in the callback, therefore overriding the phpdoc/stubs |
I found these snippets: https://psalm.dev/r/748342d72e<?php
/**
* @param string $a
* @param string $b
* @return void
*/
function foo( $a, $b ) {}
foo( 'a', 'b', 'c' );
foo( 'a' );
|
Yes, it's very common. E.g. a lot of methods accepting |
Not sure how to proceed then. Should we keep it to help user find legit mistakes or should we remove the check for functions outside Callmap not to bother more advanced usages? Maybe a config? |
Definitely want to keep it as is, as it's super helpful for us :-) |
+1 for keeping it as it is, you can always provide an alternative callable in a union if needed: |
No, you can't, because it will cause a |
I found these snippets: https://psalm.dev/r/b5116d9026<?php
/**
* @param (callable(string, string): void)|(callable(string): void) $callback
*/
function caller(callable $callback): void
{
$callback('a', 'b');
}
|
How is it helpful if it's absolutely legal to pass a non-native function with less parameters? |
@vudaltsov Would a new CallableTooManyArguments emitted especially for those cases that you could suppress for your whole project work for you? |
That would work I suppose. Or an alternative syntax (e.g. |
Ooo, I love the |
I don't think that The one who defines the contract does not really care about the number of parameters, because it does not affect the result in any way. The user can always pass I think that |
https://psalm.dev/r/1b69254ca2
It correctly complains about InvalidArgument when the type is wrong.
But it should also give an error when I only pass 1 param, but the callable expects 2
The text was updated successfully, but these errors were encountered: