1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +01:00

Fix #4637 - prevent regression when negating function call with === false

This commit is contained in:
Matt Brown 2020-11-20 09:56:12 -05:00 committed by Daniil Gentili
parent ea089d9696
commit 068907327d
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 55 additions and 53 deletions

View File

@ -753,7 +753,7 @@ class AssertionFinder
} }
if ($base_conditional instanceof PhpParser\Node\Expr\FuncCall) { if ($base_conditional instanceof PhpParser\Node\Expr\FuncCall) {
$if_types = self::processFunctionCall( $notif_types = self::processFunctionCall(
$base_conditional, $base_conditional,
$this_class_name, $this_class_name,
$source, $source,
@ -773,15 +773,17 @@ class AssertionFinder
} else { } else {
$if_types[$var_name] = [['falsy']]; $if_types[$var_name] = [['falsy']];
} }
$notif_types = [];
} else { } else {
$base_assertions = null; $notif_types = null;
if ($source instanceof StatementsAnalyzer && $cache) { if ($source instanceof StatementsAnalyzer && $cache) {
$base_assertions = $source->node_data->getAssertions($base_conditional); $notif_types = $source->node_data->getAssertions($base_conditional);
} }
if ($base_assertions === null) { if ($notif_types === null) {
$base_assertions = self::scrapeAssertions( $notif_types = self::scrapeAssertions(
$base_conditional, $base_conditional,
$this_class_name, $this_class_name,
$source, $source,
@ -792,24 +794,22 @@ class AssertionFinder
); );
if ($source instanceof StatementsAnalyzer && $cache) { if ($source instanceof StatementsAnalyzer && $cache) {
$source->node_data->setAssertions($base_conditional, $base_assertions); $source->node_data->setAssertions($base_conditional, $notif_types);
}
}
$notif_types = $base_assertions;
if (count($notif_types) === 1) {
$notif_types = $notif_types[0];
if (count($notif_types) === 1) {
$if_types = \Psalm\Internal\Algebra::negateTypes($notif_types);
} }
} }
} }
$if_types = $if_types ? [$if_types] : [];
} }
if (count($notif_types) === 1) {
$notif_types = $notif_types[0];
if (count($notif_types) === 1) {
$if_types = \Psalm\Internal\Algebra::negateTypes($notif_types);
}
}
$if_types = $if_types ? [$if_types] : [];
if ($codebase if ($codebase
&& $source instanceof StatementsAnalyzer && $source instanceof StatementsAnalyzer
&& ($var_type = $source->node_data->getType($base_conditional)) && ($var_type = $source->node_data->getType($base_conditional))
@ -1342,15 +1342,17 @@ class AssertionFinder
} else { } else {
$if_types[$var_name] = [['!falsy']]; $if_types[$var_name] = [['!falsy']];
} }
$if_types = [$if_types];
} else { } else {
$base_assertions = null; $if_types = null;
if ($source instanceof StatementsAnalyzer && $cache) { if ($source instanceof StatementsAnalyzer && $cache) {
$base_assertions = $source->node_data->getAssertions($base_conditional); $if_types = $source->node_data->getAssertions($base_conditional);
} }
if ($base_assertions === null) { if ($if_types === null) {
$base_assertions = self::scrapeAssertions( $if_types = self::scrapeAssertions(
$base_conditional, $base_conditional,
$this_class_name, $this_class_name,
$source, $source,
@ -1361,17 +1363,7 @@ class AssertionFinder
); );
if ($source instanceof StatementsAnalyzer && $cache) { if ($source instanceof StatementsAnalyzer && $cache) {
$source->node_data->setAssertions($base_conditional, $base_assertions); $source->node_data->setAssertions($base_conditional, $if_types);
}
}
$notif_types = $base_assertions;
if (count($notif_types) === 1) {
$notif_types = $notif_types[0];
if (count($notif_types) === 1) {
$if_types = \Psalm\Internal\Algebra::negateTypes($notif_types);
} }
} }
} }
@ -1419,7 +1411,7 @@ class AssertionFinder
} }
} }
return $if_types ? [$if_types] : []; return $if_types;
} }
$true_position = self::hasTrueVariable($conditional); $true_position = self::hasTrueVariable($conditional);
@ -1434,12 +1426,12 @@ class AssertionFinder
} }
if ($base_conditional instanceof PhpParser\Node\Expr\FuncCall) { if ($base_conditional instanceof PhpParser\Node\Expr\FuncCall) {
$if_types = self::processFunctionCall( $notif_types = self::processFunctionCall(
$base_conditional, $base_conditional,
$this_class_name, $this_class_name,
$source, $source,
$codebase, $codebase,
$inside_negation !$inside_negation
); );
} else { } else {
$var_name = ExpressionIdentifier::getArrayVarId( $var_name = ExpressionIdentifier::getArrayVarId(
@ -1454,15 +1446,17 @@ class AssertionFinder
} else { } else {
$if_types[$var_name] = [['falsy']]; $if_types[$var_name] = [['falsy']];
} }
$notif_types = [];
} else { } else {
$base_assertions = null; $notif_types = null;
if ($source instanceof StatementsAnalyzer && $cache) { if ($source instanceof StatementsAnalyzer && $cache) {
$base_assertions = $source->node_data->getAssertions($base_conditional); $notif_types = $source->node_data->getAssertions($base_conditional);
} }
if ($base_assertions === null) { if ($notif_types === null) {
$base_assertions = self::scrapeAssertions( $notif_types = self::scrapeAssertions(
$base_conditional, $base_conditional,
$this_class_name, $this_class_name,
$source, $source,
@ -1473,24 +1467,22 @@ class AssertionFinder
); );
if ($source instanceof StatementsAnalyzer && $cache) { if ($source instanceof StatementsAnalyzer && $cache) {
$source->node_data->setAssertions($base_conditional, $base_assertions); $source->node_data->setAssertions($base_conditional, $notif_types);
}
}
$notif_types = $base_assertions;
if (count($notif_types) === 1) {
$notif_types = $notif_types[0];
if (count($notif_types) === 1) {
$if_types = \Psalm\Internal\Algebra::negateTypes($notif_types);
} }
} }
} }
$if_types = $if_types ? [$if_types] : [];
} }
if (count($notif_types) === 1) {
$notif_types = $notif_types[0];
if (count($notif_types) === 1) {
$if_types = \Psalm\Internal\Algebra::negateTypes($notif_types);
}
}
$if_types = $if_types ? [$if_types] : [];
if ($codebase if ($codebase
&& $source instanceof StatementsAnalyzer && $source instanceof StatementsAnalyzer
&& ($var_type = $source->node_data->getType($base_conditional)) && ($var_type = $source->node_data->getType($base_conditional))

View File

@ -2596,6 +2596,16 @@ class ConditionalTest extends \Psalm\Tests\TestCase
return true; return true;
}' }'
], ],
'negateIsNull' => [
'<?php
function scope(?string $str): string{
if (is_null($str) === false){
return $str;
}
return "";
}'
],
]; ];
} }