diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 543d6e30b3..880b195123 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -2503,7 +2503,11 @@ public function unsetExpression(Expr $expr): self ); } - return $this->invalidateExpression($expr->var)->invalidateExpression(new FuncCall(new Name('count'), [new Node\Arg($expr->var)])); + $args = [new Node\Arg($expr->var)]; + + return $this->invalidateExpression($expr->var) + ->invalidateExpression(new FuncCall(new Name\FullyQualified('count'), $args)) + ->invalidateExpression(new FuncCall(new Name('count'), $args)); } return $this; diff --git a/tests/PHPStan/Analyser/data/bug-2648.php b/tests/PHPStan/Analyser/data/bug-2648.php index 9bc3e2f523..1013149503 100644 --- a/tests/PHPStan/Analyser/data/bug-2648.php +++ b/tests/PHPStan/Analyser/data/bug-2648.php @@ -34,6 +34,8 @@ public function doBar(array $list): void assertType('int', count($list)); } + assertType('int', count($list)); + if (count($list) === 1) { assertType('int', count($list)); break; diff --git a/tests/PHPStan/Rules/Comparison/NumberComparisonOperatorsConstantConditionRuleTest.php b/tests/PHPStan/Rules/Comparison/NumberComparisonOperatorsConstantConditionRuleTest.php index 1732c56769..aa828378d7 100644 --- a/tests/PHPStan/Rules/Comparison/NumberComparisonOperatorsConstantConditionRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/NumberComparisonOperatorsConstantConditionRuleTest.php @@ -34,4 +34,14 @@ public function testRule(): void ]); } + public function testBug2648(): void + { + $this->analyse([__DIR__ . '/data/bug-2648-rule.php'], []); + } + + public function testBug2648Namespace(): void + { + $this->analyse([__DIR__ . '/data/bug-2648-namespace-rule.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/bug-2648-namespace-rule.php b/tests/PHPStan/Rules/Comparison/data/bug-2648-namespace-rule.php new file mode 100644 index 0000000000..c969d7e9c2 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-2648-namespace-rule.php @@ -0,0 +1,16 @@ + $foo */ +$foo = $_GET['bar']; + +if (count($foo) > 0) { + foreach ($foo as $key => $value) { + unset($foo[$key]); + } + + if(count($foo) > 0) { + // $foo is actually empty now + } +} diff --git a/tests/PHPStan/Rules/Comparison/data/bug-2648-rule.php b/tests/PHPStan/Rules/Comparison/data/bug-2648-rule.php new file mode 100644 index 0000000000..ebd83be1be --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-2648-rule.php @@ -0,0 +1,14 @@ + $foo */ +$foo = $_GET['bar']; + +if (count($foo) > 0) { + foreach ($foo as $key => $value) { + unset($foo[$key]); + } + + if(count($foo) > 0) { + // $foo is actually empty now + } +}