Skip to content

Commit

Permalink
fix usage of callable with array_map (#5373)
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah authored Mar 12, 2021
1 parent 57234ab commit 90fd1c5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$generic_key_type = Type::getInt();
}

if ($closure_types = $function_call_type->getClosureTypes()) {
if ($function_call_type->hasCallableType()) {
$closure_types = $function_call_type->getClosureTypes() ?: $function_call_type->getCallableTypes();
$closure_atomic_type = \reset($closure_types);

$closure_return_type = $closure_atomic_type->return_type ?: Type::getMixed();
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ public function hasCallableType(): bool
/**
* @return array<string, Atomic\TCallable>
*/
private function getCallableTypes(): array
public function getCallableTypes(): array
{
return array_filter(
$this->types,
Expand Down
19 changes: 19 additions & 0 deletions tests/ClosureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,25 @@ function () use ($a): void {
}
}'
],
'CallableWithArrayMap' => [
'<?php
/**
* @psalm-template T
* @param class-string<T> $className
* @return callable(...mixed):T
*/
function maker(string $className) {
return function(...$args) use ($className) {
/** @psalm-suppress MixedMethodCall */
return new $className(...$args);
};
}
$maker = maker(stdClass::class);
$result = array_map($maker, ["abc"]);',
'assertions' => [
'$result' => 'array{stdClass}'
],
],
];
}

Expand Down

0 comments on commit 90fd1c5

Please sign in to comment.