From f789ab065564b7b2f05b7292c095684d06caf958 Mon Sep 17 00:00:00 2001 From: orklah Date: Sun, 12 Sep 2021 11:33:23 +0200 Subject: [PATCH] add test and allow checking in already inferred types instead of just relying on statements from PHPParser for assertions --- .../Statements/Expression/AssertionFinder.php | 36 ++++++++++++++++--- tests/IntRangeTest.php | 12 +++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index 707d3ec90..911e33504 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -1562,13 +1562,20 @@ class AssertionFinder * @return false|int */ protected static function hasSuperiorNumberCheck( + FileSource $source, PhpParser\Node\Expr\BinaryOp $conditional, ?int &$literal_value_comparison, bool &$isset_assert ) { $right_assignment = false; $value_right = null; - if ($conditional->right instanceof LNumber) { + if ($source instanceof StatementsAnalyzer + && ($type = $source->node_data->getType($conditional->right)) + && $type->isSingleIntLiteral() + ) { + $right_assignment = true; + $value_right = $type->getSingleIntLiteral()->value; + } elseif ($conditional->right instanceof LNumber) { $right_assignment = true; $value_right = $conditional->right->value; } elseif ($conditional->right instanceof UnaryMinus && $conditional->right->expr instanceof LNumber) { @@ -1589,7 +1596,13 @@ class AssertionFinder $left_assignment = false; $value_left = null; - if ($conditional->left instanceof LNumber) { + if ($source instanceof StatementsAnalyzer + && ($type = $source->node_data->getType($conditional->left)) + && $type->isSingleIntLiteral() + ) { + $left_assignment = true; + $value_left = $type->getSingleIntLiteral()->value; + } elseif ($conditional->left instanceof LNumber) { $left_assignment = true; $value_left = $conditional->left->value; } elseif ($conditional->left instanceof UnaryMinus && $conditional->left->expr instanceof LNumber) { @@ -1616,13 +1629,20 @@ class AssertionFinder * @return false|int */ protected static function hasInferiorNumberCheck( + FileSource $source, PhpParser\Node\Expr\BinaryOp $conditional, ?int &$literal_value_comparison, bool &$isset_assert ) { $right_assignment = false; $value_right = null; - if ($conditional->right instanceof LNumber) { + if ($source instanceof StatementsAnalyzer + && ($type = $source->node_data->getType($conditional->right)) + && $type->isSingleIntLiteral() + ) { + $right_assignment = true; + $value_right = $type->getSingleIntLiteral()->value; + } elseif ($conditional->right instanceof LNumber) { $right_assignment = true; $value_right = $conditional->right->value; } elseif ($conditional->right instanceof UnaryMinus && $conditional->right->expr instanceof LNumber) { @@ -1642,7 +1662,13 @@ class AssertionFinder $left_assignment = false; $value_left = null; - if ($conditional->left instanceof LNumber) { + if ($source instanceof StatementsAnalyzer + && ($type = $source->node_data->getType($conditional->left)) + && $type->isSingleIntLiteral() + ) { + $left_assignment = true; + $value_left = $type->getSingleIntLiteral()->value; + } elseif ($conditional->left instanceof LNumber) { $left_assignment = true; $value_left = $conditional->left->value; } elseif ($conditional->left instanceof UnaryMinus && $conditional->left->expr instanceof LNumber) { @@ -3676,6 +3702,7 @@ class AssertionFinder $isset_assert = false; $superior_value_comparison = null; $superior_value_position = self::hasSuperiorNumberCheck( + $source, $conditional, $superior_value_comparison, $isset_assert @@ -3791,6 +3818,7 @@ class AssertionFinder $isset_assert = false; $inferior_value_comparison = null; $inferior_value_position = self::hasInferiorNumberCheck( + $source, $conditional, $inferior_value_comparison, $isset_assert diff --git a/tests/IntRangeTest.php b/tests/IntRangeTest.php index b199b800b..a850e8281 100644 --- a/tests/IntRangeTest.php +++ b/tests/IntRangeTest.php @@ -58,6 +58,18 @@ class IntRangeTest extends TestCase '$c===' => 'int<-499, -61>', ] ], + 'complexAssertions' => [ + '= 495 + 5); + $b = 5000; + assert($a < $b); + ', + 'assertions' => [ + '$a===' => 'int<500, 4999>', + ] + ], 'negatedAssertions' => [ '