Skip to content

Commit

Permalink
Merge pull request #6739 from orklah/instanceof_notliteral
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Oct 25, 2021
2 parents b55924d + 8e0cf0e commit 4b0c880
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Psalm/Internal/Type/AssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ private static function refine(
if ($key
&& $code_location
&& $new_type->getId() === $existing_var_type->getId()
//even if two objects are the same, equality is not guaranteed
// example: (ErrorException and TypeError are both Throwable but not equal)
&& !$new_type->hasNamedObjectType()
&& !$is_equality
&& !($original_assertion === 'loaded-class-string' && $old_var_type_string === 'class-string')
&& (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)
Expand Down
36 changes: 36 additions & 0 deletions tests/TypeReconciliation/TypeAlgebraTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,42 @@ function a(int $a, ?int $b = null): void
}
}'
],
'ThrowableInstanceOfThrowableMayBeFalse' => [
'<?php
final class Handler
{
/**
* @var class-string<Throwable>[]
*/
private array $dontReport = [];
/**
* @param class-string<Throwable> $throwable
*/
public function dontReport(string $throwable): void
{
$this->dontReport[] = $throwable;
}
public function shouldReport(Throwable $t): bool
{
foreach ($this->dontReport as $tc) {
if ($t instanceof $tc) {
return false;
}
}
return true;
}
}
$h = new Handler();
$h->dontReport(RuntimeException::class);
$h->shouldReport(new Exception());
$h->shouldReport(new RuntimeException());'
],
];
}

Expand Down

0 comments on commit 4b0c880

Please sign in to comment.