Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't emit redundant error when checking a named type against itself #6739

Merged
merged 2 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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