diff --git a/Hooks/MockReturnTypeUpdater.php b/Hooks/MockReturnTypeUpdater.php index 5d44fd8..dec3323 100644 --- a/Hooks/MockReturnTypeUpdater.php +++ b/Hooks/MockReturnTypeUpdater.php @@ -17,7 +17,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve $return_type_candidate = $event->getReturnTypeCandidate(); $expr = $event->getExpr(); $method_id = $event->getMethodId(); - if ($return_type_candidate && $method_id === 'Mockery::mock' && isset($expr->args[0])) { + if ($return_type_candidate && self::isMockMethod($method_id) && isset($expr->args[0])) { $first_arg = $expr->args[0]->value; $fq_class_name = null; @@ -65,4 +65,9 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve } } } + + private static function isMockMethod(string $method_id): bool + { + return $method_id === 'Mockery::mock' || $method_id === 'Mockery::spy'; + } } diff --git a/tests/acceptance/MockReturn.feature b/tests/acceptance/MockReturn.feature index c7a023b..12b396a 100644 --- a/tests/acceptance/MockReturn.feature +++ b/tests/acceptance/MockReturn.feature @@ -88,3 +88,19 @@ Feature: MockReturn """ When I run Psalm Then I see no errors + + Scenario: Class spying is recognized + Given I have the following code + """ + class User + { + } + function foo(User $user): User + { + return $user; + } + + $_user = foo(Mockery::spy(User::class)); + """ + When I run Psalm + Then I see no errors