Skip to content

Commit

Permalink
Merge pull request #9538 from weirdan/fix-assertions-on-conditions-le…
Browse files Browse the repository at this point in the history
…aking-from-else
  • Loading branch information
weirdan committed Mar 18, 2023
2 parents 531eec6 + 963dd5d commit c2c2e26
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Expand Up @@ -169,7 +169,7 @@ public static function analyze(
$original_context,
$new_assigned_var_ids,
$new_possibly_assigned_var_ids,
[],
$if_scope->if_cond_changed_var_ids,
true,
);

Expand Down
66 changes: 66 additions & 0 deletions tests/TypeReconciliation/ConditionalTest.php
Expand Up @@ -2974,6 +2974,72 @@ function bar($m): void
}
',
],
'hypotheticalElseDoesNotLeak' => [
'code' => <<<'PHP'
<?php
$a = 1;
/** @psalm-suppress RedundantCondition */
if ($a !== null) {}
PHP,
'assertions' => [
'$a===' => '1',
],
],
'ifDoesNotLeak' => [
'code' => <<<'PHP'
<?php
$a = 1;
/** @psalm-suppress TypeDoesNotContainNull */
if ($a === null) {}
PHP,
'assertions' => [
'$a===' => '1',
],
],
'ifElseDoesNotLeak' => [
'code' => <<<'PHP'
<?php
$a = 1;
/** @psalm-suppress TypeDoesNotContainNull */
if ($a === null) {
} else {
}
PHP,
'assertions' => [
'$a===' => '1',
],
],
'ifElseInvertedDoesNotLeak' => [
'code' => <<<'PHP'
<?php
$a = 1;
/** @psalm-suppress RedundantCondition */
if ($a !== null) {
} else {
}
PHP,
'assertions' => [
'$a===' => '1',
],
],
'ifNotIssetDoesNotLeakArrayAssertions' => [
'code' => <<<'PHP'
<?php
/**
* @param array{x?: int, y?: int, z?: int} $a
* @param 'x'|'y'|'z' $b
* @return void
*/
function foo( $a, $b ) {
if ( !isset( $a[ $b ] ) ) {
return;
}
echo $a[ $b ];
}
PHP,
],
'SKIPPED-ctypeLowerNarrowsIntToARange' => [
'code' => '<?php
$int = rand(-1000, 1000);
Expand Down

0 comments on commit c2c2e26

Please sign in to comment.