Skip to content

Commit

Permalink
Fixed invalidating function call to count() outside of namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 5, 2020
1 parent 3a5cd06 commit 81b1afa
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2648.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'], []);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-2648-namespace-rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Bug2648Rule;

/** @var array<string> $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
}
}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-2648-rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/** @var array<string> $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
}
}

0 comments on commit 81b1afa

Please sign in to comment.