mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Ignore exceptions already caught by previous catch
This commit is contained in:
parent
6d21520628
commit
a5da866d97
@ -120,6 +120,7 @@ class TryAnalyzer
|
||||
}
|
||||
|
||||
$try_context->vars_possibly_in_scope = $context->vars_possibly_in_scope;
|
||||
$try_context->possibly_thrown_exceptions = $context->possibly_thrown_exceptions;
|
||||
|
||||
$context->referenced_var_ids = array_intersect_key(
|
||||
$try_context->referenced_var_ids,
|
||||
@ -258,40 +259,26 @@ class TryAnalyzer
|
||||
foreach ($fq_catch_classes as $fq_catch_class) {
|
||||
$fq_catch_class_lower = strtolower($fq_catch_class);
|
||||
|
||||
foreach ($context->possibly_thrown_exceptions as $exception_fqcln => $_) {
|
||||
foreach ($catch_context->possibly_thrown_exceptions as $exception_fqcln => $_) {
|
||||
$exception_fqcln_lower = strtolower($exception_fqcln);
|
||||
|
||||
if ($exception_fqcln_lower === $fq_catch_class_lower) {
|
||||
unset($context->possibly_thrown_exceptions[$exception_fqcln]);
|
||||
unset($catch_context->possibly_thrown_exceptions[$exception_fqcln]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($codebase->classExists($exception_fqcln)
|
||||
&& $codebase->classExtendsOrImplements(
|
||||
$exception_fqcln,
|
||||
$fq_catch_class
|
||||
)
|
||||
if ($exception_fqcln_lower === $fq_catch_class_lower
|
||||
|| ($codebase->classExists($exception_fqcln)
|
||||
&& $codebase->classExtendsOrImplements($exception_fqcln, $fq_catch_class))
|
||||
|| ($codebase->interfaceExists($exception_fqcln)
|
||||
&& $codebase->interfaceExtends($exception_fqcln, $fq_catch_class))
|
||||
) {
|
||||
unset($original_context->possibly_thrown_exceptions[$exception_fqcln]);
|
||||
unset($context->possibly_thrown_exceptions[$exception_fqcln]);
|
||||
unset($catch_context->possibly_thrown_exceptions[$exception_fqcln]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($codebase->interfaceExists($exception_fqcln)
|
||||
&& $codebase->interfaceExtends(
|
||||
$exception_fqcln,
|
||||
$fq_catch_class
|
||||
)
|
||||
) {
|
||||
unset($context->possibly_thrown_exceptions[$exception_fqcln]);
|
||||
unset($catch_context->possibly_thrown_exceptions[$exception_fqcln]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$context->mergeExceptions($catch_context);
|
||||
/**
|
||||
* @var array<string, array<array-key, CodeLocation>>
|
||||
*/
|
||||
$catch_context->possibly_thrown_exceptions = [];
|
||||
}
|
||||
|
||||
$catch_var_id = '$' . $catch_var_name;
|
||||
|
@ -656,4 +656,31 @@ class ThrowsAnnotationTest extends TestCase
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
|
||||
public function testNextCatchShouldIgnoreExceptionsCaughtByPreviousCatch(): void
|
||||
{
|
||||
Config::getInstance()->check_for_throws_docblock = true;
|
||||
|
||||
$this->addFile(
|
||||
'somefile.php',
|
||||
'<?php
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function method(): void
|
||||
{
|
||||
try {
|
||||
throw new \LogicException();
|
||||
} catch (\LogicException $e) {
|
||||
throw new \RuntimeException();
|
||||
} catch (\Exception $e) {
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
}'
|
||||
);
|
||||
|
||||
$context = new Context();
|
||||
|
||||
$this->analyzeFile('somefile.php', $context);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user