Skip to content
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

String recognized as function instead of string #11192

Open
parshikov opened this issue Dec 27, 2024 · 1 comment
Open

String recognized as function instead of string #11192

parshikov opened this issue Dec 27, 2024 · 1 comment

Comments

@parshikov
Copy link

parshikov commented Dec 27, 2024

The function someFunction accepts a parameter $callback with the type (callable(int):mixed)|string|null. However, when passing 'myFunc' (a name of almost any function) as an argument, a InvalidArgument error occurs.

https://psalm.dev/r/b0184be10a

Happened to me with PHP 8.3, Psalm 5.26.1, in Laravel Eloquent (v11).
I tried to aggregate metrics. count is the field of object with count of requests

/** @var Collection $statistics */
return $statistics->sum('count'); // InvalidArgument
Copy link

I found these snippets:

https://psalm.dev/r/b0184be10a
<?php

/**
 * Get the max value of a given key.
 *
 * @param  (callable(int):mixed)|string|null  $callback
 * @return mixed
 */
function someFunction(callable|string|null $callback): callable|string|null
{
    return $callback;
}

/**
 * @param array $value The array or the object.
 * @param int $mode [optional] If the optional mode parameter is set to
*/
function myFunc(array $value, int $mode = COUNT_NORMAL): int {
	return count($value)*$mode; //just to suppress UnusedParameter
}

someFunction('myFunc'); // <- this is valid parameter with type 'string' but InvalidArgument appeared, no ok
someFunction((string)'myFunc'); // <- no InvalidArgument
Psalm output (using commit 765dcbf):

ERROR: InvalidArgument - 22:14 - Argument 1 of someFunction expects callable(int):mixed|null|string, but impure-callable(array<array-key, mixed>, int=):int provided

ERROR: RedundantCast - 23:14 - Redundant cast to string

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

No branches or pull requests

1 participant