Skip to content

Commit

Permalink
phpstan-assert with count results in type loss
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Dec 8, 2023
1 parent c445490 commit 34dfd6a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PHPStan\Reflection\ResolvedFunctionVariant;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\HasOffsetType;
Expand Down Expand Up @@ -247,7 +248,14 @@ public function specifyTypesInCondition(
) {
$argType = $scope->getType($expr->right->getArgs()[0]->value);
if ($argType->isArray()->yes()) {
$result = $result->unionWith($this->create($expr->right->getArgs()[0]->value, new NonEmptyArrayType(), $context, false, $scope, $rootExpr));
$newType = new NonEmptyArrayType();
if ($argType->isList()->yes()) {
$newType = AccessoryArrayListType::intersectWith($newType);
}

$result = $result->unionWith(
$this->create($expr->right->getArgs()[0]->value, $newType, $context, false, $scope, $rootExpr),
);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/falsey-empty-certainty.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8366.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7291.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10264.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/conditional-vars.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10189.php');
}
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10264.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bug10264;

use function PHPStan\Testing\assertType;

class A
{
function doFoo() {
/** @var list<A> $list */
$list = [];

assertType('list<Bug10264\A>', $list);

assert((count($list) <= 1) === true);
assertType('list<Bug10264\A>', $list);
}
}


0 comments on commit 34dfd6a

Please sign in to comment.