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

Fix callable without args not handled correctly #10500

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,7 @@ public function getSignatureInformation(
if (InternalCallMapHandler::inCallMap($function_symbol)) {
$callables = InternalCallMapHandler::getCallablesFromCallMap($function_symbol);

if (!$callables || !$callables[0]->params) {
if (!$callables || !isset($callables[0]->params)) {
return null;
}

Expand Down Expand Up @@ -1838,7 +1838,7 @@ public function getBeginedLiteralPart(string $file_path, Position $position): st
$offset = $position->toOffset($file_contents);

preg_match('/\$?\w+$/', substr($file_contents, 0, $offset), $matches);

return $matches[0] ?? '';
}

Expand Down Expand Up @@ -1957,7 +1957,7 @@ public function getCompletionItemsForClassishThing(
str_replace('$', '', $property_name),
);
}

foreach ($class_storage->pseudo_property_set_types as $property_name => $type) {
$pseudo_property_types[$property_name] = new CompletionItem(
str_replace('$', '', $property_name),
Expand All @@ -1969,7 +1969,7 @@ public function getCompletionItemsForClassishThing(
str_replace('$', '', $property_name),
);
}

$completion_items = [...$completion_items, ...array_values($pseudo_property_types)];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ private static function isSupported(FunctionLikeParameter $container_param): boo
}

foreach ($container_param->type->getAtomicTypes() as $a) {
if (($a instanceof TClosure || $a instanceof TCallable) && !$a->params) {
// must check null explicitly, since no params (empty array) would not be handled correctly otherwise
if (($a instanceof TClosure || $a instanceof TCallable) && $a->params === null) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Codebase/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public function isCallMapFunctionPure(
null,
);

if (!$function_callable->params
if (!isset($function_callable->params)
|| ($args !== null && count($args) === 0)
|| ($function_callable->return_type && $function_callable->return_type->isVoid())
) {
Expand Down
12 changes: 12 additions & 0 deletions tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2352,6 +2352,18 @@ function fooFoo(int $a): void {}
fooFoo("string");',
'error_message' => 'InvalidScalarArgument',
],
'invalidArgumentCallableWithoutArgsUnion' => [
'code' => '<?php
function foo(int $a): void {}

/**
* @param callable()|float $callable
* @return void
*/
function acme($callable) {}
acme("foo");',
'error_message' => 'InvalidArgument',
],
'invalidArgumentWithDeclareStrictTypes' => [
'code' => '<?php declare(strict_types=1);
function fooFoo(int $a): void {}
Expand Down
Loading