From 672baea6fc16e0ab2adca3d8f6836b3b0fcd8796 Mon Sep 17 00:00:00 2001 From: Brown Date: Fri, 10 Apr 2020 00:26:11 -0400 Subject: [PATCH] Fix #3116 - allow class to be compared to a templated class-string --- .../Statements/Expression/AssertionFinder.php | 14 +++++++++----- tests/Template/FunctionTemplateAssertTest.php | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index e9240125a..45aed5fd1 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -81,9 +81,11 @@ class AssertionFinder continue; } - $instanceof_type = new Type\Union([ - new Type\Atomic\TNamedObject($instanceof_type) - ]); + $instanceof_type = Type::parseString( + $instanceof_type, + null, + $source->getTemplateTypeMap() ?: [] + ); if (!TypeAnalyzer::canExpressionTypesBeIdentical( $codebase, @@ -93,7 +95,8 @@ class AssertionFinder if ($var_type->from_docblock) { if (IssueBuffer::accepts( new RedundantConditionGivenDocblockType( - $var_type->getId() . ' does not contain ' . $instanceof_type, + $var_type->getId() . ' does not contain ' + . $instanceof_type->getId(), new CodeLocation($source, $conditional) ), $source->getSuppressedIssues() @@ -103,7 +106,8 @@ class AssertionFinder } else { if (IssueBuffer::accepts( new RedundantCondition( - $var_type->getId() . ' cannot be identical to ' . $instanceof_type, + $var_type->getId() . ' cannot be identical to ' + . $instanceof_type->getId(), new CodeLocation($source, $conditional) ), $source->getSuppressedIssues() diff --git a/tests/Template/FunctionTemplateAssertTest.php b/tests/Template/FunctionTemplateAssertTest.php index 911e8f619..477a48fe5 100644 --- a/tests/Template/FunctionTemplateAssertTest.php +++ b/tests/Template/FunctionTemplateAssertTest.php @@ -677,6 +677,21 @@ class FunctionTemplateAssertTest extends TestCase } }' ], + 'assertThrowsInstanceOfFunction' => [ + ' $exceptionType + * @psalm-assert T $outerEx + */ + function assertThrowsInstanceOf(\Throwable $outerEx, string $exceptionType) : void { + if (!($outerEx instanceof $exceptionType)) { + throw new \Exception("thrown instance of wrong type"); + } + }' + ], ]; }