1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #4699 - treat isset like !== null when variable is defined

This commit is contained in:
Matt Brown 2020-11-25 14:04:02 -05:00 committed by Daniil Gentili
parent 31249d7c85
commit 5228ff6369
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 27 additions and 4 deletions

View File

@ -486,7 +486,17 @@ class AssertionFinder
);
if ($var_name) {
$if_types[$var_name] = [['isset']];
if ($isset_var instanceof PhpParser\Node\Expr\Variable
&& $source instanceof StatementsAnalyzer
&& ($var_type = $source->node_data->getType($isset_var))
&& !$var_type->isMixed()
&& !$var_type->possibly_undefined
&& $var_name !== '$_SESSION'
) {
$if_types[$var_name] = [['!null']];
} else {
$if_types[$var_name] = [['isset']];
}
} else {
// look for any variables we *can* use for an isset assertion
$array_root = $isset_var;

View File

@ -883,9 +883,9 @@ class FunctionCallAnalyzer extends CallAnalyzer
$assert_type_assertions = Algebra::getTruthsFromFormula($simplified_clauses);
if ($assert_type_assertions) {
$changed_var_ids = [];
$changed_var_ids = [];
if ($assert_type_assertions) {
// while in an and, we allow scope to boil over to support
// statements of the form if ($x && $x->foo())
$op_vars_in_scope = Reconciler::reconcileKeyedTypes(
@ -936,6 +936,12 @@ class FunctionCallAnalyzer extends CallAnalyzer
$context->vars_in_scope = $op_vars_in_scope;
}
if ($changed_var_ids) {
$simplified_clauses = Context::removeReconciledClauses($simplified_clauses, $changed_var_ids)[0];
}
$context->clauses = $simplified_clauses;
}
/**

View File

@ -1013,6 +1013,13 @@ class IssetTest extends \Psalm\Tests\TestCase
[],
'8.0'
],
'assertComplex' => [
'<?php
function returnsInt(?int $a, ?int $b): int {
assert($a !== null || $b !== null);
return isset($a) ? $a : $b;
}'
],
];
}
@ -1090,7 +1097,7 @@ class IssetTest extends \Psalm\Tests\TestCase
}
return "bar";
}',
'error_message' => 'TypeDoesNotContainType'
'error_message' => 'TypeDoesNotContainNull'
],
'issetOnArrayOfArraysReturningString' => [
'<?php