diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index bbacec809..648c53b40 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -567,41 +567,40 @@ class AssertionFinder throw new \UnexpectedValueException('$false_position value'); } + $var_type = isset($base_conditional->inferredType) ? $base_conditional->inferredType : null; + if ($base_conditional instanceof PhpParser\Node\Expr\FuncCall) { - $conditional->assertions = self::processFunctionCall( + $if_types = self::processFunctionCall( $base_conditional, $this_class_name, $source, true ); - return; - } + } else { + $var_name = ExpressionAnalyzer::getArrayVarId( + $base_conditional, + $this_class_name, + $source + ); - $var_name = ExpressionAnalyzer::getArrayVarId( - $base_conditional, - $this_class_name, - $source - ); + if ($var_name) { + if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) { + $if_types[$var_name] = [['false']]; + } else { + $if_types[$var_name] = [['falsy']]; + } + } elseif ($var_type) { + self::scrapeAssertions($base_conditional, $this_class_name, $source, $codebase, $inside_negation); - $var_type = isset($base_conditional->inferredType) ? $base_conditional->inferredType : null; + if (!isset($base_conditional->assertions)) { + throw new \UnexpectedValueException('Assertions should be set'); + } - if ($var_name) { - if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) { - $if_types[$var_name] = [['false']]; - } else { - $if_types[$var_name] = [['falsy']]; - } - } elseif ($var_type) { - self::scrapeAssertions($base_conditional, $this_class_name, $source, $codebase, $inside_negation); + $notif_types = $base_conditional->assertions; - if (!isset($base_conditional->assertions)) { - throw new \UnexpectedValueException('Assertions should be set'); - } - - $notif_types = $base_conditional->assertions; - - if (count($notif_types) === 1) { - $if_types = \Psalm\Type\Algebra::negateTypes($notif_types); + if (count($notif_types) === 1) { + $if_types = \Psalm\Type\Algebra::negateTypes($notif_types); + } } } diff --git a/tests/TypeReconciliationTest.php b/tests/TypeReconciliationTest.php index 80b6a933c..6233e84fa 100644 --- a/tests/TypeReconciliationTest.php +++ b/tests/TypeReconciliationTest.php @@ -1579,6 +1579,20 @@ class TypeReconciliationTest extends TestCase if (true !== firstChar("sdf")) {}', 'error_message' => 'RedundantConditionGivenDocblockType', ], + 'preventAlwaysImpossibleComparisonToFalse' => [ + ' 'TypeDoesNotContainType', + ], + 'preventAlwaysPossibleComparisonToFalse' => [ + ' 'RedundantCondition', + ], ]; } }