From 584344fda3f8f787e10910eb8187f0863ab19a2e Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 30 Jan 2022 15:17:18 +0100 Subject: [PATCH 1/3] Failing test --- tests/ReturnTypeProvider/ExceptionCodeTest.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/ReturnTypeProvider/ExceptionCodeTest.php b/tests/ReturnTypeProvider/ExceptionCodeTest.php index ccc8ea10db5..84e8acb28e5 100644 --- a/tests/ReturnTypeProvider/ExceptionCodeTest.php +++ b/tests/ReturnTypeProvider/ExceptionCodeTest.php @@ -35,14 +35,23 @@ function f(\PDOException $e): string { ', [], ]; - yield 'Exception' => [ + yield 'CustomThrowable' => [ + 'getCode(); + ', + ['$code' => 'int'], + ]; + yield 'Throwable' => [ 'getCode(); ', ['$code' => 'int|string'], ]; - yield 'Throwable' => [ + yield 'Exception' => [ 'getCode(); From cd3213201d1cbccd3aa1bc4892fc4b2a3cadd60b Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 30 Jan 2022 15:30:40 +0100 Subject: [PATCH 2/3] Fix --- .../Expression/Call/Method/MethodCallReturnTypeFetcher.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php index c4779156a9e..9f8abaa7973 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php @@ -97,7 +97,11 @@ public static function fetch( if ($premixin_method_id->method_name === 'getcode' && $premixin_method_id->fq_class_name !== Exception::class - && in_array(Throwable::class, $class_storage->class_implements)) { + && ( + in_array(Throwable::class, $class_storage->class_implements) + || $codebase->interfaceExtends($premixin_method_id->fq_class_name, Throwable::class) + ) + ) { if ($premixin_method_id->fq_class_name === PDOException::class) { return Type::getString(); } else { From 854a3412fe58643b243f65f32a10df191ab409f9 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 30 Jan 2022 15:32:55 +0100 Subject: [PATCH 3/3] Use function --- .../Expression/Call/Method/MethodCallReturnTypeFetcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php index 9f8abaa7973..a5dc24d171c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php @@ -98,7 +98,7 @@ public static function fetch( if ($premixin_method_id->method_name === 'getcode' && $premixin_method_id->fq_class_name !== Exception::class && ( - in_array(Throwable::class, $class_storage->class_implements) + $codebase->classImplements($premixin_method_id->fq_class_name, Throwable::class) || $codebase->interfaceExtends($premixin_method_id->fq_class_name, Throwable::class) ) ) {