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

Fix #5020 - remove previous catch var assertions when assigning inside catch

This commit is contained in:
Matt Brown 2021-01-17 14:28:28 -05:00
parent bca58863ad
commit 2f58c6afc8
3 changed files with 47 additions and 0 deletions

View File

@ -630,6 +630,10 @@ class Context
} }
} }
/**
* This method is used after assignments to variables to remove any existing
* items in $vars_in_scope that are now made redundant by an update to some data
*/
public function removeDescendents( public function removeDescendents(
string $remove_var_id, string $remove_var_id,
?Union $existing_type = null, ?Union $existing_type = null,

View File

@ -306,6 +306,14 @@ class TryAnalyzer
) )
); );
// removes dependent vars from $context
$catch_context->removeDescendents(
$catch_var_id,
null,
$catch_context->vars_in_scope[$catch_var_id],
$statements_analyzer
);
$catch_context->vars_possibly_in_scope[$catch_var_id] = true; $catch_context->vars_possibly_in_scope[$catch_var_id] = true;
$location = new CodeLocation($statements_analyzer->getSource(), $catch->var); $location = new CodeLocation($statements_analyzer->getSource(), $catch->var);

View File

@ -732,6 +732,41 @@ class FunctionTemplateAssertTest extends TestCase
} }
}' }'
], ],
'assertSameOnMemoizedMethodCall' => [
'<?php
function testValidUsername(): void {
try {
validateUsername("123");
throw new Exception("Failed to throw exception for short username");
} catch (Exception $e) {
assertSame("a", $e->getMessage());
}
try {
validateUsername("invalid#1");
} catch (Exception $e) {
assertSame("b", $e->getMessage());
}
}
/**
* @psalm-template ExpectedType
* @psalm-param ExpectedType $expected
* @psalm-param mixed $actual
* @psalm-assert =ExpectedType $actual
*/
function assertSame($expected, $actual): void {
if ($actual !== $expected) {
throw new Exception("Bad");
}
}
function validateUsername(string $username): void {
if (strlen($username) < 5) {
throw new Exception("Username must be at least 5 characters long");
}
}'
],
]; ];
} }