1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix support for Closure(int):int annotation

This commit is contained in:
Matthew Brown 2018-04-08 10:02:41 -04:00
parent 3fe9a0606e
commit 797cab22f8
5 changed files with 33 additions and 3 deletions

View File

@ -292,7 +292,7 @@ abstract class Type
if ($parse_tree instanceof ParseTree\CallableWithReturnTypeTree) {
$callable_type = self::getTypeFromTree($parse_tree->children[0], false);
if (!$callable_type instanceof TCallable) {
if (!$callable_type instanceof TCallable && !$callable_type instanceof Type\Atomic\Fn) {
throw new \InvalidArgumentException('Parsing callable tree node should return TCallable');
}
@ -336,7 +336,7 @@ abstract class Type
$parse_tree->children
);
if (in_array($parse_tree->value, ['closure', '\closure'], false)) {
if (in_array(strtolower($parse_tree->value), ['closure', '\closure'], true)) {
return new Type\Atomic\Fn('Closure', $params);
}

View File

@ -93,4 +93,20 @@ trait CallableTrait
return 'callable' . $param_string . $return_type_string;
}
public function getId()
{
$param_string = '';
$return_type_string = '';
if ($this->params !== null) {
$param_string = '(' . implode(', ', $this->params) . ')';
}
if ($this->return_type !== null) {
$return_type_string = ' : ' . $this->return_type;
}
return $this->value . $param_string . $return_type_string;
}
}

View File

@ -322,7 +322,7 @@ class ParseTree
break;
case '(':
if (!in_array($type_token, ['closure', 'callable', '\closure'])) {
if (!in_array(strtolower($type_token), ['closure', 'callable', '\closure'])) {
throw new TypeParseTreeException(
'Bracket must be preceded by “Closure” or “callable”'
);

View File

@ -597,6 +597,7 @@ class DependencyFinderVisitor extends PhpParser\NodeVisitorAbstract implements P
$function_id = strtolower($cased_function_id);
if ($this->codebase->register_global_functions) {
$storage = new FunctionLikeStorage();
$this->codebase->functions->addStubbedFunction($function_id, $storage);
} else {

View File

@ -293,6 +293,19 @@ class CallableTest extends TestCase
[1, 2, 3]
);',
],
'returnsTypedClosure' => [
'<?php
/**
* @param Closure(int):int $f
* @param Closure(int):int $g
* @return Closure(int):int
*/
function foo(Closure $f, Closure $g) : Closure {
return function (int $x) use ($f, $g) : int {
return $f($g($x));
}
}'
],
];
}