1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Merge pull request #6739 from orklah/instanceof_notliteral

This commit is contained in:
Bruce Weirdan 2021-10-25 23:45:17 +03:00 committed by GitHub
commit 4b0c8806a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -457,6 +457,9 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
if ($key if ($key
&& $code_location && $code_location
&& $new_type->getId() === $existing_var_type->getId() && $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 && !$is_equality
&& !($original_assertion === 'loaded-class-string' && $old_var_type_string === 'class-string') && !($original_assertion === 'loaded-class-string' && $old_var_type_string === 'class-string')
&& (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer) && (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)

View File

@ -1102,6 +1102,42 @@ class TypeAlgebraTest extends \Psalm\Tests\TestCase
} }
}' }'
], ],
'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());'
],
]; ];
} }