From e50ef8bf03c799f826e6fe724308826a954a6d48 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 8 May 2018 19:49:25 -0400 Subject: [PATCH] Support __invoke return types --- .../Expression/Call/FunctionCallChecker.php | 14 ++++++ tests/CallableTest.php | 47 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/Psalm/Checker/Statements/Expression/Call/FunctionCallChecker.php b/src/Psalm/Checker/Statements/Expression/Call/FunctionCallChecker.php index 7fe8605c7..4b27f5c03 100644 --- a/src/Psalm/Checker/Statements/Expression/Call/FunctionCallChecker.php +++ b/src/Psalm/Checker/Statements/Expression/Call/FunctionCallChecker.php @@ -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(); + } } } diff --git a/tests/CallableTest.php b/tests/CallableTest.php index 5e5a8438b..3587d0da1 100644 --- a/tests/CallableTest.php +++ b/tests/CallableTest.php @@ -485,6 +485,53 @@ class CallableTest extends TestCase function doSomething($p): void {} doSomething(function(): bool { return false; });', ], + 'callableProperties' => [ + 'callable = $callable; + } + + public function callTheCallableDirectly(): bool { + return ($this->callable)(); + } + + public function callTheCallableIndirectly(): bool { + $r = $this->callable; + return $r(); + } + }', + ], + 'invokableProperties' => [ + 'invokable = $invokable; + } + + public function callTheInvokableDirectly(): bool { + return ($this->invokable)(); + } + + public function callTheInvokableIndirectly(): bool { + $r = $this->invokable; + return $r(); + } + }', + ] ]; }