1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #3116 - allow class to be compared to a templated class-string

This commit is contained in:
Brown 2020-04-10 00:26:11 -04:00
parent 556d5625b5
commit 672baea6fc
2 changed files with 24 additions and 5 deletions

View File

@ -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()

View File

@ -677,6 +677,21 @@ class FunctionTemplateAssertTest extends TestCase
}
}'
],
'assertThrowsInstanceOfFunction' => [
'<?php
namespace Foo;
/**
* @template T of \Throwable
* @psalm-param class-string<T> $exceptionType
* @psalm-assert T $outerEx
*/
function assertThrowsInstanceOf(\Throwable $outerEx, string $exceptionType) : void {
if (!($outerEx instanceof $exceptionType)) {
throw new \Exception("thrown instance of wrong type");
}
}'
],
];
}