Skip to content

Commit 81b1afa

Browse files
committed
Fixed invalidating function call to count() outside of namespaces
1 parent 3a5cd06 commit 81b1afa

5 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/Analyser/MutatingScope.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2503,7 +2503,11 @@ public function unsetExpression(Expr $expr): self
25032503
);
25042504
}
25052505

2506-
return $this->invalidateExpression($expr->var)->invalidateExpression(new FuncCall(new Name('count'), [new Node\Arg($expr->var)]));
2506+
$args = [new Node\Arg($expr->var)];
2507+
2508+
return $this->invalidateExpression($expr->var)
2509+
->invalidateExpression(new FuncCall(new Name\FullyQualified('count'), $args))
2510+
->invalidateExpression(new FuncCall(new Name('count'), $args));
25072511
}
25082512

25092513
return $this;

tests/PHPStan/Analyser/data/bug-2648.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function doBar(array $list): void
3434
assertType('int', count($list));
3535
}
3636

37+
assertType('int', count($list));
38+
3739
if (count($list) === 1) {
3840
assertType('int', count($list));
3941
break;

tests/PHPStan/Rules/Comparison/NumberComparisonOperatorsConstantConditionRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,14 @@ public function testRule(): void
3434
]);
3535
}
3636

37+
public function testBug2648(): void
38+
{
39+
$this->analyse([__DIR__ . '/data/bug-2648-rule.php'], []);
40+
}
41+
42+
public function testBug2648Namespace(): void
43+
{
44+
$this->analyse([__DIR__ . '/data/bug-2648-namespace-rule.php'], []);
45+
}
46+
3747
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bug2648Rule;
4+
5+
/** @var array<string> $foo */
6+
$foo = $_GET['bar'];
7+
8+
if (count($foo) > 0) {
9+
foreach ($foo as $key => $value) {
10+
unset($foo[$key]);
11+
}
12+
13+
if(count($foo) > 0) {
14+
// $foo is actually empty now
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/** @var array<string> $foo */
4+
$foo = $_GET['bar'];
5+
6+
if (count($foo) > 0) {
7+
foreach ($foo as $key => $value) {
8+
unset($foo[$key]);
9+
}
10+
11+
if(count($foo) > 0) {
12+
// $foo is actually empty now
13+
}
14+
}

0 commit comments

Comments
 (0)