Skip to content

Commit

Permalink
Fix count(list) regression
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Dec 8, 2023
1 parent f73a165 commit fe8d164
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function specifyTypesInCondition(
$argType = $scope->getType($expr->right->getArgs()[0]->value);
if ($argType->isArray()->yes()) {
$newType = new NonEmptyArrayType();
if ($argType->isList()->yes()) {
if ($context->truthy() && $argType->isList()->yes()) {
$newType = AccessoryArrayListType::intersectWith($newType);
}

Expand Down
15 changes: 14 additions & 1 deletion tests/PHPStan/Analyser/data/bug-10264.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bug10264;

use function PHPStan\Testing\assertType;
use stdClass;

class A
{
Expand All @@ -15,6 +16,18 @@ function doFoo() {
assert((count($list) <= 1) === true);
assertType('list<Bug10264\A>', $list);
}
}

/** @param list<int> $c */
public function sayHello(array $c): void
{
assertType('list<int>', $c);
if (count($c) > 0) {
$c = array_map(fn () => new stdClass(), $c);
assertType('non-empty-list<stdClass>', $c);
} else {
assertType('array{}', $c);
}

assertType('list<stdClass>', $c);
}
}

0 comments on commit fe8d164

Please sign in to comment.