1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Support __invoke return types

This commit is contained in:
Matthew Brown 2018-05-08 19:49:25 -04:00
parent 9a9f6d1856
commit e50ef8bf03
2 changed files with 61 additions and 0 deletions

View File

@ -140,6 +140,20 @@ class FunctionCallChecker extends \Psalm\Checker\Statements\Expression\CallCheck
) === false) {
return false;
}
$invokable_return_type = $codebase->methods->getMethodReturnType(
$var_type_part->value . '::__invoke',
$var_type_part->value
);
if (isset($stmt->inferredType)) {
$stmt->inferredType = Type::combineUnionTypes(
$invokable_return_type ?: Type::getMixed(),
$stmt->inferredType
);
} else {
$stmt->inferredType = $invokable_return_type ?: Type::getMixed();
}
}
}

View File

@ -485,6 +485,53 @@ class CallableTest extends TestCase
function doSomething($p): void {}
doSomething(function(): bool { return false; });',
],
'callableProperties' => [
'<?php
class C {
/** @psalm-var callable():bool */
private $callable;
/**
* @psalm-param callable():bool $callable
*/
public function __construct(callable $callable) {
$this->callable = $callable;
}
public function callTheCallableDirectly(): bool {
return ($this->callable)();
}
public function callTheCallableIndirectly(): bool {
$r = $this->callable;
return $r();
}
}',
],
'invokableProperties' => [
'<?php
class A {
public function __invoke(): bool { return true; }
}
class C {
/** @var A $invokable */
private $invokable;
public function __construct(A $invokable) {
$this->invokable = $invokable;
}
public function callTheInvokableDirectly(): bool {
return ($this->invokable)();
}
public function callTheInvokableIndirectly(): bool {
$r = $this->invokable;
return $r();
}
}',
]
];
}