Skip to content

Commit

Permalink
Merge pull request #10500 from kkmuffme/callable-without-args-union-n…
Browse files Browse the repository at this point in the history
…ot-handled-correctly

Fix callable without args not handled correctly
  • Loading branch information
orklah authored Jan 9, 2024
2 parents abfbded + 80dc208 commit 6c98f25
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
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 @@ -2387,6 +2387,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

0 comments on commit 6c98f25

Please sign in to comment.