Skip to content

Commit

Permalink
Merge branch refs/heads/1.10.x into 1.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
phpstan-bot committed May 3, 2024
2 parents a16184e + 0b6d92d commit e1a61a6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Analyser/TypeSpecifier.php
Expand Up @@ -243,13 +243,13 @@ public function specifyTypesInCondition(
&& $leftType->isInteger()->yes()
) {
if (
$context->truthy() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes())
|| ($context->falsey() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes())
$context->true() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes())
|| ($context->false() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes())
) {
$argType = $scope->getType($expr->right->getArgs()[0]->value);
if ($argType->isArray()->yes()) {
$newType = new NonEmptyArrayType();
if ($context->truthy() && $argType->isList()->yes()) {
if ($context->true() && $argType->isList()->yes()) {
$newType = AccessoryArrayListType::intersectWith($newType);
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -1475,6 +1475,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6613.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10187.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10834.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10952.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10893.php');
}

Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10952.php
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Bug10952;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @return array<int, string>
*/
public function getArray(): array
{
return array_fill(0, random_int(0, 10), 'test');
}

public function test(): void
{
$array = $this->getArray();

if (count($array) > 1) {
assertType('non-empty-array<int, string>', $array);
} else {
assertType('array<int, string>', $array);
}

match (true) {
count($array) > 1 => assertType('non-empty-array<int, string>', $array),
default => assertType('array<int, string>', $array),
};
}
}

0 comments on commit e1a61a6

Please sign in to comment.