Skip to content

Commit

Permalink
Fix incorrect array sizeof() when using unset
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Feb 7, 2022
1 parent b464b21 commit bd4f179
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Analyser/MutatingScope.php
Expand Up @@ -4100,6 +4100,8 @@ public function unsetExpression(Expr $expr): self
$this->getType($expr->var)->unsetOffset($this->getType($expr->dim)),
)->invalidateExpression(
new FuncCall(new FullyQualified('count'), [new Arg($expr->var)]),
)->invalidateExpression(
new FuncCall(new FullyQualified('sizeof'), [new Arg($expr->var)]),
);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -702,6 +702,8 @@ public function dataFileAsserts(): iterable
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6500.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6488.php');
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6488.php
@@ -0,0 +1,26 @@
<?php

namespace Bug6488;

use function PHPStan\Testing\assertType;

function test() {
$items = array_fill(0, rand(0, 5), rand(0, 100));


if (sizeof($items) === 0) {
return false;
}

if (sizeof($items) === 1) {
return false;
}

foreach ($items as $key => $value) {
if ($value % 2 === 0) {
unset($items[$key]);
}
}

assertType('bool',sizeof($items) > 0);
}

0 comments on commit bd4f179

Please sign in to comment.