Skip to content

Commit

Permalink
Merge pull request #6746 from orklah/instanceof-classstring
Browse files Browse the repository at this point in the history
throwable Instanceof throwable  negated
  • Loading branch information
orklah committed Oct 27, 2021
2 parents 6fba5eb + dc2ac42 commit 7807a85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Expand Up @@ -285,6 +285,12 @@ public static function reconcile(
$existing_var_type->addType(new TString);
} elseif (strpos($assertion, 'getclass-') === 0) {
$assertion = substr($assertion, 9);
} elseif ($existing_var_type->isSingle()
&& $existing_var_type->hasNamedObjectType()
&& isset($existing_var_type->getAtomicTypes()[$assertion])
) {
// checking if two types share a common parent is not enough to guarantee childs are instanceof each other
// fall through
} elseif (!$is_equality) {
$codebase = $statements_analyzer->getCodebase();

Expand Down
16 changes: 16 additions & 0 deletions tests/TypeReconciliation/TypeAlgebraTest.php
Expand Up @@ -1138,6 +1138,22 @@ public function shouldReport(Throwable $t): bool
$h->shouldReport(new Exception());
$h->shouldReport(new RuntimeException());'
],
'ThrowableInstanceOfThrowableMayBeTrue' => [
'<?php
class Mapper
{
/** @param class-string<Throwable> $class */
final public function map(Throwable $throwable, string $class): ?Throwable
{
if (! $throwable instanceof $class) {
return null;
}
return $throwable;
}
}'
],
];
}

Expand Down

0 comments on commit 7807a85

Please sign in to comment.